# Reading a VTR file with Python using vtkXMLRectilinearGridReader()

**URL:** https://discourse.vtk.org/t/reading-a-vtr-file-with-python-using-vtkxmlrectilineargridreader/1846
**Category:** Support
**Created:** [October 1, 2019, 3:17pm UTC](https://discourse.vtk.org/t/reading-a-vtr-file-with-python-using-vtkxmlrectilineargridreader/1846 "2019-10-01T15:17:06Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![swebste9](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@swebste9](https://discourse.vtk.org/u/swebste9)
#### Post date: [October 1, 2019, 3:17pm UTC](https://discourse.vtk.org/t/reading-a-vtr-file-with-python-using-vtkxmlrectilineargridreader/1846/1 "2019-10-01T15:17:06Z")

</div>

I am a new user of VTR files and have inherited some legacy code that intends to read in a VTR file with Python using the vtkXMLRectilinearGridReader(). I am having difficulties getting this to work and am hoping someone may have a suggestion for me. My overall goal is to be able to access the underlying data stored in the VTR file (i.e. see the quantity of data present as well as specific data values).

The first part of the code I am using is below:

#=======================================================================  
from sys import argv  
import numpy as np  
import matplotlib as mpl  
import matplotlib.pyplot as plt  
from mpl\_toolkits.mplot3d import Axes3D  
fig = plt.figure()  
ax = fig.add\_subplot(111, projection=‘3d’)  
import sys  
sys.path.append(’/usr/lib/python2.7/dist-packages’)  
import vtk  
#===========================================================  
#set input variables  
#===========================================================

infile= ‘filename.vtr’

outfile=infile.split(’/’)[-1]  
outfile=outfile.split(’.’)[0]+‘mor.’+outfile.split(’.’)[1]

kerthreshold=250.  
strucsize=10  
connectval=50.  
constructsize=10

#===========================================================  
#loading data  
#===========================================================  
reader=vtk.vtkXMLRectilinearGridReader()  
reader.SetFileName(infile)  
reader.Update()

#===========================================================  
#get information from vtk file  
#===========================================================  
datainfo=reader.GetOutput()  
pointdata=datainfo.GetPointData()  
extents=[]  
extents=datainfo.GetExtent()  
num=datainfo.GetNumberOfPoints()

oricube=[]

print (“Read datacube…”)  
for pid in range(0,num):  
value=pointdata.GetScalars().GetValue(pid)  
oricube.append(value)  
#=======================================================================

The code processes fine until it gets to the for loop at the end. At this point, I get an error stating AttributeError: ‘NoneType’ object has no attribute ‘GetValue’. Apparently, the reader.GetOutput().GetPointData().GetScalars() is not recognized as an actual object, and therefore the GetValue() method is not recognized as a method.

It’s possible that there is a better way to read in and access the VTR file data that the code I am currently working with, but for now I am limited to working with the code that I have. If anyone has any suggestions for me, please let me know! Thank you

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [October 3, 2019, 6:11pm UTC](https://discourse.vtk.org/t/reading-a-vtr-file-with-python-using-vtkxmlrectilineargridreader/1846/2 "2019-10-03T18:11:27Z")

</div>

You might benefit from using [PyVista](https://docs.pyvista.org) - PyVista simplifies routines like this so that you don’t have to worry about properly setting up the reader pipeline and PyVista will handle the creation of NumPy arrays from the VTR dataset for you.

Try this:

```python
...

import pyvista as pv
# Read the file
datainfo = pv.read(infile)
extents = datainfo.extent
# Get the data array you fetch
oricube = datainfo.active_scalar

...

```
