[vtkusers] individually colored points in Python?

David Gobbi david.gobbi at gmail.com
Thu Feb 3 12:25:31 EST 2011


Thanks for fixing the example.  The "import hybrid" isn't needed for
anything, I've removed it.

 - David

On Thu, Feb 3, 2011 at 10:01 AM, Lucas <wsacul at gmail.com> wrote:

> Yes that and SetScalars make it work.
>
> I changed the example code on
> http://www.vtk.org/Wiki/VTK/Examples/Python/SolidColoredTriangle to use
> SetScalars (not sure what the 'import hybrid' is there for, I didn't get it
> with my python-vtk install so had to delete it to run the example)-
>
> Thanks!
>
> Lucas
>
>
> On Wed, Feb 2, 2011 at 4:18 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>
>> 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
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110203/76cd144b/attachment.htm>


More information about the vtkusers mailing list