[vtkusers] How to color a sphere source in an appendpolydata object?

Rishi Rawat rishiraw at usc.edu
Fri Jan 29 13:04:48 EST 2016


How to color a sphere source in an appendpolydata object?

Eventually, I want to plot hundreds+ spheres in 3D, each with a different
color. I'd like to use an appendpolydata object to improve efficiency.
Below is the code I have so far using 1 sphere.

I think I've tracked the problem down to the appendData object. doing
appendData.AddInputData(polydata sp ) causes nothing to show up in my
renderwindow, whereas if I do
appendData.AddInputConnection(Sphere.GetOutputPort()
), I get an object to show up, but it is gray (no color).

How can I color objects before placing them in the appendpolydata object?


My Code:


import vtk
from vtk import *
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

#setup colors (setting the name to "Colors" is nice but not necessary)
Colors = vtk.vtkUnsignedCharArray();
Colors.SetNumberOfComponents(3);
Colors.SetName("Colors");
Colors.InsertNextTuple3(255,0,0);


sphere = vtk.vtkSphereSource()
sphere.SetThetaResolution(5)
sphere.SetPhiResolution(5)
sphere.SetRadius(10)
sphere.SetCenter( 0,0,0)
sp = sphere.GetOutput()
sp.GetCellData().SetScalars(Colors);
appendData.AddInputData(sp )


# If I enable this next line, i get a sphere, but it is not colored, it is
gray
#appendData.AddInputConnection(sphere.GetOutputPort() )

print "done"
#cd.SetScalars(Colors)
#cd.Update()
#sphere.Update()




mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    mapper.SetInput(trianglePolyData)
else:
    #mapper.SetInputData(trianglePolyData)
    mapper.SetInputConnection(appendData.GetOutputPort() )
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# assign actor to the renderer
ren.AddActor(actor)

# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160129/17021aca/attachment.html>


More information about the vtkusers mailing list