Get attributes and type data associated with points/cells

Hi,
I am trying to extract the attributes associated with the point and cell data from a vtp file that contains tractography streamlines, together with some associated scalar and tensor data.

So far I have been able to extract the names of the attributes with the code I am attaching (I’m using Python). I would like to:

  1. Get the attribute type associated with each one.
  2. Get the actual data associated with each one.

How can I do this?

Relevant code snippet:

import vtk
reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName(in_fname)
reader.Update()

in_data = reader.GetOutput()
vtk_data = in_data.GetPointData()
# vtk_data = in_data.GetCellData()

names = []
if vtk_data.GetNumberOfArrays() > 0:
    point_data_arr_idx = list(range(vtk_data.GetNumberOfArrays()))
    for idx in point_data_arr_idx:
        array = vtk_data.GetArray(idx)
        names.append(array.GetName())

The test data I am using is attached.

Thanks.

T_CC1.vtp (724.2 KB)

Try:

import vtk
from vtkmodules.util.numpy_support import vtk_to_numpy

reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName(in_fname)
reader.Update()

in_data = reader.GetOutput()
vtk_pdata = in_data.GetPointData()

for idx in range(vtk_pdata.GetNumberOfArrays()):
    array = vtk_pdata.GetArray(idx)
    print(array.GetName(), array.GetDataType(), vtk_to_numpy(array).dtype)

with vedo or pyvista with similar syntax:

from vedo import *
lines = load("https://discourse.vtk.org/uploads/short-url/nC2RjJgTerpHKR0jD02Na6BRHVl.vtp")
for k in lines.pointdata.keys(): 
    print("array:", k, lines.pointdata[k].dtype)
lines.cmap("rainbow", "cluster_idx")
show(lines, axes=1, bg='blackboard')

Works like a charm. Thanks @marcomusy.

1 Like