Hi everyone.
I wanted to keep you appraised of new work we are doing to improve the VTK Python interface. The part I will discuss here is at the wrapper level and applies to all of VTK.
PS: The VTK wrapper is the piece of code that parses the VTK C++ code and generates C code that is compiled into Python modules to interface between Python and the C++ library. There is no hand-written Python code involved.
The first thing that we are doing is to add support for Python properties. This is based on work done by @dgobbi (big round of applause) and is being modified by @jaswantp (another big round of applause) to be included in the current wrappers. This is a work in progress that can be found here: https://gitlab.kitware.com/vtk/vtk/-/merge_requests/10820
This code identifies appropriate GetXXX() and SetXXX() methods and generates Python properties. For example, it will generate a property called point_data
from the method GetPointData
. The GetPointData
method will also be generated as before.
Jaswant is also working on the ability to initialize properties in the constructor. Something like:
c = vtkContourFilter(contours=[10, 20, 30], generate_normals=True)
Going forward, we will also have to make changes to the public API of VTK classes to make them more property-friendly. For example, the example above may require adding a signature like this to vtkContourFilter:
const std::vector<double> GetContours();
void SetContours(const std::vector<double>& ctrs);
Hopefully, this is all straightforward and will get support from the community.
Now, some potentially controversial ideas.
I would like to overlead the Python rshift (>>) operator to enable making pipeline connections:
image = vtkImageData()
# fill image
pipeline = image >> vtkContourFilter(contours=[10,20]) >> vtkShrinkFilter()
pipeline.Update()
# ...
The operator will return the last filter in the pipeline (the shrink filter in this case).
I would also like to add an Execute method (execute in Python) that lets us do this:
image = vtkImageData()
# fill image
output_data = (image >> vtkContourFilter(contours=[10,20]) >> vtkShrinkFilter()).execute()
# ...
and this
image = vtkImageData()
# fill image
output_data = (vtkContourFilter(contours=[10,20]) >> vtkShrinkFilter()).execute(image)
# or
contour = vtkContourFilter(contours=[10,20]) .execute(image)
shrunk_contour = vtkShrinkFilter().execute(contour)
We are exploring and making up as we go so feedback would be very much appreciated.
I am working on improving the dataset_adapter module (which I will likely rename) to make it more pythonic and more tightly integrated with the mainstream Python interface. More on this later.