Access point/cell values in Paraview Python scripting

I’ve been using the Paraview GUI for some time, I just got started with the Python scripting feature. I want to access point/cell values from Python. The approach I have tried:

reader = OpenDataFile("/path/to/my/datafile")
tmp = reader.PointData["MyScalarField"]
tmp*2

I get: TypeError: unsupported operand type(s) for *: 'ArrayInformation' and 'int'
So the object I stored in tmp is meta-information about the array and not the array itself.

On the other hand, if I do something very similar from the Python calculator interface: input[0].PointData["MyScalarField"][0]

, I can access the value of MyScalarField at point #1.

Any reference to resources that might help is welcome, including tutorials. I think I lack vtk knowledge.

PS: I posted the exact same question on stackexchange before realizing there was a better place, I hope that is not a problem.

You are mixing VTKPython and pvpython, this is possible, but you need to fetch your dataset from the server to the client first.

reader = OpenDataFile("/path/to/my/datafile")
reader.UpdatePipeline()
dataset = servermanager.Fetch(reader)
dataset.GetPointData().GetArray("MyScalarField")
1 Like

Thank you for your answer. For any other beginners out there, the last line results in a vtk data array. To inspect a component of the array, you can call the method GetComponent:

tmp = dataset.GetPointData().GetArray("MyScalarField") #results in vtk data array
tmp.GetComponent(point_number, 0) #component index is 0 for a scalar array

This works for Unstructured Grids, you might get an error at the fetching step for other types of data sets.

1 Like