Aug 4, 2009

Actor Color / Colour Change

I am using VTK (Visualization Toolkit) to display a shape, read in from a file called test.vtk
I am coding in Python. I want to be able to change the shape from the default redish to a blue.
According to http://www.vtk.org/VTK/help/examplecode.html, a tried drawing a blue sphere, in python. It worked.

from vtk import *
sphere = vtkSphereSource()
sphere.SetRadius(1.0)
sphere.SetThetaResolution(18)
sphere.SetPhiResolution(18)
map = vtkPolyDataMapper()
map.SetInput(sphere.GetOutput())
aSphere = vtkActor()
aSphere.SetMapper(map)
aSphere.GetProperty().SetColor(0,0,1)
ren1 = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren1.AddActor(aSphere)
ren1.SetBackground(1,1,1)
renWin.Render()
iren.Start()

Problem:

However, when I changed the code to read in my data file, test.vtk, it did not work. By this, I mean that the shape was still drawn, but it was redish, and not blue. The first few lines of the above were changed to:

from vtk import *
Reader = vtkUnstructuredGridReader()
Reader.SetFileName("test.vtk")
sphere = Reader # cone is an unstructured grid

map = vtkDataSetMapper()
map.SetInput(sphere.GetOutput())
#
aSphere = vtkActor()

etc...as above.


Solution:

Add this line:

map.ScalarVisibilityOff()

at the place where the '#' is positioned above.

Why?

The colour in the actor’s property only takes effect if there is no scalar data available to the actor’s mapper. By default, the mapper’s input scalar data colours the actor, and the actor’s colour is ignored. To ignore the scalar data, use the method ScalarVisibilityOff().

(Taken from the book: The VTK User's Guide; updated for VTK Version 5)