Skip to content

fix: PyVista Qt integration in third-party applications #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/223.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: PyVista Qt integration in third applications
24 changes: 17 additions & 7 deletions examples/00-basic-pyvista-examples/qt_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
# =======================
# .. code-block:: python
#
# cube = pv.Cube()
# pv_backend = PyVistaBackend(use_qt=True)
# pl = Plotter(backend=pv_backend)
# pl.plot(cube)
# pl.show()
#
# cube = pv.Cube()
# pv_backend = PyVistaBackend(use_qt=True, show_qt=True)
# pl = Plotter(backend=pv_backend)
# pl.plot(cube)
# pl.backend.enable_widgets()
# pv_backend.scene.show()


#####################
Expand All @@ -67,5 +67,15 @@
# ==========================
# .. code-block:: python
#
# pv_backend.close()
# pv_backend.close()

###########################################
# Integrate the plotter in a Qt application
# =========================================
# .. code-block:: python
#
# pv_backend = PyVistaBackend(use_qt=True, show_qt=False)
# pv_backend.enable_widgets()
#
# # You can use this plotter in a Qt application
# pl = pv_backend.scene
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class PyVistaBackendInterface(BaseBackend):
Whether to show the plane in the plotter.
use_qt : Optional[bool], default: False
Whether to use the Qt backend for the plotter.
show_qt : Optional[bool], default: True
Whether to show the Qt window.
"""

def __init__(
Expand All @@ -100,6 +102,7 @@ def __init__(
plot_picked_names: Optional[bool] = False,
show_plane: Optional[bool] = False,
use_qt: Optional[bool] = False,
show_qt: Optional[bool] = True,
**plotter_kwargs,
) -> None:
"""Initialize the ``use_trame`` parameter and save the current ``pv.OFF_SCREEN`` value."""
Expand Down Expand Up @@ -153,7 +156,12 @@ def __init__(
logger.warning(warn_msg)
self._pl = PyVistaInterface(show_plane=show_plane)
else:
self._pl = PyVistaInterface(show_plane=show_plane, use_qt=use_qt, **plotter_kwargs)
self._pl = PyVistaInterface(
show_plane=show_plane,
use_qt=use_qt,
show_qt=show_qt,
**plotter_kwargs
)

self._enable_widgets = self._pl._enable_widgets

Expand All @@ -166,6 +174,11 @@ def pv_interface(self) -> PyVistaInterface:
"""PyVista interface."""
return self._pl

@property
def scene(self) -> pv.Plotter:
"""PyVista scene."""
return self._pl.scene

def enable_widgets(self):
"""Enable the widgets for the plotter."""
# Create Plotter widgets
Expand Down Expand Up @@ -550,10 +563,16 @@ def __init__(
allow_picking: Optional[bool] = False,
allow_hovering: Optional[bool] = False,
plot_picked_names: Optional[bool] = True,
use_qt: Optional[bool] = False
use_qt: Optional[bool] = False,
show_qt: Optional[bool] = False,
) -> None:
"""Initialize the generic plotter."""
super().__init__(use_trame, allow_picking, allow_hovering, plot_picked_names, use_qt=use_qt)
super().__init__(use_trame, allow_picking, allow_hovering, plot_picked_names, use_qt=use_qt, show_qt=show_qt)

@property
def base_plotter(self):
"""Return the base plotter object."""
return self._pl.scene

def plot_iter(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class PyVistaInterface:
Whether to show the XY plane in the plotter window.
use_qt : bool, default: False
Whether to use the Qt backend for the plotter window.

show_qt : bool, default: True
Whether to show the Qt plotter window.
"""

def __init__(
Expand All @@ -75,6 +76,7 @@ def __init__(
enable_widgets: bool = True,
show_plane: bool = False,
use_qt: bool = False,
show_qt: bool = True,
**plotter_kwargs,
) -> None:
"""Initialize the plotter."""
Expand All @@ -92,7 +94,7 @@ def __init__(
plotter_kwargs.pop("off_screen", None)
scene = pv.Plotter(off_screen=True, **plotter_kwargs)
elif use_qt:
scene = pyvistaqt.BackgroundPlotter()
scene = pyvistaqt.BackgroundPlotter(show=show_qt)
else:
scene = pv.Plotter(**plotter_kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, plotter: Plotter, direction: CameraPanDirection):
"""Initialize the ``DisplacementArrow`` class."""
super().__init__(plotter, direction)
self.direction = direction
self.update()

def callback(self, state: bool) -> None:
"""Move the camera in the direction defined by the button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, plotter: "Plotter") -> None:
self._button: vtkButtonWidget = self._plotter._pl.scene.add_checkbox_button_widget(
self.callback, position=(10, 10), size=30, border_size=3
)
self.update()

def callback(self, state: bool) -> None:
"""Remove or add the hide widget actor upon click.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, plotter_helper: "Plotter") -> None:
self._button: vtkButtonWidget = self.plotter_helper._pl.scene.add_checkbox_button_widget(
self.callback, position=(10, 60), size=30, border_size=3
)
self.update()

def callback(self, state: bool) -> None:
"""Remove or add the measurement widget actor upon click.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, plotter_helper: "Plotter") -> None:
)
self._mb = None
self._mesh_actor_list = []
self.update()

@property
def _meshes(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, plotter: Plotter) -> None:
self._button: vtkButtonWidget = self.plotter.add_checkbox_button_widget(
self.callback, position=(10, 100), size=30, border_size=3
)
self.update()

def callback(self, state: bool) -> None:
"""Remove or add the ruler widget actor upon click.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, plotter: Plotter) -> None:
self._button: vtkButtonWidget = self.plotter._pl.scene.add_checkbox_button_widget(
self.callback, position=(45, 100), size=30, border_size=3
)
self.update()

def callback(self, state: bool) -> None:
"""Remove or add the screenshot widget actor upon click.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, plotter: Plotter, direction: tuple):
"""Initialize the ``ViewButton`` class."""
super().__init__(plotter, direction)
self.direction = direction
self.update()

def callback(self, state: bool) -> None:
"""Change the view depending on button interaction.
Expand Down
5 changes: 5 additions & 0 deletions src/ansys/tools/visualization_interface/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def __init__(self, backend: BaseBackend = None) -> None:
else:
self._backend = backend

@property
def backend(self):
"""Return the base plotter object."""
return self._backend

def plot(self, plottable_object: Any, **plotting_options):
"""Plots an object using the specified backend.

Expand Down
Loading