Improved VTK – numpy integration

Recently, we introduced a new Python module called numpy_interface to VTK. The main objective of this module is to make it easier to interface VTK and numpy. This article is the first in a series that introduces this module. Let’s start with a teaser.

This example prints out the following (assuming that you have a relatively new checkout of VTK master from git).

The last three lines are what is new. Note how we used a different API to access the PointData and the Elevation array on the last two lines. Also note that when we printed the Elevation array, the output didn’t look like one from a vtkDataArray. In fact:

prints the following.

So a VTK array is a numpy array? What kind of trickery is this you say? What kind of magic makes the following possible?

Output:

It is all in the numpy_interface module. It ties VTK datasets and data arrays to numpy arrays and introduces a number of algorithms that can work on these objects. There is quite a bit to this module and I will introduce it piece by piece in upcoming blogs.

Let’s wrap up this blog with one final teaser:

Output:

Please note that this example is not very easily replicated by using pure numpy. The gradient function returns the gradient of an unstructured grid – a concept that does not exist in numpy. However, the ease-of-use of numpy is there.

Continue on to Part 2.

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

One Response to Improved VTK – numpy integration

  1. I am really glad to see a set of posts covering numpy integration. Very useful information.

    The code blocks in this post are displaying as just an html dump. I see that you mostly fixed the issue in Part 2, but it would be nice if the code showed up clearly here too. Mainly because it is the first post of this 5 part series.

    In Part 2 and I believe other posts, there are still some issues. For example the > character is displaying as a set of four characters. If I type them here, it won’t let me submit the comment.

Questions or comments are always welcome!