[vtkusers] individually colored points in Python?

David Gobbi david.gobbi at gmail.com
Wed Feb 2 19:18:24 EST 2011


Hi Lucas,

Changing to SetScalars() was correct.  The SetVectors() method is not for
colors.

The bug in your code appears to be how you are filling in the array.  You
need to instantiate the vtkUnsignedCharArray before the loop, not inside of
the loop:

#setup colors
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetName("Colors")

for i in range(255):
  for j in range(255):
    id = Points.InsertNextPoint(i, j, 1)
    Vertices.InsertNextCell(1)
    Vertices.InsertCellPoint(id)
    Colors.InsertNextTuple3(255,i,j)

They way you had written the code, you were creating 255 different arrays
and setting only the first tuple of each one.

  - David


On Wed, Feb 2, 2011 at 2:27 PM, Lucas <wsacul at gmail.com> wrote:

> I'd like to render a point cloud with points individually colored, so I
> combined the example
> http://www.vtk.org/Wiki/VTK/Examples/Python/TriangleColoredPoints with
> others that sets up the interactive render window.  But the points are all
> white, unless I call SetColor which gives them all the same color.  What am
> I missing?
>
> import vtk
>
> # create a rendering window and renderer
> ren = vtk.vtkRenderer()
> renWin = vtk.vtkRenderWindow()
> renWin.AddRenderer(ren)
> renWin.SetSize(400,400)
>
> # create a renderwindowinteractor
> iren = vtk.vtkRenderWindowInteractor()
> iren.SetRenderWindow(renWin)
>
> #setup points and vertices
> Points = vtk.vtkPoints()
> Vertices = vtk.vtkCellArray()
>
> for i in range(255):
>   for j in range(255):
>     id = Points.InsertNextPoint(i, j, 1)
>     Vertices.InsertNextCell(1)
>     Vertices.InsertCellPoint(id)
>
>     #setup colors
>     Colors = vtk.vtkUnsignedCharArray()
>     Colors.SetNumberOfComponents(3)
>     Colors.SetName("Colors")
>     Colors.InsertNextTuple3(255,i,j)
>
> polydata = vtk.vtkPolyData()
> polydata.SetPoints(Points)
> polydata.SetVerts(Vertices)
> polydata.GetPointData().SetVectors(Colors)
> polydata.Modified()
> polydata.Update()
>
> ######
> mapper = vtk.vtkPolyDataMapper()
> mapper.SetInput(polydata)
>
> actor = vtk.vtkActor()
> actor.SetMapper(mapper)
> ren.AddActor(actor)
>
> # enable user interface interactor
> renWin.Render()
> iren.Initialize()
> iren.Start()
>
>
> Replacing SetVectors() with SetScalars produces some point coloring, but
> the colors seem random.
>
> I'm using vtk 5.4.2 and Python 2.6.6 on Ubuntu.
>
> Thanks,
>
> Lucas
>
> _______________________________________________
> 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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110202/52d0e8dd/attachment.htm>


More information about the vtkusers mailing list