# How to set face color for vtkAnnotatedCubeActor?

**URL:** https://discourse.vtk.org/t/how-to-set-face-color-for-vtkannotatedcubeactor/8378
**Category:** Support
**Created:** [April 26, 2022, 3:33am UTC](https://discourse.vtk.org/t/how-to-set-face-color-for-vtkannotatedcubeactor/8378 "2022-04-26T03:33:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [April 26, 2022, 3:33am UTC](https://discourse.vtk.org/t/how-to-set-face-color-for-vtkannotatedcubeactor/8378/1 "2022-04-26T03:33:32Z")

</div>

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:

> [@Colors of vtkAnnotatedCubeActor faces (with vtkOrientationMarkerWidget)](https://discourse.vtk.org/t/colors-of-vtkannotatedcubeactor-faces-with-vtkorientationmarkerwidget/934/4):
>
> Hi Marco! The best solution I could think of is to compose the letters from the vtkAnnotatedCubeActor with a basic cube actor of the same size with colored faces. In order to do that, you should: Hide the annotated cube faces by setting it’s opacity property to 0 Create an array to hold the six colors for the faces Create a actor based on the vtkCubeSource setting its CellData Scalars to use the color array Merge the vtkAnnotatedCubeActor and colored actor using vtkPropAssembly Use the vt…

[https://kitware.github.io/vtk-examples/site/Python/VisualizationAlgorithms/ColoredAnnotatedCube/](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:

```auto
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:

```auto
axact = vtk.vtkAnnotatedCubeActor()
actors = vtk.vtkPropCollection()
axact.GetActors(actors)
cubeData = list(actors)[0].GetMapper().GetInput()

```

The color of face can be changed by

```auto
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:

1. when I run it as release, the face color do not change

2. when I run it as debug with break point, the face color do not change  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/8/8e5f427f932fd56d391a29ba6db6ec2cf3f0e6ec.png)

3. when I run it as debug with a break point at `cubeData.GetCellData().SetScalars(face_colors)`, the face color change as what I expected:  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/9/98ff349c2a44f5b8d5fd2e5a82332dbc6d8d1326.png)

My environment is:

```auto
win 10
vtk 9.0.3
pycharm 2021.3

```

It really make me confuzed. Any suggestion is appreciated~~~

The complete demo code is:

```auto
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()

```
