Improved VTK – numpy integration (part 2)

This is my second blog about the recently introduced numpy_interface module. In the first one, I gave a brief overview of the module and shared a few teasers. In this one, I will go over the dataset_adapter module which is part of numpy_interface. This module was designed to simplify accessing VTK datasets and arrays from Python and to provide a numpy-style interface.

The first step in using the dataset_adapter module is to convert an existing VTK dataset object to a dataset_adapter.VTKObjectWrapper. Let’s see how this is done by examining the teaser from the last blog:

will print:

What we did here is to create an instance of the dataset_adapter.PolyData class, which refers to the output of the vtkElevationFilter filter. We can access the underlying VTK object using the VTKObject member:

Note that the WrapDataObject() function will return an appropriate wrapper class for all vtkDataSet subclasses, vtkTable and all vtkCompositeData subclasses. Other vtkDataObject subclasses are not currently supported.

VTKObjectWrapper forwards VTK methods to its VTKObject so the VTK API can be accessed directy as follows:

However, VTKObjectWrappers cannot be directly passed to VTK methods as an argument.

Dataset Attributes

So far, pretty boring, right? We have a wrapper for VTK data objects that partially behaves like a VTK data object. This gets a little bit more interesting when we start looking how one can access the fields (arrays) contained within this dataset.

Note that this works with composite datasets as well:

It is possible to access PointData, CellData, FieldData, Points (subclasses of vtkPointSet only), Polygons (vtkPolyData only) this way. We will continue to add accessors to more types of arrays through this API.

This is it for now. In my next blog in this series, I will talk about the array API and various algorithms the numpy_interface module provides.

Continue on to Part 3.

All posts in this series: Part 1, Part 2, Part 3, Part 4, and Part 5.

One Response to Improved VTK – numpy integration (part 2)

  1. I really think these posts would be awesome as iPython notebooks. That would allow us to download and run the notebook. Letting us poke around as we go along with the material.

Questions or comments are always welcome!