Jakob S. - 7 months ago 68

Python Question

I am using Mayavi2 in a Python script to calculate 3d iso-surfaces. As a result I get a vtkPoints object. Now I want to convert this vtkPoints object ('vtkout' in the code sample below) to a simple numpy array with 3 lines containing all x, y and z values.

I get vtkout using a code like this:

`import numpy`

from enthought.mayavi import mlab

import array

randVol = numpy.random.rand(50,50,50) # fill volume with some random potential

X, Y, Z = numpy.mgrid[0:50, 0:50, 0:50] # grid

surf = mlab.contour3d(X, Y, Z, randVol, contours=[0.5]) # calc contour

vtkout = surf.contour.contour_filter.output.points # get the vtkPoints object

At the moment I use the following code to extract the points into an array:

`pointsArray = numpy.zeros((3, vtkout.number_of_points))`

for n in range(vtkout.number_of_points):

pointsArray[0,n] = vtkout[n][0]

pointsArray[1,n] = vtkout[n][1]

pointsArray[2,n] = vtkout[n][2]

I wonder if there is no general routine doing such conversions for me in a convenient, fast and safe way?

Answer

As confirmed from comments on the original post, you might try:

```
vtkout.to_array().T
```

This is a direct method that does not require looping.

Source (Stackoverflow)