[vtkusers] Mixing Boost.Python and VTK/Python

David Gobbi david.gobbi at gmail.com
Thu May 26 11:25:02 EDT 2011


Hi Bryn,

Don't use vtkPythonGetObjectFromPointer(), it is an internal function and no
longer exists in the VTK development head.

Your earlier attempt with address_str is the right way to go, but the string
format is important.  Take a look at GetObjectFromObject() in
vtkPythonUtil.cxx to see how the string is decoded.

 - David


On Thu, May 26, 2011 at 8:07 AM, Bryn Lloyd <lloyd at itis.ethz.ch> wrote:

> Although I am still interested in a better solution, I currently have found
> following way to deal with this:
>
>
>
> I wrote a function, which creates a boost::python::object from the
> vtkUnstructuredGrid, which it obtains from my MyClass object:
>
>
>
> #include <vtkPythonUtil.h>
>
> using boost::python::object;
>
> using boost::python::import;
>
>
>
> boost::python::object GetGrid(MyClass *m)
>
> {
>
>                 object vtk_module = import("vtk");
>
>                 vtkUnstructuredGrid *volume = m->GetVolume();
>
>                 if(volume==NULL) return
> object(boost::python::handle<>(Py_None));
>
>                 boost::python::handle<>
> pvolume(vtkPythonGetObjectFromPointer((vtkObjectBase *)volume));
>
>                 return object(pvolume);
>
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
> *From:* vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] *On
> Behalf Of *Bryn Lloyd
> *Sent:* Thursday, May 26, 2011 12:05 PM
> *To:* vtkusers at vtk.org
> *Subject:* [vtkusers] Mixing Boost.Python and VTK/Python
>
>
>
> 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/49ffc82d/attachment.htm>


More information about the vtkusers mailing list