[vtkusers] Mixing Boost.Python and VTK/Python

Bryn Lloyd lloyd at itis.ethz.ch
Wed Jun 1 09:47:54 EDT 2011


Hi David

 

Here is the working code example.  The previous code only could convert
vtkUnstructuredGrid to a PyObject. I realized then, that I actually needed
to write a converter for a pointer to vtkUnstructuredGrid:

 

template<class T>

struct vtkObjectPointer_to_python

{

                static PyObject *convert(const T &p)

                {

                                if(p == NULL)

                                {

                                                return incref(Py_None);

                                }

                                std::ostringstream oss;

                                oss << (void*) p; // p is already a pointer
so don't use &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*,

 
vtkObjectPointer_to_python<vtkUnstructuredGrid*> >();

.

}

 

 

 

 

From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On Behalf
Of Bryn Lloyd
Sent: Friday, May 27, 2011 12:00 PM
To: 'David Gobbi'
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Mixing Boost.Python and VTK/Python

 

Hi David

 

Thanks. I will give it a try and let the vtkusers know if I got a nicer
solution using your suggestion.

 

Bryn

 

 

From: David Gobbi [mailto:david.gobbi at gmail.com] 
Sent: Thursday, May 26, 2011 5:25 PM
To: Bryn Lloyd
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Mixing Boost.Python and VTK/Python

 

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/20110601/04af5138/attachment.htm>


More information about the vtkusers mailing list