CameraOrientationWidget
Description¶
Use multiple camera orientation widgets in a window with the provision to relabel the axes labels and change axis colors.
To use the snippet, click the Copy to clipboard at the upper right of the code blocks.
Implementation¶
from vtkmodules.vtkInteractionWidgets import vtkCameraOrientationWidget
def set_axes_labels():
"""
Define the axes labels.
:return: The axes labels.
"""
return {
# Labels are: Anterior, Posterior, Dorsal, Ventral, Left, Right
'apdvlr': {'+X': 'A', '-X': 'P', '+Y': 'D', '-Y': 'V', '+Z': 'L', '-Z': 'R'},
'apdvrl': {'+X': 'A', '-X': 'P', '+Y': 'D', '-Y': 'V', '+Z': 'R', '-Z': 'L'},
'aprlvd': {'+X': 'A', '-X': 'P', '+Y': 'R', '-Y': 'L', '+Z': 'V', '-Z': 'D'},
'padvlr': {'+X': 'P', '-X': 'A', '+Y': 'D', '-Y': 'V', '+Z': 'L', '-Z': 'R'},
# Labels are: Left, Right, Superior, Inferior, Anterior, Posterior
'lrsiap': {'+X': 'L', '-X': 'R', '+Y': 'S', '-Y': 'I', '+Z': 'A', '-Z': 'P'},
'lrpasi': {'+X': 'L', '-X': 'R', '+Y': 'P', '-Y': 'A', '+Z': 'S', '-Z': 'I'},
'rlpais': {'+X': 'R', '-X': 'L', '+Y': 'P', '-Y': 'A', '+Z': 'I', '-Z': 'S'},
# Default labels
'xyz': None
}
def set_axes_colors():
"""
Define the axes colors.
:return: The axes colors.
"""
color1 = {'+X': 'IndianRed', '-X': 'FireBrick',
'+Y': 'LimeGreen', '-Y': 'DarkGreen',
'+Z': 'Blue', '-Z': 'SteelBlue'}
return {
'apdvlr': color1,
'apdvrl': color1,
'aprlvd': color1,
'padvlr': color1,
'lrsiap': color1,
# Default colors.
'lrpasi': None,
'rlpais': None,
'xyz': None
}
def get_axes_params():
"""
Gather the defined axes labels and colors into a dictionary.
:return: The dictionary of axes labels and colors.
"""
# The keys must be the same.
axes_labels = set_axes_labels()
axes_colors = set_axes_colors()
label_keys = set(axes_labels.keys())
color_keys = set(axes_colors.keys())
common_keys = label_keys.intersection(color_keys)
alc = dict()
for k in common_keys:
alc[k] = (axes_labels[k], axes_colors[k])
return alc
def make_camera_orientation_widget(ren, alc_key='xyz', position=3):
"""
Make a camera orientation widget for a given renderer.
position has these values 0: LowerLeft, 1: UpperLeft, 2: LowerRight, 3: UpperRight
:param ren: The renderer.
:param alc_key: The key specifying the desired labels and colors for the axes.
:param position: Position the camera orientation widget.
:return: The camera orientation widget.
"""
cow = vtkCameraOrientationWidget(parent_renderer=ren, enabled=True)
rep = cow.representation
axes_parameters = get_axes_params()
if alc_key not in axes_parameters:
print(
f'Invalid key for axes labels and colors.'
f'\nValid keys are: {sorted(axes_parameters.keys())}'
f'\nUsing the key: xyz.')
alc_key = 'xyz'
alc = axes_parameters[alc_key]
match position:
case 0:
rep.AnchorToLowerLeft()
case 1:
rep.AnchorToUpperLeft()
case 2:
rep.AnchorToLowerRight()
case _:
rep.AnchorToUpperRight()
if not alc[0] is None:
rep.x_plus_label_text = alc[0]['+X']
rep.x_minus_label_text = alc[0]['-X']
rep.y_plus_label_text = alc[0]['+Y']
rep.y_minus_label_text = alc[0]['-Y']
rep.z_plus_label_text = alc[0]['+Z']
rep.z_minus_label_text = alc[0]['-Z']
if not alc[1] is None:
colors = vtkNamedColors()
rep.x_axis_color = colors.GetColor3d(alc[1]['+X'])
rep.y_axis_color = colors.GetColor3d(alc[1]['+Y'])
rep.z_axis_color = colors.GetColor3d(alc[1]['+Z'])
cow.SetRepresentation(rep)
cow.Off()
return cow
Usage¶
Here we have a custom widget positioned upper left and the default widget positioned upper right.
# Important: The interactor must be set prior to enabling the widget.
category = 'lrpasi'
cow = make_camera_orientation_widget(ren, category, 1)
cow.On()
cow1 = make_camera_orientation_widget(ren)
cow1.On()