[vtk-developers] Python wrappers and pointers

David Gobbi david.gobbi at gmail.com
Fri Sep 17 17:32:49 EDT 2010


Hi all,

I've been struggling for a while to think of the best way to wrap
pointers in python.  It is a difficult thing to do safely, because it
is hard to know the size of the memory area that is pointed to, and
also because the memory might be freed at an inopportune time, leaving
the user with an invalid pointer.

The most sensible idea that I've come up with so far is to make a new
python array type that is a proxy to a block of memory existing within
VTK, usually within a vtkDataArray but potentially from anywhere.
This "proxy array" would be much like a numpy array but with
vtk-specific features:

1) it would have a weak pointer to the underlying VTK object, to allow
it to check that the VTK object still exists before each operation
2) it would store pointers to the Ivars where the VTK object stores
the memory pointer and arrays size, so it can check if these change
3) it would store the data type and the dimensions
4) the dimensions would be reshapeable, as long as the full size (i.e.
product of all dimensions) remained the same
5) it would support slicing

The first and most basic way to get/set this array would be via the
SetArray/GetArray methods of the vtkDataArray subclasses.  Note that
the GetArray() method doesn't exist yet, but I would add it.
Eventually all the pointer methods in the vtkAbstractArray and
vtkArray hierarchies would be supported, and perhaps those in
vtkCellArray and the vtkDataSet.  The extensiveness would only depend
on the amount of hinting for the wrappers.  Any pointer in VTK could
be represented either as one of these array objects, or as a slice.

All of the checks done in 1 and 2 above would make this a fairly safe
mechanism for accessing internal VTK memory.  For multithreading,
thread safety would be the responsibility of the application
developer, i.e. no different from using the VTK objects themselves.

The name of the type would be vtk.array() and it would support
python's sequence protocols and buffer protocols.

Any thoughts on this?  Has anyone else done something similar?

  - David



More information about the vtk-developers mailing list