How can I access a field in Paraview's programmable filter as a numpy array?

I want to:

  • Import an existing field as a numpy array
  • Create a similar array for output
  • Register it as a new field for visualization

I'm familiar with the wiki, but it doesn't address the issue.

Given this example:

elev0 = inputs[0].PointData['Elevation']
elev1 = inputs[1].PointData['Elevation']
output.PointData.append(elev1 - elev0, 'difference')

I'd like to do (assuming elev0 and elev1 are numpy arrays):

diff = elev1 - elev0
diff[ diff > 1.0 ] = 1.0
output.PointData.append(diff, 'difference')

but this doesn't work as those are VTKArray objects.


2 Answers 2


With ParaView 4.2, your original code will work as expected. The following is now acceptable.

output.PointData.append(numpyarray, "name")

Reading the Paraview python API, found the following solution to convert back and forth between VTKArray and numpy arrays. This uses the numpy_support and vtk.dataset_adapter modules :

from paraview.numpy_support import vtk_to_numpy
from paraview.vtk.dataset_adapter import numpyTovtkDataArray, vtkDataArrayToVTKArray
import numpy as np

# get paraview.vtk.dataset_adapter.VTKArray object
vtkArr = inputs[0].PointData['Elevation']
# convert to numpy array, shape is (nNodes,) for scalars, (nNodes,nComponents) for vectors
npArr = vtk_to_numpy(vtkArr)
# do math on a copy so that original field is not altered
npArr2 = npArr.copy()
npArr2 *= 2.0
# convert back to VTKArray
vtkArr2 = vtkDataArrayToVTKArray(numpyTovtkDataArray(npArr2))
# append new field to output
output.PointData.append(vtkArr2, 'New field')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.