Skip to content

Commit 099ff57

Browse files
fix: Custom picker init arguments (#365)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 3487ed5 commit 099ff57

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: Custom picker init arguments

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ class CustomPicker(AbstractPicker):
6161
The plotter backend to use.
6262
plot_picked_names : bool, optional
6363
Whether to plot the names of picked objects, by default True.
64+
label : str, optional
65+
Extra parameter to exemplify the usage of custom parameters.
6466
"""
65-
def __init__(self, plotter_backend: "Plotter", plot_picked_names: bool = True) -> None:
67+
def __init__(self, plotter_backend: "Plotter", plot_picked_names: bool = True, label: str = "This label: ") -> None:
6668
"""Initialize the ``Picker`` class."""
6769
# Picking variables
6870
self._plotter_backend = plotter_backend
6971
self._plot_picked_names = plot_picked_names
72+
self._label = label
7073

7174
# Map that relates PyVista actors with the added actors by the picker
7275
self._picker_added_actors_map = {}
@@ -104,7 +107,7 @@ def pick_select_object(self, custom_object: MeshObjectPlot, pt: "np.ndarray") ->
104107
if self._plot_picked_names:
105108
label_actor = self._plotter_backend.pv_interface.scene.add_point_labels(
106109
[pt],
107-
[text],
110+
[self._label + text],
108111
always_visible=True,
109112
point_size=0,
110113
render_points_as_spheres=False,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
class AbstractPicker(ABC):
3838
"""Abstract base class for pickers."""
39+
@abstractmethod
40+
def __init__(self, plotter_backend: "Plotter", **kwargs) -> None:
41+
"""Initialize the ``AbstractPicker`` class."""
42+
pass
3943

4044
@abstractmethod
4145
def pick_select_object(self, custom_object: Union[MeshObjectPlot, EdgePlot], pt: "np.ndarray") -> None:

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class PyVistaBackendInterface(BaseBackend):
9191
Whether to use the Qt backend for the plotter.
9292
show_qt : Optional[bool], default: True
9393
Whether to show the Qt window.
94+
custom_picker : AbstractPicker, default: None
95+
Custom picker class that extends the ``AbstractPicker`` class.
96+
custom_picker_kwargs : Optional[Dict[str, Any]], default: None
97+
Keyword arguments to pass to the custom picker class.
9498
"""
9599

96100
def __init__(
@@ -103,6 +107,7 @@ def __init__(
103107
use_qt: Optional[bool] = False,
104108
show_qt: Optional[bool] = True,
105109
custom_picker: AbstractPicker = None,
110+
custom_picker_kwargs: Optional[Dict[str, Any]] = None,
106111
**plotter_kwargs,
107112
) -> None:
108113
"""Initialize the ``use_trame`` parameter and save the current ``pv.OFF_SCREEN`` value."""
@@ -162,7 +167,10 @@ def __init__(
162167
if custom_picker is None:
163168
self._custom_picker = Picker(self, self._plot_picked_names)
164169
elif issubclass(custom_picker, AbstractPicker):
165-
self._custom_picker = custom_picker(self, self._plot_picked_names)
170+
if custom_picker_kwargs:
171+
self._custom_picker = custom_picker(self, **custom_picker_kwargs)
172+
else:
173+
self._custom_picker = custom_picker(self)
166174
else:
167175
raise TypeError("custom_picker must be an instance of AbstractPicker.")
168176

0 commit comments

Comments
 (0)