Skip to content

Commit fef5197

Browse files
AlejandroFernandezLucespyansys-ci-botRobPasMue
authored
fix: PyVista Qt integration in third-party applications (#223)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]>
1 parent fcc5e1d commit fef5197

File tree

12 files changed

+56
-12
lines changed

12 files changed

+56
-12
lines changed

doc/changelog.d/223.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: PyVista Qt integration in third-party applications

examples/00-basic-pyvista-examples/qt_backend.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
# =======================
4545
# .. code-block:: python
4646
#
47-
# cube = pv.Cube()
48-
# pv_backend = PyVistaBackend(use_qt=True)
49-
# pl = Plotter(backend=pv_backend)
50-
# pl.plot(cube)
51-
# pl.show()
52-
#
47+
# cube = pv.Cube()
48+
# pv_backend = PyVistaBackend(use_qt=True, show_qt=True)
49+
# pl = Plotter(backend=pv_backend)
50+
# pl.plot(cube)
51+
# pl.backend.enable_widgets()
52+
# pv_backend.scene.show()
5353

5454

5555
#####################
@@ -67,5 +67,15 @@
6767
# ==========================
6868
# .. code-block:: python
6969
#
70-
# pv_backend.close()
70+
# pv_backend.close()
71+
72+
###########################################
73+
# Integrate the plotter in a Qt application
74+
# =========================================
75+
# .. code-block:: python
76+
#
77+
# pv_backend = PyVistaBackend(use_qt=True, show_qt=False)
78+
# pv_backend.enable_widgets()
7179
#
80+
# # You can use this plotter in a Qt application
81+
# pl = pv_backend.scene

src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class PyVistaBackendInterface(BaseBackend):
9090
Whether to show the plane in the plotter.
9191
use_qt : Optional[bool], default: False
9292
Whether to use the Qt backend for the plotter.
93+
show_qt : Optional[bool], default: True
94+
Whether to show the Qt window.
9395
"""
9496

9597
def __init__(
@@ -100,6 +102,7 @@ def __init__(
100102
plot_picked_names: Optional[bool] = False,
101103
show_plane: Optional[bool] = False,
102104
use_qt: Optional[bool] = False,
105+
show_qt: Optional[bool] = True,
103106
**plotter_kwargs,
104107
) -> None:
105108
"""Initialize the ``use_trame`` parameter and save the current ``pv.OFF_SCREEN`` value."""
@@ -153,7 +156,12 @@ def __init__(
153156
logger.warning(warn_msg)
154157
self._pl = PyVistaInterface(show_plane=show_plane)
155158
else:
156-
self._pl = PyVistaInterface(show_plane=show_plane, use_qt=use_qt, **plotter_kwargs)
159+
self._pl = PyVistaInterface(
160+
show_plane=show_plane,
161+
use_qt=use_qt,
162+
show_qt=show_qt,
163+
**plotter_kwargs
164+
)
157165

158166
self._enable_widgets = self._pl._enable_widgets
159167

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

177+
@property
178+
def scene(self) -> pv.Plotter:
179+
"""PyVista scene."""
180+
return self._pl.scene
181+
169182
def enable_widgets(self):
170183
"""Enable the widgets for the plotter."""
171184
# Create Plotter widgets
@@ -550,10 +563,16 @@ def __init__(
550563
allow_picking: Optional[bool] = False,
551564
allow_hovering: Optional[bool] = False,
552565
plot_picked_names: Optional[bool] = True,
553-
use_qt: Optional[bool] = False
566+
use_qt: Optional[bool] = False,
567+
show_qt: Optional[bool] = False,
554568
) -> None:
555569
"""Initialize the generic plotter."""
556-
super().__init__(use_trame, allow_picking, allow_hovering, plot_picked_names, use_qt=use_qt)
570+
super().__init__(use_trame, allow_picking, allow_hovering, plot_picked_names, use_qt=use_qt, show_qt=show_qt)
571+
572+
@property
573+
def base_plotter(self):
574+
"""Return the base plotter object."""
575+
return self._pl.scene
557576

558577
def plot_iter(
559578
self,

src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class PyVistaInterface:
6464
Whether to show the XY plane in the plotter window.
6565
use_qt : bool, default: False
6666
Whether to use the Qt backend for the plotter window.
67-
67+
show_qt : bool, default: True
68+
Whether to show the Qt plotter window.
6869
"""
6970

7071
def __init__(
@@ -75,6 +76,7 @@ def __init__(
7576
enable_widgets: bool = True,
7677
show_plane: bool = False,
7778
use_qt: bool = False,
79+
show_qt: bool = True,
7880
**plotter_kwargs,
7981
) -> None:
8082
"""Initialize the plotter."""
@@ -92,7 +94,7 @@ def __init__(
9294
plotter_kwargs.pop("off_screen", None)
9395
scene = pv.Plotter(off_screen=True, **plotter_kwargs)
9496
elif use_qt:
95-
scene = pyvistaqt.BackgroundPlotter()
97+
scene = pyvistaqt.BackgroundPlotter(show=show_qt)
9698
else:
9799
scene = pv.Plotter(**plotter_kwargs)
98100

src/ansys/tools/visualization_interface/backends/pyvista/widgets/displace_arrows.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, plotter: Plotter, direction: CameraPanDirection):
5454
"""Initialize the ``DisplacementArrow`` class."""
5555
super().__init__(plotter, direction)
5656
self.direction = direction
57+
self.update()
5758

5859
def callback(self, state: bool) -> None:
5960
"""Move the camera in the direction defined by the button.

src/ansys/tools/visualization_interface/backends/pyvista/widgets/hide_buttons.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __init__(self, plotter: "Plotter") -> None:
5252
self._button: vtkButtonWidget = self._plotter._pl.scene.add_checkbox_button_widget(
5353
self.callback, position=(10, 10), size=30, border_size=3
5454
)
55+
self.update()
5556

5657
def callback(self, state: bool) -> None:
5758
"""Remove or add the hide widget actor upon click.

src/ansys/tools/visualization_interface/backends/pyvista/widgets/measure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __init__(self, plotter_helper: "Plotter") -> None:
5252
self._button: vtkButtonWidget = self.plotter_helper._pl.scene.add_checkbox_button_widget(
5353
self.callback, position=(10, 60), size=30, border_size=3
5454
)
55+
self.update()
5556

5657
def callback(self, state: bool) -> None:
5758
"""Remove or add the measurement widget actor upon click.

src/ansys/tools/visualization_interface/backends/pyvista/widgets/mesh_slider.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, plotter_helper: "Plotter") -> None:
5454
)
5555
self._mb = None
5656
self._mesh_actor_list = []
57+
self.update()
5758

5859
@property
5960
def _meshes(self):

src/ansys/tools/visualization_interface/backends/pyvista/widgets/ruler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self, plotter: Plotter) -> None:
4949
self._button: vtkButtonWidget = self.plotter.add_checkbox_button_widget(
5050
self.callback, position=(10, 100), size=30, border_size=3
5151
)
52+
self.update()
5253

5354
def callback(self, state: bool) -> None:
5455
"""Remove or add the ruler widget actor upon click.

src/ansys/tools/visualization_interface/backends/pyvista/widgets/screenshot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self, plotter: Plotter) -> None:
4949
self._button: vtkButtonWidget = self.plotter._pl.scene.add_checkbox_button_widget(
5050
self.callback, position=(45, 100), size=30, border_size=3
5151
)
52+
self.update()
5253

5354
def callback(self, state: bool) -> None:
5455
"""Remove or add the screenshot widget actor upon click.

src/ansys/tools/visualization_interface/backends/pyvista/widgets/view_button.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(self, plotter: Plotter, direction: tuple):
5656
"""Initialize the ``ViewButton`` class."""
5757
super().__init__(plotter, direction)
5858
self.direction = direction
59+
self.update()
5960

6061
def callback(self, state: bool) -> None:
6162
"""Change the view depending on button interaction.

src/ansys/tools/visualization_interface/plotter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def __init__(self, backend: BaseBackend = None) -> None:
4444
else:
4545
self._backend = backend
4646

47+
@property
48+
def backend(self):
49+
"""Return the base plotter object."""
50+
return self._backend
51+
4752
def plot(self, plottable_object: Any, **plotting_options):
4853
"""Plots an object using the specified backend.
4954

0 commit comments

Comments
 (0)