Skip to content

Commit

Permalink
Refactor axes widget to be on any renderer (pyvista#536)
Browse files Browse the repository at this point in the history
* Refactor axes widget to be on any renderer

* Make create_axes_marker available

* Update docs
  • Loading branch information
banesullivan authored Jan 16, 2020
1 parent ed52d1d commit e631db8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 48 deletions.
4 changes: 2 additions & 2 deletions pyvista/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
from .renderer import CameraPosition, Renderer, scale_point
from .theme import (DEFAULT_THEME, FONT_KEYS, MAX_N_COLOR_BARS,
parse_color, parse_font_family, rcParams, set_plot_theme)
from .tools import (create_axes_orientation_box, opacity_transfer_function,
system_supports_plotting)
from .tools import (create_axes_marker, create_axes_orientation_box,
opacity_transfer_function, system_supports_plotting)
from .widgets import WidgetHelper
88 changes: 46 additions & 42 deletions pyvista/plotting/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,48 +545,53 @@ def _render(self):
def add_axes(self, interactive=None, line_width=2,
color=None, x_color=None, y_color=None, z_color=None,
xlabel='X', ylabel='Y', zlabel='Z', labels_off=False,
box=None, box_args=None):
"""Add an interactive axes widget."""
if interactive is None:
interactive = rcParams['interactive']
if hasattr(self, 'axes_widget'):
self.axes_widget.SetInteractive(interactive)
update_axes_label_color(color)
return
if box is None:
box = rcParams['axes']['box']
if box:
if box_args is None:
box_args = {}
self.axes_actor = create_axes_orientation_box(
label_color=color, line_width=line_width,
x_color=x_color, y_color=y_color, z_color=z_color,
xlabel=xlabel, ylabel=ylabel, zlabel=zlabel,
labels_off=labels_off, **box_args)
else:
self.axes_actor = create_axes_marker(
label_color=color, line_width=line_width,
x_color=x_color, y_color=y_color, z_color=z_color,
xlabel=xlabel, ylabel=ylabel, zlabel=zlabel, labels_off=labels_off)
self.axes_widget = vtk.vtkOrientationMarkerWidget()
self.axes_widget.SetOrientationMarker(self.axes_actor)
if hasattr(self, 'iren'):
self.axes_widget.SetInteractor(self.iren)
self.axes_widget.SetEnabled(1)
self.axes_widget.SetInteractive(interactive)
return
box=None, box_args=None, loc=None):
"""Add an interactive axes widget in the bottom left corner.
Adds to the currently active renderer.
Parameters
----------
interacitve : bool
Enable this orientation widget to be moved by the user.
line_width : int
The width of the marker lines
box : bool
Show a box orientation marker. Use ``box_args`` to adjust.
See :any:`pyvista.create_axes_orientation_box` for details.
"""
self._active_renderer_index = self.loc_to_index(loc)
renderer = self.renderers[self._active_renderer_index]
return renderer.add_axes(interactive=interactive, line_width=line_width,
color=color, x_color=x_color, y_color=y_color, z_color=z_color,
xlabel=xlabel, ylabel=ylabel, zlabel=zlabel, labels_off=labels_off,
box=box, box_args=box_args)

def hide_axes(self):
def hide_axes(self, loc=None):
"""Hide the axes orientation widget."""
if hasattr(self, 'axes_widget'):
self.axes_widget.EnabledOff()
self._active_renderer_index = self.loc_to_index(loc)
renderer = self.renderers[self._active_renderer_index]
return renderer.hide_axes()

def show_axes(self):
def hide_axes_all(self):
"""Hide the axes orientation widget in all renderers."""
for renderer in self.renderers:
renderer.hide_axes()
return

def show_axes(self, loc=None):
"""Show the axes orientation widget."""
if hasattr(self, 'axes_widget'):
self.axes_widget.EnabledOn()
else:
self.add_axes()
self._active_renderer_index = self.loc_to_index(loc)
renderer = self.renderers[self._active_renderer_index]
return renderer.show_axes()

def show_axes_all(self):
"""Show the axes orientation widget in all renderers."""
for renderer in self.renderers:
renderer.show_axes()
return

