[vtkusers] change ploydata color using vtk.vtkUnsignedCharArray dynamically not re-rendering

Abhishek abhishekworld at gmail.com
Tue Jun 5 19:11:14 EDT 2018


Hi David,
Thanks for pointing out,
Just to be clear InsertValue(id, val) trows error because val should be
int,  I tried InsertTuple(id, tuple) it didn't throw an error but didn't
work either.  End up re-initializing the vtk.vtkUnsignedCharArray  in def
select_color(self) as
           self.colors = vtk.vtkUnsignedCharArray()
            self.colors.SetNumberOfComponents(4)

and it works smooth. :)

Thanks,


On Tue, Jun 5, 2018 at 11:41 PM David E DeMarle <dave.demarle at kitware.com>
wrote:

> Hi Ahishek,
>
> It looks to me like you are appending to the end of the self.colors. Call
> self.colors.GetNumberOfTuples() to verify that.
> If you fix (do replacement with Insertvalue(id, value) instead of
> InserNextValue(value)) I think it will do what you are trying to do.
>
> hth
>
>
>
>
> David E DeMarle
> Kitware, Inc.
> Principal Engineer
> 21 Corporate Drive
> <https://maps.google.com/?q=21+Corporate+Drive+Clifton+Park,+NY+12065&entry=gmail&source=g>
> Clifton Park, NY 12065
> <https://maps.google.com/?q=21+Corporate+Drive+Clifton+Park,+NY+12065&entry=gmail&source=g>
> -8662
> Phone: 518-881-4909
>
> On Mon, Jun 4, 2018 at 8:35 PM, Abhishek <abhishekworld at gmail.com> wrote:
>
>> Greetings,
>> I am new to VTK so may be my question is very basic.
>>
>> I have following code, where the aim is to be able to change the lines
>> colors using dynamically based on data we get from QColorDialog. if I
>> apply the color directly to vtkActor and re-render the window then it
>> just works fine. But eventually I wan to be able to change individual line
>> Points color and thats why I am setting
>> self.linesPolyData.GetCellData().SetScalars(self.colors) when I change
>> the color using QPushButton in function def select_color(self): it
>> doesn't re-render the lines with new color. What am I doing wrong?
>>
>> from Analysis.Channel import *import vtkimport numpy as npimport itertoolsimport sysimport math
>> from PyQt5 import QtGui, QtCorefrom PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QScrollArea, QPushButton, QColorDialogfrom vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
>>
>>
>> # Qt Class which will render the objectsclass MainWindow(QWidget):
>>     def __init__(self, parent = None):
>>         super().__init__()
>>         self.layout = QGridLayout(self)
>>         self.color_button = QPushButton('Select Color')
>>
>>         self.layout.addWidget(self.color_button)
>>         self.color_button.clicked.connect(self.select_color)
>>
>>         self.color = [255, 255, 0, 255]
>>         self.colors = vtk.vtkUnsignedCharArray()
>>         self.colors.SetNumberOfComponents(4)
>>
>>         # Create the polydata where we will store all the geometric data
>>         self.linesPolyData = vtk.vtkPolyData()
>>
>>         # Create three points
>>         origin = [0.0, 0.0, 0.0]
>>         p0 = [1.0, 0.0, 0.0]
>>         p1 = [0.0, 1.0, 0.0]
>>
>>         # Create a vtkPoints container and store the points in it
>>         pts = vtk.vtkPoints()
>>         pts.InsertNextPoint(origin)
>>         pts.InsertNextPoint(p0)
>>         pts.InsertNextPoint(p1)
>>
>>         self.linesPolyData.SetPoints(pts)
>>
>>         # Create the first line (between Origin and P0)
>>         line0 = vtk.vtkLine()
>>         line0.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
>>         line0.GetPointIds().SetId(1, 1)  # the second 1 is the index of P0 in linesPolyData's points
>>
>>         # Create the second line (between Origin and P1)
>>         line1 = vtk.vtkLine()
>>         line1.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
>>         line1.GetPointIds().SetId(1, 2)  # 2 is the index of P1 in linesPolyData's points
>>
>>         self.lines = vtk.vtkCellArray()
>>         self.lines.InsertNextCell(line0)
>>         self.lines.InsertNextCell(line1)
>>         # Add the lines to the polydata container
>>         self.linesPolyData.SetLines(self.lines)
>>
>>         for i in range(0, self.lines.GetNumberOfCells()):
>>             self.colors.InsertNextTuple(self.color)
>>
>>         self.linesPolyData.GetCellData().SetScalars(self.colors)
>>
>>         self.mapper = vtk.vtkPolyDataMapper()
>>         self.mapper.SetInputData(self.linesPolyData)
>>
>>         self.actor = vtk.vtkActor()
>>         self.actor.SetMapper(self.mapper)
>>         self.actor.GetProperty().SetLineWidth(4)
>>
>>         self.vtk_widget = QVTKRenderWindowInteractor()
>>         # vtk_widget.setMinimumSize(round(1024/3), round(1024/3))
>>         ren = vtk.vtkRenderer()
>>         self.vtk_widget.GetRenderWindow().AddRenderer(ren)
>>
>>         ren.AddActor(self.actor)
>>
>>         ren.ResetCamera()
>>         interactor = self.vtk_widget.GetRenderWindow().GetInteractor()
>>         interactor.Initialize()
>>         interactor.Start()
>>
>>         self.layout.addWidget(self.vtk_widget)
>>
>>     def select_color(self):
>>         color = QColorDialog.getColor(QtCore.Qt.green, self)
>>         if color.isValid():
>>             self.color = color.getRgb()
>>             for i in range(0, self.lines.GetNumberOfCells()):
>>                 self.colors.InsertNextTuple(self.color)
>>
>>             self.linesPolyData.GetCellData().SetScalars(self.colors)
>>             self.mapper.SetInputData(self.linesPolyData)
>>             self.linesPolyData.Modified()
>>             self.mapper.Modified()
>>             # self.actor.GetProperty().Modified()
>>             self.vtk_widget.Render()
>>
>>
>> if __name__ == '__main__':
>>     app = QApplication(sys.argv)
>>     win = MainWindow()
>>     win.show()
>>     sys.exit(app.exec_())
>>
>> --
>>
>> Abhishek
>>
>> zeroth.me
>>
>> --
>> Abhishek Patil
>> zeroth.me
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> https://public.kitware.com/mailman/listinfo/vtkusers
>>
>>
> --
Abhishek Patil
zeroth.me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180606/ca406b3d/attachment.html>


More information about the vtkusers mailing list