How to install vtk with python bindings on a Mac


Drew at MacResearch has an excellent article on installing vtk on the Mac. However, rather than use python bindings, he accesses vtk via its Cocoa bindings in C. I wanted to use Python bindings however, and it was kinda difficult to install. I shall write down everything I did differently, to install vtk using python bindings. Follow Drew's instructions, and you might want to modify them as below...

--------------------------------------------------------
1. Make sure that you have the right variables in CMakeCache.txt:

To Install the files in the right directory:
CMAKE_INSTALL_PREFIX:PATH=/Users/sankhamukherjee/Develop/VTKBin

Make the following changes for Python bindings:
BUILD_SHARED_LIBS:BOOL=ON
VTK_WRAP_PYTHON:BOOL=ON

I dont particularly care for Cocoa much, so I let the normal bindings be ...
-----------------------------------------

Turns out sudo doesn't accept the root password in the Mac terminal. To tell you the truth, I have no idea what the root password is. So, we cant install python bindings in the /usr/lib directories. I guess this is why we build the program in a VTKBin directory! Crappy alternative, but thats all that there is to offer. I work from the VTKBuild directory for the following commands.

Find your current python library folder (mine's in /usr/lib/python2.6). Create a python2.6 (if you have a different version of python, you would want to create a different directory) folder in the ../VTKBin/lib folder, and copy the entire contents here…

Then, within this directory, create a folder named site-packages.

Make sure that vtk knows where this is …
export PYTHONPATH=/Users/sankhamukherjee/Develop/VTKBin/lib/python2.6/site-packages

Then, compile and install the packages, like Drew.
----------------------------------------------------------------------

Reconfigure (create if needed) the .bash_profile file. Generally, I have seen that blindly imitating the .bash_profile file of someone else is not enough. You need to find the right information before you create the file. My file is shown below as an example.

Note the following:
Your LD_LIBRARY_PATH= the path where you find your .so files.
Your DYLD_LIBRARY_PATH= path where you find your .dyld files.
Your PYTHONPATH= the site-packages directory that you just created.
Your PATH= directory where you find the vtkpython executable.

# Set the path to the vtk libraries for python
export LD_LIBRARY_PATH=/Users/sankhamukherjee/Develop/VTKBin/lib/python2.6/site-packages/VTK-5.4.2-py2.6.egg/vtk:${LD_LIBRARY_PATH}
export DYLD_LIBRARY_PATH=/Users/sankhamukherjee/Develop/VTKBin/lib/vtk-5.4:${DYLD_LIBRARY_PATH}

# Set the python path to use the vtk module
export PYTHONPATH=/Users/sankhamukherjee/Develop/VTKBin/lib/python2.6/site-packages

# Set the path for the bin of the vtk
export PATH=/Users/sankhamukherjee/Develop/VTKBin/bin:$PATH


restart bash for good measure (i.e. do a [cmd]-q on the terminal and thenreopen it)

---------------------------------------------------------------

Run the following program from the website:

#! /usr/bin/python
from vtk import *

# create a rendering window and renderer
ren = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(300,300)
renWin.StereoCapableWindowOn()

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# create an actor and give it cone geometry
cone = vtkConeSource()
cone.SetResolution(8)
coneMapper = vtkPolyDataMapper()
coneMapper.SetInput(cone.GetOutput())
coneActor = vtkActor()
coneActor.SetMapper(coneMapper)

# assign our actor to the renderer
ren.AddActor(coneActor)

# enable user interface interactor
iren.Initialize()

iren.Start()


0 comments:

Post a Comment