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?


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:


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.

3 Responses to Improved VTK – numpy integration

  1. Pingback: Lucas Chavez

  2. Pingback: Michael

    • Pingback: Berk Geveci

Questions or comments are always welcome!