How to print a list of vertices' location and IDs using python?

(Wenye He) #1

Hello,
I am new to VTK.
I want to print a list of vertices’ location and IDs from an imported mesh.
For a list of vertices, I have to loop “GetPoint(i)”…?
For a list of IDs, “GetPointIds” return the list of point ids, but how to print the list of IDs.
What I got is vtkIdList…
Besides, how to use “PrintSelf”?

(Jean M Favre) #2

It seems like you want a list of ids and coordinates as numpy arrays.
I would suggest the following:

from vtk.util import numpy_support
pd=mesh.GetPointData()
ida=pd.GetGlobalIds()
nida=numpy_support.vtk_to_numpy(ida)
coords = mesh.GetPoints().GetData()
ncoords = numpy_support.vtk_to_numpy(coords)

ncoords and nida are numpy arrays. easy to view on your terminal.

HTH

(Wenye He) #3

I did my work by loops. Thanks for your suggestion. I am doing point2point registration. I modify the arrange and locations of the points in numpy array and replace those of copy of target mesh. Is there any document of numpy_support for numpy_to_vtk?

(Bane Sullivan) #4

PyVista might be a library worth checking out - it creates a streamlined interface to the VTK Python bindings making tasks like this way simpler:

import pyvista as pv
import numpy as np

# Load your file
mesh = pv.read('my_file.vtk')
# Access all the points
print(mesh.points)

Also note, we handle all the data conversions between VTK and NumPy for you. These can get complicated depending on your data type

assert isinstance(mesh.points, np.ndarray)

For the IDs, we preserve the order of all data from the underlying VTK data object. The IDs would be the index along the 0th axis of the mesh.points numpy array:

ids = np.arange(mesh.n_points, dtype=int)
print(ids)