Tuesday, June 22, 2010

Installing VTK in Ubuntu and Making VTKPython Work

There are at least two ways to get VTK working in Ubuntu with Python wrapping. The first and easiest is to just install the packages with the Aptitude Package Manager:

sudo apt-get install libvtk5-dev python-vtk

Once you do that, you can run vtkpython and it just works. Unfortunately, the version of VTK in the packages for Ubuntu 10.04 is 5.2. That's a little out of date, and I needed a newer version. Here's how to install VTK 5.6 or newer with Python wrapping enabled:

Make sure CMake is installed:

sudo apt-get install cmake

Download the VTK source from the Downloads page.

Untar it:

tar xvzf vtk-5.6.0.tar.gz

Create an Out-Of-Source build and configure with CMake:

mkdir VTK_BUILD
cd VTK_BUILD
ccmake ../VTK

Make sure you enable python wrapping and set your install prefix to where you want the package to go. The default /usr/local works fine.

sudo make -j 8 install
(the -j 8 for make just makes the build process parallel assuming you've got the processors for it)

You now have VTK installed. Congrats! if you try to run vtkpython though, you'll get an error:


vtkpython: error while loading shared libraries: libvtksys.so.5.6: cannot open shared object file: No such file or directory

To fix this, append these lines to your .bash_profile, .bashrc, or .profile file in your home directory:


# add vtk paths
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib/vtk-5.6"
PYTHONPATH="$PYTHONPATH:/usr/local/lib/vtk-5.6"

You'll need to reset your terminal now.

That sets up your library and python paths for the vtkpython executable. To test this, you can run

brandt@amaterasu:~/work$ vtkpython
vtk version 5.6.0
Python 2.6.5 (r265:79063, Apr 16 2010, 14:15:55) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from vtk import *
>>> renderer = vtkRenderer()
>>> 

If you got that far, your installation works!





Thursday, June 17, 2010

Search and Replace on Multiple Files

If you need to do a search and replace on multiple files in a directory this little one liner in perl does the job easily:

perl -p -i -e 's/oldstring/newstring/g' `grep -ril search *`

The grep makes sure only files that contain the old string are looked at by the perl script, that way you don't waste execution time - important on really large directory trees.