[vtkusers] Struggling with ColorCellsWithRGB example and Python

Eric Petersen peer9802 at gmail.com
Sat Jan 28 18:21:56 EST 2017


Hello,

I'm relatively new to VTK and I'm running into an issue with a VTK example
and Python (running VTK 6 and Python 3.6 on Win7 x64).  I've got an STL
file where I want to set certain cells to a given color.  I've got the cell
IDs (through ray casting) and colors using a colormap from matplotlib.
>From what I can tell, the ColorCellsWithRGB example should allow me to set
certain triangles to a given color.  My current issue is the mesh is
rendering black given the below code.  If I comment out the 'InsertTuples"
line and uncomment the "InsertNextTuple3" line, triangles are represented
different colors; however, it's random and looks like a kaleidoscope when
it should be monochromatic. FWIW, my STL images render fine when not using
the SetScalars command.  Can someone give me pointers on what I can look at
next so that I can achieve my goals?*.*

class Mesh:
    def __init__(self):
        self.MeshData = None
        self.__OldMeshData = None
        self.FileName = None
        self.Name = None

    def LoadSTL(self, filenameSTL):
        readerSTL = vtk.vtkSTLReader()
        readerSTL.SetFileName(filenameSTL)
        # 'update' the reader i.e. read the .stl file
        readerSTL.Update()
        polydata = readerSTL.GetOutput()
        # If there are no points in 'vtkPolyData' something went wrong
        if polydata.GetNumberOfPoints() == 0:
            raise ValueError(
                "No point data could be loaded from '" + filenameSTL)
            return None

        self.MeshData = polydata
        self.FileName = filenameSTL
        self.Name = self.FileName
        return polydata




cellData = vtk.vtkUnsignedCharArray()
cellData.SetNumberOfComponents(3)
cellData.SetNumberOfTuples(laa.MeshData.GetNumberOfCells())
cellData.SetName('Colors')
for i in range(0,laa.MeshData.GetNumberOfCells()):
    cellData.InsertTuple(i,(.5,.5,.5))
    #cellData.InsertNextTuple3(.5,.5,.5)

laa.MeshData.GetCellData().SetScalars(cellData)
laa.MeshData.Modified()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(laa.MeshData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renderer.AddActor(actor)
renderer.SetBackground((1,1,1))
renderWindow.Render()
renderWindowInteractor.Start
#ren.ShowWindow()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170128/bbb2ffe1/attachment.html>


More information about the vtkusers mailing list