Skip to content

Commit d0f3c2f

Browse files
AlejandroFernandezLucespyansys-ci-botpre-commit-ci[bot]
authored
fix: Avoid importing VTK on startup (#358)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 76c201c commit d0f3c2f

File tree

9 files changed

+30
-13
lines changed

9 files changed

+30
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: Avoid importing VTK on startup

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
2727

2828
import pyvista as pv
29-
from vtkmodules.vtkCommonCore import vtkCommand
30-
from vtkmodules.vtkInteractionWidgets import vtkHoverWidget
31-
from vtkmodules.vtkRenderingCore import vtkPointPicker
3229

3330
import ansys.tools.visualization_interface
3431
from ansys.tools.visualization_interface.backends._base import BaseBackend
@@ -111,6 +108,9 @@ def __init__(
111108
**plotter_kwargs,
112109
) -> None:
113110
"""Initialize the ``use_trame`` parameter and save the current ``pv.OFF_SCREEN`` value."""
111+
from vtkmodules.vtkInteractionWidgets import vtkHoverWidget
112+
from vtkmodules.vtkRenderingCore import vtkPointPicker
113+
114114
# Check if the use of trame was requested
115115
if use_trame is None:
116116
use_trame = ansys.tools.visualization_interface.USE_TRAME
@@ -419,6 +419,9 @@ def enable_set_focus_center(self):
419419

420420
def enable_hover(self):
421421
"""Enable hover capabilities in the plotter."""
422+
from vtkmodules.vtkCommonCore import vtkCommand
423+
from vtkmodules.vtkInteractionWidgets import vtkHoverWidget
424+
422425
self._hover_widget = vtkHoverWidget()
423426
self._hover_widget.SetInteractor(self._pl.scene.iren.interactor)
424427
self._hover_widget.SetTimerDuration(100) # Time (ms) required to trigger a hover event

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from pathlib import Path
2626

2727
from pyvista import Plotter
28-
from vtk import vtkButtonWidget, vtkPNGReader
2928

3029
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
3130

@@ -49,6 +48,8 @@ class Button(PlotterWidget):
4948

5049
def __init__(self, plotter: Plotter, button_config: tuple, dark_mode: bool = False) -> None:
5150
"""Initialize the ``Button`` class."""
51+
from vtk import vtkButtonWidget
52+
5253
super().__init__(plotter)
5354
self._dark_mode = dark_mode
5455
self._button: vtkButtonWidget = self.plotter.add_checkbox_button_widget(
@@ -70,6 +71,8 @@ def callback(self, state: bool) -> None:
7071

7172
def update(self) -> None:
7273
"""Assign the image that represents the button."""
74+
from vtk import vtkPNGReader
75+
7376
if self._dark_mode:
7477
is_inv = "_inv"
7578
else:

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
from pathlib import Path
2424
from typing import TYPE_CHECKING
2525

26-
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
27-
2826
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
2927

3028
if TYPE_CHECKING:
@@ -45,6 +43,9 @@ class HideButton(PlotterWidget):
4543

4644
def __init__(self, plotter: "Plotter", dark_mode: bool = False) -> None:
4745
"""Initialize the ``HideButton`` class."""
46+
from vtk import vtkActor, vtkButtonWidget
47+
48+
4849
# Call PlotterWidget ctor
4950
super().__init__(plotter._pl.scene)
5051
self._dark_mode = dark_mode
@@ -77,6 +78,8 @@ def callback(self, state: bool) -> None:
7778

7879
def update(self) -> None:
7980
"""Define the hide widget button parameters."""
81+
from vtk import vtkPNGReader
82+
8083
if self._dark_mode:
8184
is_inv = "_inv"
8285
else:

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
from pathlib import Path
2424
from typing import TYPE_CHECKING
2525

26-
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
27-
2826
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
2927

3028
if TYPE_CHECKING:
@@ -45,6 +43,8 @@ class MeasureWidget(PlotterWidget):
4543

4644
def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None:
4745
"""Initialize the ``MeasureWidget`` class."""
46+
from vtk import vtkActor, vtkButtonWidget
47+
4848
# Call PlotterWidget ctor
4949
super().__init__(plotter_helper._pl.scene)
5050
self._dark_mode = dark_mode
@@ -89,6 +89,8 @@ def callback(self, state: bool) -> None:
8989

9090
def update(self) -> None:
9191
"""Define the measurement widget button parameters."""
92+
from vtk import vtkPNGReader
93+
9294
if self._dark_mode:
9395
is_inv = "_inv"
9496
else:

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from typing import TYPE_CHECKING
2525

2626
import pyvista as pv
27-
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
2827

2928
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
3029

@@ -45,6 +44,8 @@ class MeshSliderWidget(PlotterWidget):
4544

4645
def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None:
4746
"""Initialize the ``MeshSliderWidget`` class."""
47+
from vtk import vtkActor, vtkButtonWidget
48+
4849
# Call PlotterWidget ctor
4950
super().__init__(plotter_helper._pl.scene)
5051
self._dark_mode = dark_mode
@@ -103,6 +104,8 @@ def callback(self, state: bool) -> None:
103104

104105
def update(self) -> None:
105106
"""Define the mesh slider widget button parameters."""
107+
from vtk import vtkPNGReader
108+
106109
if self._dark_mode:
107110
is_inv = "_inv"
108111
else:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
from pathlib import Path
2424
from typing import TYPE_CHECKING
2525

26-
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
27-
2826
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
2927

3028
if TYPE_CHECKING:
@@ -45,6 +43,7 @@ class PickRotCenterButton(PlotterWidget):
4543

4644
def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None:
4745
"""Initialize the ``PickRotCenterWidget`` class."""
46+
from vtk import vtkActor, vtkButtonWidget
4847
# Call PlotterWidget ctor
4948
super().__init__(plotter_helper._pl.scene)
5049
self._dark_mode = dark_mode
@@ -94,6 +93,7 @@ def callback(self, state: bool) -> None:
9493

9594
def update(self) -> None:
9695
"""Define the measurement widget button parameters."""
96+
from vtk import vtkPNGReader
9797
if self._dark_mode:
9898
is_inv = "_inv"
9999
else:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from pathlib import Path
2525

2626
from pyvista import Plotter
27-
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
2827

2928
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
3029

@@ -43,6 +42,7 @@ class Ruler(PlotterWidget):
4342

4443
def __init__(self, plotter: Plotter, dark_mode: bool = False) -> None:
4544
"""Initialize the ``Ruler`` class."""
45+
from vtk import vtkActor, vtkButtonWidget
4646
# Call PlotterWidget ctor
4747
super().__init__(plotter)
4848
self._dark_mode = dark_mode
@@ -85,6 +85,7 @@ def callback(self, state: bool) -> None:
8585

8686
def update(self) -> None:
8787
"""Define the configuration and representation of the ruler widget button."""
88+
from vtk import vtkPNGReader
8889
if self._dark_mode:
8990
is_inv = "_inv"
9091
else:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from pathlib import Path
2525

2626
from pyvista import Plotter
27-
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
2827

2928
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
3029

@@ -43,6 +42,7 @@ class ScreenshotButton(PlotterWidget):
4342

4443
def __init__(self, plotter: Plotter, dark_mode: bool = False) -> None:
4544
"""Initialize the ``ScreenshotButton`` class."""
45+
from vtk import vtkActor, vtkButtonWidget
4646
# Call PlotterWidget ctor
4747
super().__init__(plotter)
4848
self._dark_mode = dark_mode
@@ -77,6 +77,7 @@ def callback(self, state: bool) -> None:
7777

7878
def update(self) -> None:
7979
"""Define the configuration and representation of the screenshot widget button."""
80+
from vtk import vtkPNGReader
8081
if self._dark_mode:
8182
is_inv = "_inv"
8283
else:

0 commit comments

Comments
 (0)