[vtkusers] embedding python vtk pipelines in c++ vtk
David Gobbi
dgobbi at irus.rri.on.ca
Thu Mar 8 14:03:30 EST 2001
Hi Charl,
In order to embed python/vtk in a C++ program, you shouldn't need
any tools other than the vtkpython.so library (vtkpython.dll) that
is part of VTK. Re-wrapping VTK in python using other tools isn't
really going to help.
The basics for embedding python in C/C++ are outlined in
http://www.python.org/doc/current/ext/embedding.html,
you've probably already covered this.
To load the vtkpython extensions, you just have to call
PyObject *obj = PyRun_SimpleString("from vtkpython import *")
Py_XDECREF(obj); // decrement the null that was returned
Note that if your main program is linked to vtkpython.so,
then this call is guaranteed to find it, otherwise it will
search or it in python's load path.
The most efficent way to get the vtkObject pointer from a PyVTKObject
is to include "common/vtkPythonUtil.h" and use e.g.
PyObject *pyobj = PyRun_SimpleString("filter.GetOutput()");
vtkObject *obj = vtkPythonGetPointerFromObject(pyobj, "vtkPolyData");
obj->Register(NULL); // now we own the vtkObject
Py_XDECREF(pyobj); // free the pyobj returned by PyRun
// now you own a reference to 'vtkobj' and can use it from C++
Calling Register/Delete and Py_XINCREF/Py_XDECREF at the appropriate
time is important.
To go the other way around, i.e. create a PyVTKObject from
a vtkObject, use the following:
PyObject *pyobj = vtkPythonGetObjectFromPointer(filter->GetOutput());
and then, to bind the pyobj to a python variable do
PyObject *mainmod = PyImport_ImportModule("__main__");
PyObject *maindict = PyModule_GetDict(mainmod);
Py_XDECREF(mainmod);
PyMapping_SetItemString(maindict,"variablename",pyobj);
You'll probably want to keep 'maindict' around, so don't Py_XDECREF it.
I hope that all of this helps. The important files to read are
vtk/common/vtkPythonUtil.h and vtk/python/python.txt. If you get
anywhere with this let me know, I might want to try it sometime.
- David
--
David Gobbi, MSc dgobbi at irus.rri.on.ca
Advanced Imaging Research Group
Robarts Research Institute, University of Western Ontario
On Thu, 8 Mar 2001, Charl P. Botha wrote:
> Thanks for the tip, but I actually meant it the other way round. My
> application is all in C++, but I would like to be able to interpret little
> snippets of python code (representing VTK pipelines) and connect these
> pipelines with the pipelines in my main C++ code. I am able to interpret
> the python snippets, but I don't know how to connect a python VTK pipeline
> to my "main" C++ vtk pipelines.
>
> On Thu, Mar 08, 2001 at 09:42:00PM +0530, Prabhu Ramachandran wrote:
> > Take a look at the Boost Python Library (http://www.boost.org). It is
> > rather easy to use and allows you to wrap C++ objects into Python very
>
>
More information about the vtkusers
mailing list