Reading a .vtk file with python

Viewed 46799

I've been given a legacy format vtk file (I think its an unstructured grid) and I'd like to read it in with python and output a .npy file instead, since I know how to deal with that.

The file is a dump from ATHENA and so has density, velocity, magnetic field along with the coordinates.

I'm very much a procedural programmer, so all these objects are confusing...

6 Answers

Here's a short snippet for reading points from legacy VTK files:

import numpy as np
import vtk

filename = 'model.vtk'

reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(filename)
reader.Update()

points = np.array( reader.GetOutput().GetPoints().GetData() )

The variable points is an (N,2) or (N,3) array, where N is the number of points.

Related