I am using vtkAnnotatedCubeActor
in my application. It has method GetXPlusFaceProperty
to change the text color, but I can not find a method to change the color of face.
Fortunately, a solution has been provided by:
https://kitware.github.io/vtk-examples/site/Python/VisualizationAlgorithms/ColoredAnnotatedCube/
However, I think the solution is too complicated.
After reading the source code of vtkAnnotatedCubeActor
, I find it provide a method GetActors
. The GetActors
will return a vtkPropCollection
which contains 8 actor:
this->Assembly->AddPart( this->CubeActor );
this->Assembly->AddPart( this->XPlusFaceActor );
this->Assembly->AddPart( this->XMinusFaceActor );
this->Assembly->AddPart( this->YPlusFaceActor );
this->Assembly->AddPart( this->YMinusFaceActor );
this->Assembly->AddPart( this->ZPlusFaceActor );
this->Assembly->AddPart( this->ZMinusFaceActor );
this->Assembly->AddPart( this->TextEdgesActor );
So, I think I can obtain the CubeActor
and change its color. The CubeActor
can be obtained by:
axact = vtk.vtkAnnotatedCubeActor()
actors = vtk.vtkPropCollection()
axact.GetActors(actors)
cubeData = list(actors)[0].GetMapper().GetInput()
The color of face can be changed by
colors = vtk.vtkNamedColors()
face_colors = vtk.vtkUnsignedCharArray()
face_colors.SetNumberOfComponents(3)
face_x_plus = colors.GetColor3ub('Red')
face_x_minus = colors.GetColor3ub('Green')
face_y_plus = colors.GetColor3ub('Blue')
face_y_minus = colors.GetColor3ub('Yellow')
face_z_plus = colors.GetColor3ub('Cyan')
face_z_minus = colors.GetColor3ub('Magenta')
face_colors.InsertNextTypedTuple(face_x_minus)
face_colors.InsertNextTypedTuple(face_x_plus)
face_colors.InsertNextTypedTuple(face_y_plus)
face_colors.InsertNextTypedTuple(face_y_minus)
face_colors.InsertNextTypedTuple(face_z_plus)
face_colors.InsertNextTypedTuple(face_z_minus)
cubeData.GetCellData().SetScalars(face_colors)
cubeData.Modified()
list(actors)[0].Modified()
axact.Modified()
When run this code, a stranger thing happen:
-
when I run it as release, the face color do not change
-
when I run it as debug with break point, the face color do not change
-
when I run it as debug with a break point at
cubeData.GetCellData().SetScalars(face_colors)
, the face color change as what I expected:
My environment is:
win 10
vtk 9.0.3
pycharm 2021.3
It really make me confuzed. Any suggestion is appreciated~~~
The complete demo code is:
import vtkmodules.all as vtk
sphere = vtk.vtkSphereSource()
sphere.Update()
sphereMapper = vtk.vtkPolyDataMapper()
sphereMapper.SetInputData(sphere.GetOutput())
sphereActor = vtk.vtkActor()
sphereActor.SetMapper(sphereMapper)
render = vtk.vtkRenderer()
render.AddActor(sphereActor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
cubeAnnotation = vtk.vtkAnnotatedCubeActor()
cubeAnnotation.SetXPlusFaceText('A')
cubeAnnotation.SetXMinusFaceText('P')
cubeAnnotation.SetYPlusFaceText('L')
cubeAnnotation.SetYMinusFaceText('R')
cubeAnnotation.SetZPlusFaceText('S')
cubeAnnotation.SetZMinusFaceText('I')
cubeAnnotation.PickableOff()
actors = vtk.vtkPropCollection()
cubeAnnotation.GetActors(actors)
cubeData = list(actors)[0].GetMapper().GetInput()
colors = vtk.vtkNamedColors()
face_colors = vtk.vtkUnsignedCharArray()
face_colors.SetNumberOfComponents(3)
face_x_plus = colors.GetColor3ub('Red')
face_x_minus = colors.GetColor3ub('Green')
face_y_plus = colors.GetColor3ub('Blue')
face_y_minus = colors.GetColor3ub('Yellow')
face_z_plus = colors.GetColor3ub('Cyan')
face_z_minus = colors.GetColor3ub('Magenta')
face_colors.InsertNextTypedTuple(face_x_minus)
face_colors.InsertNextTypedTuple(face_x_plus)
face_colors.InsertNextTypedTuple(face_y_plus)
face_colors.InsertNextTypedTuple(face_y_minus)
face_colors.InsertNextTypedTuple(face_z_plus)
face_colors.InsertNextTypedTuple(face_z_minus)
cubeData.GetCellData().SetScalars(face_colors)
widget = vtk.vtkOrientationMarkerWidget()
widget.SetOrientationMarker(cubeAnnotation)
widget.SetViewport(0.85, 0.85, 1, 1)
widget.SetInteractor(iren)
widget.SetEnabled(1)
widget.InteractiveOff()
render.ResetCamera()
renWin.Render()
iren.Initialize()
iren.Start()