[vtkusers] Can I mix Boost.Python and VTK Python wrapper?

David Gobbi david.gobbi at gmail.com
Wed Feb 23 17:05:25 EST 2011


Hi Andrew,

Don't forget that vtkSmartPointer has a superclass, vtkSmartPointerBase,
that is not templated.  So likewise, your smart-pointer-to-python method
does not have to be templated: just make it use vtkSmartPointerBase
instead of vtkSmartPointer<T>.  Also, use vtkObjectBase(address) instead
of vtkObject(address).

To check for leaks, compile VTK with VTK_DEBUG_LEAKS set in cmake.

 - David







2011/2/23 Андрей Траченко <goodrone at gmail.com>:
> Thank you, David, you helped me a lot!
>
> Using "__this__" as a search keyword I found a very useful page [1],
> describing how to convert VTK objects between C++ and Python. I've
> also used another great article [2] on writing converters in terms of
> Boost.Python.
>
> Now I'm going to describe what exactly I've done.
>
> Suppose we have a class:
>
> class Foo {
> public:
>    vtkSmartPointer<vtkUnstructuredGrid> grid();
> };
>
> and we wrap it using Boost.Python:
>
> // inside BOOST_PYTHON_MODULE(MyModule)
> class_<Foo>("Foo")
>    .def("grid", &Foo::grid);
>
> then we also have to add to_python_converter like this:
>
> // inside BOOST_PYTHON_MODULE(MyModule)
> to_python_converter<vtkSmartPointer<vtkUnstructuredGrid>,
>    vtkSmartPointer_to_python<vtkUnstructuredGrid> >();
>
> and define struct vtkSmartPointer_to_python somewhere before:
>
> template<class T>
> struct vtkSmartPointer_to_python
> {
>    static PyObject *convert(const vtkSmartPointer<T> &p)
>    {
>        std::ostringstream oss;
>        oss << (void*) p.GetPointer();
>        std::string address_str = oss.str();
>
>        using namespace boost::python;
>        object obj = import("vtk").attr("vtkObject")(address_str);
>        return incref(obj.ptr());
>    }
> };
>
> Thereby we can write in Python:
>
> from MyModule import Foo
> f = Foo()
> print f.grid()
>
> Аfter all, I see two major flaws in this approach:
> - every vtk class should get its own to_python_converter (I hope that
> power of Boost.Python will help me to simplify this)
> - I haven't ensured yet that there are no vtkSmartPointer leaks (and
> currently have no idea how).
>
> [1] http://visitusers.org/index.php?title=Python_Filters_Development
> [2] http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/
>
> --
> Andrew
>
> 2011/2/23 David Gobbi <david.gobbi at gmail.com>:
>> Hi Andrew,
>>
>> It is possible to generate VTK/Python objects from the memory address
>> of the C++ VTK objects. In the python wrappers, you can get the memory
>> address of a VTK object like this:
>>
>> o = vtk.vtkObject()
>> address = o.__this__
>>
>> and to create a python-wrapped object from a memory address, do this:
>>
>> o = vtk.vtkObject(address)
>>
>> Also, if you want an object to "look" like a VTK/Python object even if
>> it isn't, you can add a __vtk__() method which will be called when a
>> conversion to a VTK/Python object is required by the wrappers.
>>
>> If you are successful using this to mix Boost.Python and VTK/Python,
>> please report back to the list because there are probably other people
>> who are interested in doing the same.  More information about the
>> wrappers is provided here:
>> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Wrapping/Python/README_WRAP.txt
>>
>> Cheers,
>>
>>  - David
>>
>>
>> 2011/2/23 Андрей Траченко <goodrone at gmail.com>:
>>> I have an application in C++, which uses VTK. Now I'm exposing C++
>>> classes to Python using Boost.Python library and using my C++ classes
>>> within Python scripts.
>>>
>>> Now I want to pass VTK objects between C++ and Python code. Is there
>>> any correct way to do it? As far as I understand, vtkSmartPointer is
>>> used as a "smart pointer" in terms of Boost.Python to wrap VTK C++
>>> classes.
>>>
>>> Currently I'm swapping vtkUnstructuredGrid and other stuff using
>>> temporary files, and it's way too slow on large datasets, so I hope I
>>> could build a "bridge" between C++ and Python for VTK.
>>>
>>> Thanks!
>>> Andrew
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>
>>
>



More information about the vtkusers mailing list