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

Cory Quammen cory.quammen at kitware.com
Fri Jan 29 14:55:09 EST 2016


Hi Rishi,

Answers inlined below:

On Fri, Jan 29, 2016 at 1:04 PM, Rishi Rawat <rishiraw at usc.edu> wrote:

> 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?
>

VTK doesn't quite work that way, but you are close. See my notes below.


> 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);
>

You'll need a color for *each* cell in in each sphere that you are
appending together. You'll need a call to

Colors.SetNumberOfTuples()

after Colors.SetNumberOfComponents(3) and pass it the number of cells in
the sphere source. Then, you'll need to duplicate the color by calling

Colors.InsertNextTuple3()

once for each cell. You can actually use

color = [255, 0, 0]
Colors.SetTupleValue(i, color)

where i is the cell index because you have already allocated the array when
you called Colors.SetNumberOfTuples().


> sphere = vtk.vtkSphereSource()
> sphere.SetThetaResolution(5)
> sphere.SetPhiResolution(5)
> sphere.SetRadius(10)
> sphere.SetCenter( 0,0,0)
> sp = sphere.GetOutput()
>

You'll need a call to 'sphere.Update()' here so that the output is
generated, otherwise 'sp' will be empty and you'll be appending a lot of
empty data sets.


> 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)
>

You'll also need to call

mapper.SetColorModeToDirectScalars()

to ensure that your spheres are colored by the colors you set in the cell
data array rather than being mapped through a color map.

HTH,
Cory


>
> # assign actor to the renderer
> ren.AddActor(actor)
>
> # enable user interface interactor
> iren.Initialize()
> renWin.Render()
> iren.Start()
>
>
> _______________________________________________
> 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
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>


-- 
Cory Quammen
R&D Engineer
Kitware, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160129/c11838e8/attachment.html>


More information about the vtkusers mailing list