[vtkusers] Making the python binding more pythonesque

Dov Grobgeld dov.grobgeld at gmail.com
Sun Feb 16 01:39:32 EST 2014


Thanks for the reply, David. I had a look yesterday at the vtk python
binding sources and I was confused by the levels of complexity. Why do you
need PyVTKCLass at all? Why isn't each wrapped vtk class assigned to its
own PyObject derivation? Why all the extra attribute lookup at run time?

I recently finished a large pyhton binding of a C++ class, see
https://github.com/dov/hirsch . I autogenerated the python code based on
the h-headers, but also left in several points in the process for manual
tweaking. At the moment I don't really see how the same approach can be
done to the current vtk python binding.

For an example of my approach, see e.g.:

https://github.com/dov/hirsch/blob/master/pyhtuple.cc

The parser and the wrapper generator may be found in:

https://github.com/dov/hirsch/blob/master/HParseTypes.py

I wish the vtk python binding was more like this.

Regards,
Dov



On Sat, Feb 15, 2014 at 9:34 PM, David Gobbi <david.gobbi at gmail.com> wrote:

> Hi Dov,
>
> I'll answer in reverse order.  Patches are submitted via gerrit, which
> is a web-based git review tool.  Some details are here:
>
> http://www.vtk.org/Wiki/VTK/Git/Develop
>
> The VTK wrapper-generation tools are written in C because,
> historically, lex and yacc only worked with C (the VTK wrappers
> were first written back in the 1990's).  For my part, at least, I find it
> much easier to simply modify and improve what is already there,
> rather than re-write the wrapper tools in a new language.
>
> Yes, the python wrappers are generated by vtkWrapPython.c.  It
> isn't very well documented, but all the documentation that exists is
> on the VTK wiki (just do a google of "vtk wrappers" and you'll find
> all the relevant wiki pages).
>
> I think that supporting the iterator and sequence protocols is a good
> idea, and I'd be willing to review the change.  The VTK arrays already
> support the old buffer protocol (see PythonCore/PyVTKObject.cxx).
>
>   David
>
> On Sat, Feb 15, 2014 at 11:41 AM, Dov Grobgeld <dov.grobgeld at gmail.com>
> wrote:
> > I've been thinking about how to make the vtk python binding more
> > pythonesque? Here are a couple of examples and what I have in mind:
> >
> > Current syntax:
> >
> > # poly is vtkPolyLine
> > num_points = poly.GetNumberOfPoints()
> > pts = c.GetPoints()
> > for i in range(num_points):
> >    p = pts.GetPoint(i)
> >    foo(p)
> >
> > New syntax:
> >
> > for p in poly:
> >    foo(p)
> >
> > Needed change:
> >
> > For all vtk array like objects, support python sequence iterator API
> tp_iter
> > and tp_iternext.
> >
> > Current syntax:
> >
> > pts = c.GetPoints()
> > p1 = pts.GetPoint(0)
> > p2 = pts.GetPoint(1)
> > p3 = pts.GetPoint(2)
> > norm = [0,0,0]
> > vtk.vtkTriangle.ComputeNormalDirection(p1,p2,p3,norm)
> >
> > New syntax:
> >
> > norm = vtk.vtkTriangle.ComputeNormalDirection(c.GetPoints()[0:3])
> >
> > Needed change:
> >
> > Support sequence methods sq_length, sq_concat, sq_item, sq_slice,
> > For all methods returning values, if the arguments only contain the
> number
> > of input parameters, return the result in a python tuple.
> >
> > Please let me know if you would accept patches for implementing the above
> > behavior.
> >
> > I would also appreciate a description of the mechanism for the
> > autogeneration of the python binding. I assume that
> > VTK/Wrapping/Tools/vtkWrapPython.c is used. (Btw, why is it written in c
> and
> > not in C++ (or even python)?)
> >
> > And finally, what is the proper way of submit patches? Is a pull request
> on
> > github accepted?
> >
> > Regards,
> > Dov
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140216/ac1d1600/attachment.html>


More information about the vtkusers mailing list