user32882
user32882

Reputation: 5877

Using vtk library in python to extract vtk data as array

I would like to use the vtk library in python 2.7 to extract data from vtk unstructured grid files and convert this data to numpy or python list format. The vtk file is structured as follows:

ASCII 
DATASET UNSTRUCTURED_GRID 
POINTS       96 float 
CELLS       96     192 
CELL_TYPES       96 
POINT_DATA       96 
VECTORS vector1 float 
VECTORS vector2 float 
etc 

Since the vtk library documentation does not appear to do justice to its functionalities, I did a bit of tinkering myself:

import vtk
Filename = 'test.vtk'
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(FileName)
reader.Update()

Then what? Doing a dir on the reader variables shows a number of different methods/attributes, for instance GetNumberOfVectorsInFile gets the number of vector layers contained in the ASCII file, GetVectorsNameInFile(5) outputs exactly the name of the 5th array of vector information contained within the file. How do I get a list of the actual values in the file?

Upvotes: 1

Views: 2767

Answers (1)

user6764549
user6764549

Reputation:

The names of VTK functions in Python are analogous to the C++ ones. So you can use the functions given in the C++ Documentation for vtkUnstructuredGridReader

import vtk
Filename = 'test.vtk'
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(FileName)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
usg = reader.GetOutput()

vec1 = usg.GetPointData().GetVector('vector1')

Upvotes: 1

Related Questions