[vtkusers] vtk, Python and SWIG - 'state of the union'

Mark Asbach mark.asbach at post.rwth-aachen.de
Mon Oct 13 10:15:01 EDT 2003


Hi list,

this topic has come up several times here, but I could find a 
comprehensive and general solution for those who just want to integrate 
vtk with  own python/c++ projects, especially if SWIG or Boost++/python 
is concerned. I' trying to summarize the current state, but there's 
still much information missing, so your feedback is very welcome:


On vtk's own wrappers:

vtk has its own wrapper generator that takes its input from the class 
headers only. Different versions of this generator spit out tcl, python 
or java wrappers. If one wants to extend vtk with own classes, the 
natural way is to use this generator, too. An example an how to do that 
can be found in the vtk distribution under 'Examples/Build/'. However, 
this gets impossible if your new classes need datatypes that are not 
part of the vtk package. Therefore this approach doesn't help if you 
want to add vtk parts and their wrappers to an existing software 
project that already has wrappers and datatypes in your destination 
language.


SWIG:

Today it seems possible to forget about the existing wrappers in vtk 
and just generate your own ones with SWIG:

	http://beckford.netfirms.com/hobbies/swig/

However, this is still a lot of work and can introduce new errors in 
your software; in addition it breaks compatibility with additional 
software that relies on the original wrappers (like MayaVi or the 
pipeline browsers). It would be nice, if one could just intermix SWIG 
generated wrappers (they just produce pointer strings for vtk objects) 
with vtk's own wrappers. In the mailing list were some discussions on 
that issue:

	http://public.kitware.com/pipermail/vtkusers/2000-October/004661.html

I guess I found the code mentioned in the postings (SWIG pointer 
[un]mangling) in Common/vtkPythonUtil.cxx, but doesn't seem to get 
involved in the usual python argument parsing done by vtk. Does anybody 
know, how to 'cast' swig pointers an vtk objects to 'native' vtk 
objects in python? Others seem to have written their own converters 
from vtk/python to SWIG/python:

	http://www.itk.org/pipermail/insight-users/2003-July/004388.html

The above approach seems to be available with itk only and I don't know 
if it's general enough for non-itk projects.


Handwritten wrappers:

At the moment it looks like the only way to get your Python/C++ classes 
adapted to vtk is to write the wrapping code by hand. For details on 
how to do that, refer to:

	http://public.kitware.com/pipermail/vtkusers/2002-October/013980.html
	http://www.python.org/doc/current/api/api.html

Of course, it will be additional work to adapt your handwritten 
wrappers to SWIG also …


Again, comments, corrections and improvements are welcome,

Mark




More information about the vtkusers mailing list