Date

VTK is an open-source, freely available C++ library for 3D computer graphics, image processing, and visualization. It's responsible for the heavy lifting behind Paraview and 3D Slicer.

So it goes without saying that VTK is super-useful for scientific visualization. But there's a troublesome asymmetric gloss in that last sentence: scientists are not known for their willingness to learn how to code, least of all in C++. "STD colon colon what? Bugger off, I've got a grant to write." Good thing VTK ships with a python wrapping option.

During configuration, if you turn on the cmake VTK_WRAP_PYTHON option, the vtk python module will be built which aims to provide a Python interface matching up with its C++ interface. This is useful if you want to use VTK, but don't want to spend a decade learning how to code in C++.

(I kid, VTK has a comprehensive examples directory, but using Python is easier on your eyes, morale, and liver.)

This is also useful if you want to use mayavi, a python library for interactive visualization, which depends on VTK's python wrapping. That's actually what inspired me to put together this post.

Unfortunately the most comprehensive instructions I could find for compiling these wrappers is on a mailing list from 2004. To be fair, the instructions still work pretty well, but I figured I'd write my own in case anyone found it useful.

First thing's first, if you're on Arch Linux, you're going to need an older compiler, VTK will not yet build with GCC 6. (Actually it will, but it won't configure correctly, requiring you to mangle some cmake files -- this will probably be fixed soon, but for now just use an older compiler as I doubt it's fully tested on GCC 6 yet. Maybe once I've tested it I'll release my results to the scientific community.)

Here's my guide to compiling GCC 4.6.3 on Arch. So you could use that, or install an older version from the AUR.

Now we download the latest VTK release, and create our build and install directories.

mkdir vtk && cd vtk
wget http://www.vtk.org/files/release/7.0/VTK-7.0.0.tar.gz
tar xzf VTK-7.0.0.tar.gz
mkdir build
mkdir install
cd build

Now go ahead and configure with the latest cmake:

cmake -DCMAKE_BUILD_TYPE:PATH=Release \
      -DCMAKE_INSTALL_PREFIX:PATH=/home/chris/projects/vtk/install \
      -DBUILD_SHARED_LIBS:BOOL=ON \
      -DVTK_USE_HYBRID:BOOL=ON \
      -DVTK_WRAP_TCL:BOOL=ON \
      -DVTK_WRAP_PYTHON:BOOL=ON \
      -DPYTHON_EXECUTABLE:PATH=/home/chris/pyvenv/mne-venv2/bin/python \
      -DPYTHON_INCLUDE_DIR:PATH=/home/chris/pyvenv/mne-venv2/include/python2.7 \
      /home/chris/projects/vtk/VTK-7.0.0

You'll notice we turned VTK_USE_HYBRID and VTK_WRAP_TCL on. That's because mayavi uses those. You may also need to specify -DVTK_RENDERING_BACKEND:STRING=OpenGL instead of OpenGL2. Also we're using Python 2.7 in this build, but that's only because that's what mayavi requires -- VTK now supports Python 3.

Now we build:

make
make install

Now either at the command line or in your .bashrc file set

export LD_LIBRARY_PATH=/home/chris/projects/vtk/install/lib/

Also in your .bashrc you can put:

export PYTHONPATH="${PYTHONPATH}:/home/chris/projects/vtk/install/lib/python2.7/site-packages"

or simply run the following every time you start the python interpreter:

import sys
sys.path.append('/home/chris/projects/vtk/install/lib/python2.7/site-packages')
sys.path.append('/home/chris/projects/vtk/install/lib/python2.7/site-packages/vtk')

Now you should be able to start your python interpreter and run

import vtk

without errors. Maybe mess around with some of the examples, or maybe continue to run

pip install mayavi

, your call. Happy coding!