[PySide] Binding C++/VTK code using Shiboken

Matthew Woehlke matthew.woehlke at kitware.com
Tue Dec 17 19:34:11 CET 2013


On 2013-12-17 04:19, Nicolas SAUBAT wrote:
> On 12/13/2013 06:43 PM, Matthew Woehlke wrote:
>> On 2013-12-13 12:18, Nicolas SAUBAT wrote:
>>> If I have a C++ class based on both Qt and VTK (like this one :
>>> http://camitk.imag.fr/apidocumentation/3.2/classcamitk_1_1RendererWidget.html),
>>> I would be able to expose the VTK part using VTK's Python binder OR Qt
>>> part using Shiboken, but not both.
>> *AFAIK* this is correct. At least, I know that I've run into the same
>> problem and didn't come up with a solution.
>>
>>> And then the solution seems to be to still use Shiboken,
>>> and inject code in the wrapped classes (containing CPython code) in
>>> order to tell it how to convert the Python object (PyObject) into a
>>> VTKObject when necessary. Which seems difficult, from the C++ side,
>>> regarding how VTK handles its VTKObjects.
>>> Please let me know if I've misunderstood some points !

So I was talking to someone that claims you *can* get a 
vtkObject[Base?]* from a PyObject (i.e. there is a function for this in 
a VTK Python utility library). *If* that's correct, then you may be able 
to declare all of your VTK types as custom types and write code 
injections to do the C++ <-> Python mapping.

This won't help if you're shiboken-wrapping something derived from a VTK 
class, however. In particular, you wouldn't be able to pass such a class 
to a VTK binding, and you'll be missing the base class members.

> Have you already tried binding VTK with Shiboken on your own ?

I started, but I was working from the angle of trying to implement it 
"in place" in the existing VTK repository, rather than approaching it as 
a separate project. Besides that I don't know that I could publish that 
code, it's not hugely likely to be very useful anyway.

One thing I did "decide" when I was looking at it, however, is that 
you'll probably want to have a script auto-generate the typesystem XML 
in order to implement custom bindings for vtk get/set macros in order to 
do reasonable mapping of functions that take e.g. float*.

IOW, there is lots of this:

   void SetColor(double*)
   void GetColor(double*)

...which should be wrapped with a code injection to take a PySequence of 
≥ 3 (or 4) elements, attempt to fill a local (to the injection) 
double[3] variable with the elements, and pass that to the C++ function. 
And similar for the Get, to pass a local double[3] and turn it into a 
PySequence to return.

The "trick" is that the size of the array is "known" only from the name 
of the macro that produces such functions, hence the need to parse the 
headers separately. (VTK's Python binding basically does something similar.)

-- 
Matthew




More information about the PySide mailing list