Pyvista access to VTK file values

1.1k Views Asked by At

I have a VTK file and I can open that using pyvista. When I open this file in any 3D viewer applicatoins (e.g. Paraview) I can see the points and their values (there are many points distributed in X,Y,Z and each point has its own value).

In pyvista I can only see the points coordinates and don't know how to access the values (or labels) at each coordinate.

import pyvista as pv
pd = pv.read('data.vtk')
pd.points

# UnstructuredGrid (0x20fef143e28)
#  N Cells: 0
#  N Points:    80851
#  X Bounds:    -2.570e+03, 2.550e+03
#  Y Bounds:    -1.280e+03, 1.280e+03
#  Z Bounds:    -1.075e+03, 2.048e+02
#  N Arrays:    0
1

There are 1 best solutions below

2
On

You're referring to the point_arrays property, which you can use to access scalars associated with each point.

If you've assigned a value to each point within a mesh, you can access it with the point_arrays property, which behaves like a python dictionary. For example, when generating a Sphere within pyvista, the normals are included with the mesh and can be accessed with:

>>> import pyvista as pv
>>> mesh =  pv.Sphere()
>>> mesh.point_arrays['Normals']
pyvista_ndarray([[ 0.        ,  0.        ,  1.        ],
                 [ 0.        ,  0.        , -1.        ],
                 [ 0.10811902,  0.        ,  0.99413794],
                 ...,
                 [ 0.31232402, -0.06638652, -0.9476532 ],
                 [ 0.21027282, -0.04469487, -0.97662055],
                 [ 0.10575636, -0.02247921, -0.99413794]], dtype=float32)

See the Basic API Usage for more details.