Pythonic Callbacks and Iteration in VTK

July 7, 2016

My name is Thomas “Hastings” Greer. A few weeks ago, I began an internship at Kitware’s office in Carrboro, North Carolina. So far, it has been an amazing experience. Even though it has only been a few weeks, I have already had a chance to make significant contributions to the Visualization Toolkit (VTK) for scientific visualization with Python.

With help from Jean-Christophe Fillion-Robin (Kitware) and David Gobbi (University of Calgary), during my initial explorations of VTK, I discovered two features that I could add to improve its integration and ease-of-use with Python. These features were as follows: (1) extend vtkObject::InvokeEvent to support a variety of data types and (2) enable iteration through vtkObjects in a vtkCollection using “Pythonic” iterator syntax. These features are described next.

(1) vtkObject::InvokeEvent

With my first few VTK commits, vtkObject’s method InvokeEvent can support a variety of types of call data. Events are central to the design of VTK, and now, Python scripts have full access to them. This makes it possible for Python code to keep the user updated on the status of a computation or to initiate reactions to user changes—all without writing a line of C++.

(2) vtkCollection Iteration

With my next set of VTK commits, vtkCollection objects can support “Pythonic” iteration! Before, you needed the following:

iterator = myCollection.NewIterator()

obj = iterator.GetCurrentObject()

while obj:

   #do some stuff with the object


   obj = iterator.GetCurrentObject()

Now you can loop through a collection with this:

for obj in myCollection:

   #do stuff!

The Python expression’s obj in myCollection and list(myCollection) also work.

Building upon these initial successes. I am working on confirming and fixing bugs in the Insight Segmentation and Registration Toolkit (ITK) and 3D Slicer. I am also participating it Kitware’s code review process and other open-source software practices.

To learn more about the VTK pipeline infrastructure, consider reading these prior posts.

Leave a Reply