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

David Gobbi dgobbi at imaging.robarts.ca
Mon Oct 13 12:22:45 EDT 2003


Hi Mark,

1) Getting a swig pointer for a VTK-wrapped object is fairly
straightforward, I added a hook for this years ago:

>>> import vtk
>>> o = vtk.vtkObject()
>>> o.__this__
'_080b1330_vtkObject_p'

But obviously no-one out there uses it, or I would have received
complaints that it doesn't return '_080b1330_p_vtkObject' like it's
supposed to!  I'll fix that today. I myself don't mix SWIG and the VTK
wrappers so you'll have to shout at me if something isn't working.

2) making a VTK-wrapped object from a SWIG pointer is also possible,
but is untested, so if it works I'd like your feedback.  You just
pass the SWIG-style pointer to the VTK-wrapped constructor:

>>> o2 = vtk.vtkObject('_080b1330_p_vtkObject')

This doesn't do swig-style type checking (i.e. you could replace the
'p_vtkObject' with garbage and it would work just as well).  Only
the hexidecimal address is important.

I hope this is what you were looking for.

 - David

--
  David Gobbi, PhD                dgobbi at imaging.robarts.ca
  Advanced Imaging Research Group
  Robarts Research Institute, University of Western Ontario

On Mon, 13 Oct 2003, Mark Asbach wrote:

> 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
>
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>




More information about the vtkusers mailing list