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

Abhishek abhishekworld at gmail.com
Mon Jun 4 20:35:26 EDT 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180605/d643c387/attachment.html>


More information about the vtkusers mailing list