Skip to content

Commit 94553c1

Browse files
committed
Modified VTK import so that it can take advantage of the new vtkmodules structure in VTK==8.2.0 if available. This makes import faster and PyInstaller applications smaller.
1 parent 294c369 commit 94553c1

23 files changed

+161
-58
lines changed

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
long_description = fh.read()
1414

1515
setup(name='vtkplotlib',
16-
version='1.2.0',
17-
description='High level 3D graphics and plotting',
16+
version='1.2.1',
17+
description='High level 3D graphics and plotting powered by VTK',
1818
long_description=long_description,
1919
long_description_content_type="text/markdown",
2020
url='https://vtkplotlib.readthedocs.io/en/latest/index.html',

vtkplotlib/_get_vtk.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding: utf-8 -*-
2+
# =============================================================================
3+
# Created on Wed Jan 15 15:44:54 2020
4+
#
5+
# @author: Brénainn Woodsend
6+
#
7+
#
8+
# one line to give the program's name and a brief idea of what it does.
9+
# Copyright (C) 2019-2020 Brénainn Woodsend
10+
#
11+
# This program is free software: you can redistribute it and/or modify
12+
# it under the terms of the GNU General Public License as published by
13+
# the Free Software Foundation, either version 3 of the License, or
14+
# (at your option) any later version.
15+
#
16+
# This program is distributed in the hope that it will be useful,
17+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
# GNU General Public License for more details.
20+
#
21+
# You should have received a copy of the GNU General Public License
22+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
23+
# =============================================================================
24+
"""
25+
"""
26+
27+
try:
28+
from PyQt5 import QtWidgets, QtGui, QtCore
29+
PyQt5_AVAILABLE = True
30+
del QtWidgets, QtCore, QtGui
31+
except ImportError:
32+
PyQt5_AVAILABLE = False
33+
34+
35+
try:
36+
37+
class vtk(object):
38+
"""This dummpy vtk class mimicks the default vtk module structure
39+
whilst only loading the libraries and corresponding dlls that
40+
vtkplotlib uses. This makes import quicker and pyinstaller builds
41+
smaller.
42+
"""
43+
from vtkmodules.vtkRenderingCorePython import vtkActor
44+
from vtkmodules.vtkRenderingCorePython import vtkRenderer
45+
from vtkmodules.vtkRenderingCorePython import vtkWindowToImageFilter
46+
from vtkmodules.vtkFiltersSourcesPython import vtkCubeSource
47+
from vtkmodules.vtkCommonCorePython import VTK_ID_TYPE
48+
from vtkmodules.vtkCommonCorePython import vtkCommand
49+
from vtkmodules.vtkRenderingCorePython import vtkTextActor
50+
from vtkmodules.vtkInteractionStylePython import vtkInteractorStyleTrackballCamera
51+
from vtkmodules.vtkCommonCorePython import VTK_COLOR_MODE_DEFAULT
52+
from vtkmodules.vtkCommonDataModelPython import vtkImageData
53+
from vtkmodules.vtkInteractionStylePython import vtkInteractorStyleImage
54+
from vtkmodules.vtkFiltersSourcesPython import vtkSphereSource
55+
from vtkmodules.vtkCommonCorePython import vtkPoints
56+
from vtkmodules.vtkRenderingCorePython import VTK_SCALAR_MODE_DEFAULT
57+
from vtkmodules.vtkCommonMathPython import vtkMatrix4x4
58+
from vtkmodules.vtkRenderingAnnotationPython import vtkScalarBarActor
59+
from vtkmodules.vtkCommonCorePython import VTK_COLOR_MODE_MAP_SCALARS
60+
from vtkmodules.vtkRenderingCorePython import VTK_SCALAR_MODE_USE_POINT_DATA
61+
from vtkmodules.vtkIOLegacyPython import vtkPolyDataReader
62+
from vtkmodules.vtkRenderingCorePython import vtkImageMapper
63+
from vtkmodules.vtkRenderingAnnotationPython import vtkLegendBoxActor
64+
from vtkmodules.vtkRenderingCorePython import vtkPolyDataMapper
65+
from vtkmodules.vtkCommonCorePython import VTK_MAJOR_VERSION
66+
from vtkmodules.vtkIOLegacyPython import vtkPolyDataWriter
67+
from vtkmodules.vtkCommonCorePython import VTK_COLOR_MODE_DIRECT_SCALARS
68+
from vtkmodules.vtkCommonCorePython import vtkLookupTable
69+
from vtkmodules.vtkCommonTransformsPython import vtkTransform
70+
from vtkmodules.vtkRenderingCorePython import vtkActor2D
71+
from vtkmodules.vtkRenderingCorePython import VTK_SCALAR_MODE_USE_CELL_DATA
72+
from vtkmodules.vtkFiltersSourcesPython import vtkArrowSource
73+
from vtkmodules.vtkRenderingCorePython import vtkRenderWindowInteractor
74+
from vtkmodules.vtkCommonDataModelPython import vtkCellArray
75+
from vtkmodules.vtkRenderingFreeTypePython import vtkVectorText
76+
from vtkmodules.vtkRenderingCorePython import vtkRenderWindow
77+
from vtkmodules.vtkIOGeometryPython import vtkSTLReader
78+
from vtkmodules.vtkCommonDataModelPython import vtkPolyData
79+
from vtkmodules.vtkFiltersGeneralPython import vtkCursor3D
80+
from vtkmodules.vtkRenderingCorePython import vtkFollower
81+
82+
from vtkmodules.vtkRenderingOpenGL2Python import vtkOpenGLRenderer
83+
84+
from vtkmodules.util import numpy_support
85+
86+
if PyQt5_AVAILABLE:
87+
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
88+
89+
90+
except ImportError:
91+
# pass
92+
import vtk
93+
from vtk.util import numpy_support
94+
95+
if PyQt5_AVAILABLE:
96+
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
97+
98+
99+
numpy_to_vtk = numpy_support.numpy_to_vtk
100+
get_vtk_array_type = numpy_support.get_vtk_array_type
101+
vtk_to_numpy = numpy_support.vtk_to_numpy
102+
get_vtk_to_numpy_typemap = numpy_support.get_vtk_to_numpy_typemap
103+
numpy_to_vtkIdTypeArray = numpy_support.numpy_to_vtkIdTypeArray

vtkplotlib/_numpy_vtk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
import numpy as np
27-
from vtk.util.numpy_support import numpy_to_vtk
27+
from vtkplotlib._get_vtk import numpy_to_vtk
2828

2929
# try:
3030
# numpy_to_vtk(np.arange(4).reshape(2, 2).T)

vtkplotlib/colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import numpy as np
3030
from matplotlib import colors, cm
3131
from pathlib2 import Path
32-
import vtk
33-
from vtk.util.numpy_support import numpy_to_vtk, vtk_to_numpy
32+
from vtkplotlib._get_vtk import vtk, numpy_to_vtk, vtk_to_numpy
3433

3534

3635
try:

vtkplotlib/data/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
# =============================================================================
2424

2525

26-
import numpy as np
2726
import sys
28-
import os
2927
from pathlib2 import Path
3028

3129
import pkg_resources
3230

31+
3332
if getattr( sys, 'frozen', False ) :
34-
# running in a bundle
33+
# running in a pyinstaller bundle
3534
DATA_FOLDER = Path(pkg_resources.resource_filename("vtkplotlib", "")).parent / "vpl-data"
3635
else :
37-
# running live
36+
# running normally
3837
DATA_FOLDER = Path(pkg_resources.resource_filename("vtkplotlib", "")) / "data"
3938

39+
ROOT = DATA_FOLDER.parent
4040

4141

4242
MODELS_FOLDER = DATA_FOLDER / "models"
@@ -55,7 +55,7 @@ def get_rabbit_stl():
5555
def assert_ok():
5656
assert ICONS_FOLDER.is_dir()
5757
assert MODELS_FOLDER.is_dir()
58-
assert os.path.isfile(get_rabbit_stl())
58+
assert Path(get_rabbit_stl()).exists()
5959

6060
if __name__ == "__main__":
6161
assert_ok()

vtkplotlib/figures/QtFigure.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import sys
2828
import os
2929
from pathlib2 import Path
30-
import vtk
30+
from vtkplotlib._get_vtk import vtk, QVTKRenderWindowInteractor
3131

3232
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
33-
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
3433
from vtkplotlib.figures.BaseFigure import BaseFigure, VTKRenderer
3534
from vtkplotlib import nuts_and_bolts
35+
from vtkplotlib._vtk_errors import handler
3636

3737

3838
class QtFigure(BaseFigure, QWidget):
@@ -198,6 +198,11 @@ def __init__(self, name="qt vtk figure", parent=None):
198198
self.renWin
199199
iren = self.iren
200200

201+
# try to prevent error pop-up windows
202+
handler.attach(self.vtkWidget)
203+
handler.attach(self.iren)
204+
handler.attach(self.renWin)
205+
201206

202207
self.data_holder = []
203208
# print("basefig init")
@@ -256,8 +261,8 @@ def test():
256261

257262
# vpl.show()
258263
self.show()
259-
self.__init__()
260-
self.show()
264+
# self.__init__()
265+
# self.show()
261266
# self.vtkWidget.show()
262267
# self.update()
263268
# self.show(False)

vtkplotlib/figures/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from builtins import super
2626

27-
import vtk
27+
from vtkplotlib._get_vtk import vtk
2828
import numpy as np
2929
import os
3030
import sys

vtkplotlib/figures/figure_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import os
3030
from pathlib2 import Path
3131

32-
import vtk
33-
from vtk.util.numpy_support import vtk_to_numpy
32+
from vtkplotlib._get_vtk import vtk, vtk_to_numpy
3433

3534
try:
3635
# Doing this allows the current figure to be remembered if vtkplotlib get's

vtkplotlib/figures/render_window.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
"""
2525

2626

27-
import vtk
27+
from vtkplotlib._get_vtk import (vtk,
28+
numpy_to_vtk,
29+
numpy_to_vtkIdTypeArray,
30+
vtk_to_numpy)
2831
import numpy as np
29-
from vtk.util.numpy_support import (
30-
numpy_to_vtk,
31-
numpy_to_vtkIdTypeArray,
32-
vtk_to_numpy,
33-
)
3432

3533
from vtkplotlib import _vtk_errors, nuts_and_bolts
3634

vtkplotlib/image_io.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
except ImportError:
3939
Image = None
4040

41-
import vtk
42-
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
41+
from vtkplotlib._get_vtk import vtk, vtk_to_numpy, numpy_to_vtk
4342

4443
from vtkplotlib.unicode_paths import PathHandler
4544

0 commit comments

Comments
 (0)