<div dir="ltr"><span style="font-size:12.8px">How to color a sphere source in an appendpolydata object?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">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. </span><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I think I've tracked the problem down to the appendData object. doing appendData.AddInputData(</span><span style="font-size:12.8px">polydata sp ) causes nothing to show up in my renderwindow, whereas if I do appendData.AddInputConnection(</span><span style="font-size:12.8px">Sphere.GetOutputPort() ), I get an object to show up, but it is gray (no color).</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">How can I color objects before placing them in the appendpolydata object?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><div style="font-size:12.8px">My Code:<div><br></div><div><br>import vtk<br>from vtk import *<br># create a rendering window and renderer<br>ren = vtk.vtkRenderer()<br>renWin = vtk.vtkRenderWindow()<br>renWin.AddRenderer(ren)<br><br># create a renderwindowinteractor<br>iren = vtk.vtkRenderWindowInteractor()<br>iren.SetRenderWindow(renWin)<br><br>#setup colors (setting the name to "Colors" is nice but not necessary)<br>Colors = vtk.vtkUnsignedCharArray();<br>Colors.SetNumberOfComponents(3);<br>Colors.SetName("Colors");<br>Colors.InsertNextTuple3(255,0,0);<br><br><br>sphere = vtk.vtkSphereSource()<br>sphere.SetThetaResolution(5)<br>sphere.SetPhiResolution(5)<br>sphere.SetRadius(10)<br>sphere.SetCenter( 0,0,0)<br>sp = sphere.GetOutput()<br>sp.GetCellData().SetScalars(Colors);<br>appendData.AddInputData(sp )<br><br><br># If I enable this next line, i get a sphere, but it is not colored, it is gray<br>#appendData.AddInputConnection(sphere.GetOutputPort() )<br><br>print "done"<br>#cd.SetScalars(Colors)<br>#cd.Update()<br>#sphere.Update()<br><br><br><br><br>mapper = vtk.vtkPolyDataMapper()<br>if vtk.VTK_MAJOR_VERSION <= 5:<br> mapper.SetInput(trianglePolyData)<br>else:<br> #mapper.SetInputData(trianglePolyData)<br> mapper.SetInputConnection(appendData.GetOutputPort() )<br># actor<br>actor = vtk.vtkActor()<br>actor.SetMapper(mapper)<br><br># assign actor to the renderer<br>ren.AddActor(actor)<br><br># enable user interface interactor<br>iren.Initialize()<br>renWin.Render()<br>iren.Start()</div></div><div><br></div>
</div></div>