def isometric_view_interactive(self):
"""Set the current interactive render window to isometric view."""
Expand Down Expand Up @@ -2317,7 +2322,7 @@ def add_scalar_bar(self, title=None, n_labels=5, italic=False,
font_family=None, shadow=False, mapper=None,
width=None, height=None, position_x=None,
position_y=None, vertical=None,
interactive=False, fmt=None, use_opacity=True,
interactive=None, fmt=None, use_opacity=True,
outline=False, nan_annotation=False,
below_label=None, above_label=None,
background_color=None, n_colors=None, fill=False):
Expand Down Expand Up @@ -2406,6 +2411,8 @@ def add_scalar_bar(self, title=None, n_labels=5, italic=False,
sizing for both the title and label.
"""
if interactive is None:
interactive = rcParams['interactive']
if font_family is None:
font_family = rcParams['font']['family']
if label_font_size is None:
Expand Down Expand Up @@ -2720,9 +2727,6 @@ def close(self):
self.last_image = self.screenshot(None, return_img=True)
self.last_image_depth = self.get_image_depth()

if hasattr(self, 'axes_widget'):
del self.axes_widget

if hasattr(self, 'scalar_widget'):
del self.scalar_widget

Expand Down
5 changes: 2 additions & 3 deletions pyvista/plotting/qt_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ def __init__(self, parent=None, title=None, shape=(1, 1), off_screen=None,
# Enter trackball camera mode
istyle = vtk.vtkInteractorStyleTrackballCamera()
self.SetInteractorStyle(istyle)
self.add_axes()

self.iren.Initialize()

Expand Down Expand Up @@ -613,8 +612,8 @@ def __init__(self, show=True, app=None, shape=(1, 1), window_size=None,
view_menu.addSeparator()
# Orientation marker
orien_menu = view_menu.addMenu('Orientation Marker')
orien_menu.addAction('Show', self.show_axes)
orien_menu.addAction('Hide', self.hide_axes)
orien_menu.addAction('Show All', self.show_axes_all)
orien_menu.addAction('Hide All', self.hide_axes_all)
# Bounds axes
axes_menu = view_menu.addMenu('Bounds Axes')
axes_menu.addAction('Add Bounds Axes (front)', self.show_bounds)
Expand Down
69 changes: 68 additions & 1 deletion pyvista/plotting/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pyvista.utilities import wrap

from .theme import parse_color, parse_font_family, rcParams, MAX_N_COLOR_BARS
from .tools import create_axes_marker
from .tools import update_axes_label_color, create_axes_orientation_box, create_axes_marker



Expand Down Expand Up @@ -277,6 +277,71 @@ def add_axes_at_origin(self, x_color=None, y_color=None, z_color=None,
self._actors[memory_address] = self.marker_actor
return self.marker_actor


def add_axes(self, interactive=None, line_width=2,
color=None, x_color=None, y_color=None, z_color=None,
xlabel='X', ylabel='Y', zlabel='Z', labels_off=False,
box=None, box_args=None):
"""Add an interactive axes widget in the bottom left corner.
Parameters
----------
interacitve : bool
Enable this orientation widget to be moved by the user.
line_width : int
The width of the marker lines
box : bool
Show a box orientation marker. Use ``box_args`` to adjust.
See :any:`pyvista.create_axes_orientation_box` for details.
"""
if interactive is None:
interactive = rcParams['interactive']
if hasattr(self, 'axes_widget'):
self.axes_widget.SetInteractive(interactive)
update_axes_label_color(color)
return
if box is None:
box = rcParams['axes']['box']
if box:
if box_args is None:
box_args = {}
self.axes_actor = create_axes_orientation_box(
label_color=color, line_width=line_width,
x_color=x_color, y_color=y_color, z_color=z_color,
xlabel=xlabel, ylabel=ylabel, zlabel=zlabel,
labels_off=labels_off, **box_args)
else:
self.axes_actor = create_axes_marker(
label_color=color, line_width=line_width,
x_color=x_color, y_color=y_color, z_color=z_color,
xlabel=xlabel, ylabel=ylabel, zlabel=zlabel, labels_off=labels_off)
self.axes_widget = vtk.vtkOrientationMarkerWidget()
self.axes_widget.SetOrientationMarker(self.axes_actor)
if hasattr(self.parent, 'iren'):
self.axes_widget.SetInteractor(self.parent.iren)
self.axes_widget.SetEnabled(1)
self.axes_widget.SetInteractive(interactive)
self.axes_widget.SetCurrentRenderer(self)
return


def hide_axes(self):
"""Hide the axes orientation widget."""
if hasattr(self, 'axes_widget'):
self.axes_widget.EnabledOff()


def show_axes(self):
"""Show the axes orientation widget."""
if hasattr(self, 'axes_widget'):
self.axes_widget.EnabledOn()
self.axes_widget.SetCurrentRenderer(self)
else:
self.add_axes()


def show_bounds(self, mesh=None, bounds=None, show_xaxis=True,
show_yaxis=True, show_zaxis=True, show_xlabels=True,
show_ylabels=True, show_zlabels=True, italic=False,
Expand Down Expand Up @@ -1070,6 +1135,8 @@ def deep_clean(self):
del self.edl_pass
if hasattr(self, '_box_object'):
self.remove_bounding_box()
if hasattr(self, 'axes_widget'):
del self.axes_widget

self.RemoveAllViewProps()
self._actors = None
Expand Down
1 change: 1 addition & 0 deletions pyvista/plotting/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'y_color': 'seagreen',
'z_color': 'mediumblue',
'box': False,
'show': True,
},
'multi_samples': 4,
'multi_rendering_splitting_position': None,
Expand Down

0 comments on commit e631db8

Please sign in to comment.