[vtkusers] Mixing Boost.Python and VTK/Python

Bryn Lloyd lloyd at itis.ethz.ch
Mon Jun 13 10:53:10 EDT 2011


Hi David

I added the example code to the VTK Python Wiki page:

http://www.vtk.org/Wiki/Example_from_and_to_python_converters

I added the from python converter, which uses the __this__ attribute of a VTKPython object to convert the class.
Is this safe practice or could this string vary from system to system, or in new versions of VTK?

Would it be better to use the vtkPythonGetPointerFromObject function from vtkPythonUtil.h, which is a VTK internal function?

Thanks
Bryn






On Jun 1, 2011, at 4:59 PM, David Gobbi wrote:

> Hi Bryn,
> 
> I think that this line:
> 
> oss << (void*) p;
> 
> should probably be this:
> 
> oss << static_cast<vtkObjectBase *>(p);
> 
> Otherwise you might get the wrong pointer when converting
> multi-inherited class pointers.  In VTK multiple inheritance is
> rare, but better safe than sorry.
> 
> - David
> 
> 
> On Wed, Jun 1, 2011 at 7:47 AM, Bryn Lloyd <lloyd at itis.ethz.ch> wrote:
>> 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>())
>> 
>>        ;
>> 
>> }
> 




More information about the vtkusers mailing list