The post introduces how to create an environment for learning VTK by Python.

The basic conditions on my computer:
macOS Mojave 10.14
Python 2.7.10

Write a bash script and run it.

cmake ./ -G "Unix Makefiles" \
-DModule_vtkGUISupportQt:BOOL=ON \
-DModule_vtkGUISupportQtOpenGL:BOOL=ON \
-DModule_vtkGUISupportQtSQL:BOOL=ON \
-DModule_vtkGUISupportQtWebkit:BOOL=OFF \
-DModule_vtkRenderingQt:BOOL=ON \
-DModule_vtkViewsQt:BOOL=ON \
-DTK_INTERNAL_PATH:PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/tk-private \
-DPYTHON_EXECUTABLE=/usr/bin/python \

The last four options are related to Python. The variable PYTHON_INCLUDE_DIR represents a path that contains C/C++ files.
Then we make and sudo make install.
Finally, we can see the similar information.

-- Installing: /usr/local/lib/python2.7/site-packages/vtk/
-- Installing: /usr/local/lib/python2.7/site-packages/vtk/
-- Installing: /usr/local/bin/vtkpython

Now we can try to run a python script by command vtkpython, but you may can see the error information, ImportError: No module named vtkCommonCorePython.
We need to set the environment variable PYTHONPPATH for finding python library files.

export PYTHONPATH="/usr/local/lib/python2.7/site-packages":"/usr/local/lib/p ython2.7/site-packages/vtk"

Run it again, you may can see the error information, python Library not loaded: @rpath/QtWidgets.framework/Versions/5/QtWidgets. We have to set the variable DYLD_FRAMEWORK_PATH to find Qt lib files.

export DYLD_FRAMEWORK_PATH=$DYLD_FRAMEWORK_PATH:/Users/weiyang/Qt5.9.2/5.9.2 /clang_64/lib

Let’s write the two commands into the file ~/.bashrc, then run source ~/.bashrc.
Finally, we can run a python script for VTK.

Here is a simple python script for drawing cone in VTK, VTK – A Simple Example Written In Python Script, we can run it by vtkpython.

Categories: VTK

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments

3D Model Viewer: add grid plane and convex hull.
Add google translate tool at right-bottom position.

A prohibited operation
Would love your thoughts, please comment.x