[vtkusers] Recent additions to Python wrappers

David Gobbi david.gobbi at gmail.com
Tue Sep 12 09:11:47 EDT 2017


Hi All,

The VTK master branch has some new features in the Python wrappers.

First, pass-by-reference is now done with a special "vtk.reference()"
object.  Some of you might have used vtk.mutable() in the past.  Well,
vtk.reference() is the same except that it also allows references to
pointers.  For example, consider this previously unwrapped method in
vtkCellArray:

    void GetCell(vtkIdType loc, vtkIdType &npts, vtkIdType* &pts)

This returns a pointer in the third parameter.  To call this method from
Python, use the following code:

    npts = vtk.reference(0)    # create a proxy to a Python int object
    pts = vtk.reference((0,))  # create a proxy to a Python tuple
    cellarray.GetCell(0, npts, pts)

The "reference" is simply a container with set()/get() methods that the
wrappers can use to return values.  So when GetCell() returns a pointer by
reference, the wrappers convert the pointer to a tuple (since they know how
values are pointed to) and then they assign the reference to that tuple.

A "vtk.reference()" object also acts as a proxy to whatever object it
references.  So vtk.reference(0.0) can be used as a number, and
vtk.reference((1,2,3)) can be used anywhere you would use a tuple.


Also, parameter checks (method preconditions) were added to the array
methods a couple weeks ago.  This means that if you write code like this in
Python, you will no longer get a segfault:

    a = vtk.vtkFloatArray()
    a.SetNumberOfValues(3)
    a.SetValue(10, 1.0)

In this case, the wrappers will raise a ValueError because the index is out
of range.  This is nice, because you can catch the exception and respond
appropriately.

 - David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170912/ba433b63/attachment.html>


More information about the vtkusers mailing list