[vtkusers] Mixing Boost.Python and VTK/Python

Bryn Lloyd lloyd at itis.ethz.ch
Thu May 26 06:05:06 EDT 2011


Hi

 

A similar question has been asked here before, but I have not managed to get
a working solution from it. 

I have a class which returns a vtkUnstructuredGrid. I wrap my class using
boost python.

 

class MyClass { 

public: 

vtkUnstructuredGrid* GetGrid () { return m_Grid; }

private:

vtkUnstructuredGrid * m_Grid;
};

 

 

But if I call the function GetGrid in Python:

 

from vtk import * 

from MyModule import *

obj = MyClass()

grid = obj.GetGrid()

 

 

I get following error:

TypeError: No Python class registered for C++ class class
vtkUnstructuredGrid

 

 

The class MyClass has been wrapped as follows:

 

template<class T>

struct vtkObject_to_python

{

       static PyObject *convert(const T &p)

       {

              std::ostringstream oss;

              oss << (void*) &p;

              std::string address_str = oss.str();

 

              object obj = import("vtk").attr("vtkObjectBase")(address_str);

              return incref(obj.ptr());

       }

};

 

BOOST_PYTHON_MODULE(MyModule)

{

       to_python_converter<vtkUnstructuredGrid,

              vtkObject_to_python<vtkUnstructuredGrid> >();

 

       class_<MyClass>("MyClass")

              .def("GetGrid",&MyClass::GetGrid,
return_value_policy<reference_existing_object>())

       ;

}

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110526/12aba3ad/attachment.htm>


More information about the vtkusers mailing list