I'm trying to modify the color of each object that I've placed in a vtkAppendPolyData
but I get weird colors. I mean the object is not in one color but each face of it has a different color.
I would like to have one color by object. Here is the minimal example:
import vtk
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(5,0,0)
sphereSource.Update()
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Cellarray = sphereSource.GetOutput().GetPolys().GetNumberOfCells()
Colors.SetNumberOfTuples(Cellarray)
for c in range(Cellarray):
Colors.InsertNextTuple3(255, 0, 0)
sphereSource.GetOutput().GetCellData().SetScalars(Colors)
sphereSource.Update()
coneSource =vtk.vtkConeSource()
coneSource.Update()
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Cellarray = coneSource.GetOutput().GetPolys().GetNumberOfCells()
Colors.SetNumberOfTuples(Cellarray)
for c in range(Cellarray):
Colors.InsertNextTuple3(0, 255, 0)
coneSource.GetOutput().GetCellData().SetScalars(Colors)
coneSource.Update()
# Append the two meshes
appendFilter = vtk.vtkAppendPolyData()
appendFilter.AddInputData(sphereSource.GetOutput())
appendFilter.AddInputData(coneSource.GetOutput())
appendFilter.Update()
# Remove any duplicate points.
cleanFilter = vtk.vtkCleanPolyData()
cleanFilter.SetInputConnection(appendFilter.GetOutputPort())
cleanFilter.Update()
# Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()
# mapper.ScalarVisibilityOn()
mapper.SetInputConnection(cleanFilter.GetOutputPort())
mapper.SetColorModeToDirectScalars()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# Create a renderer, render window, and interactor
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actors to the scene
renderer.AddActor(actor)
renderer.SetBackground(.3, .2, .1) # Background color dark red
# Render and interact
renderWindow.Render()
renderWindowInteractor.Start()