# Python: Reading, Calculate, Write Out

**URL:** https://discourse.vtk.org/t/python-reading-calculate-write-out/1138
**Category:** Support
**Created:** [June 14, 2019, 12:00pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138 "2019-06-14T12:00:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Horus](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/h/ce73a5/32.png) [@Horus](https://discourse.vtk.org/u/Horus)
#### Post date: [June 14, 2019, 12:00pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/1 "2019-06-14T12:00:42Z")

</div>

Hello,

I want to read in a VTK file, evaluate some function on it and write the file back, including the function result. All that, using the Python VTK interface.

So far I got:

```
from vtk import *

reader = vtkDataSetReader()
reader.SetFileName("bunny-0.1.vtk")
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()

data = reader.GetOutput()

calc = vtkArrayCalculator()
calc.SetInputData(data)

calc.SetFunction("5")
calc.SetResultArrayName("MyResults")
calc.Update()

# Gives: AttributeError: 'NoneType' object has no attribute 'GetPointData'
# print(calc.GetPolyDataOutput().GetPointData().GetArray("MyResults").getValue(10))

writer = vtkUnstructuredGridWriter()
writer.SetInputData(data)
writer.SetFileName("Output.vtk")
writer.Write()

```

It works so far, that it writes out the same geometry to Output.vtk, but without the additional field (here, constant 5).

What is the missing piece?

Thanks!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 14, 2019, 12:35pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/2 "2019-06-14T12:35:05Z")

</div>

The problem is that you write the original input to file and not the filter output.

---

<div class="post-metadata">

### Author: ![Horus](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/h/ce73a5/32.png) [@Horus](https://discourse.vtk.org/u/Horus)
#### Post date: [June 14, 2019, 2:08pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/3 "2019-06-14T14:08:24Z")

</div>

Ok, thanks!

`writer.SetInputData(calc.GetOutput())`

did the trick.

Another follow up question:

when using the calculator expression: `calc.SetFunction("coordsX+coordsY+coordsZ")`, which works in interactive Paraview I get:

```auto
ERROR: In /build/paraview/src/ParaView-v5.6.0/VTK/Common/Misc/vtkFunctionParser.cxx, line 1244
vtkFunctionParser (0x55eb730b46f0): Syntax error: expecting a variable name; see position 0

```

Thanks!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 14, 2019, 2:20pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/4 "2019-06-14T14:20:47Z")

</div>

Check documentation of vtkArrayCalculator. If “follow-up” question is unrelated to the original question (as it was in this case) then please post it as a new topic.

---

<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: [June 14, 2019, 7:11pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/5 "2019-06-14T19:11:46Z")

</div>

Hi @Horus, [PyVista](http://docs.pyvista.org) might be a tool of interest for you. PyVista provides a Pythonic interface to VTK - so you can focus on what’s important; the task at hand, not the nuances of setting up the VTK pipeline. In PyVista, your task would be:

```python
import pyvista as pv
import numpy as np

mesh = pv.read('bunny-0.1.vtk')
mesh['new array'] = np.full(mesh.n_points, 5)
mesh.save('output.vtk')

```

Three tasks, three lines of code 😉 .

If you want to get fancier with summing the coordinates like you tried above, you could try:

```python
mesh['new array'] = np.sum(mesh.points, axis=1)

```

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [June 14, 2019, 8:08pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/6 "2019-06-14T20:08:55Z")

</div>

@Horus

ParaView uses a subclass of `vtkArrayCalculator` to set up some variables to make them available in expressions, including `coordsX`, `coordsY`, and `coordsZ`. To do the same in your VTK program, you can add

```auto
calc.AddCoordinateScalarVariable("coordsX", 0)
calc.AddCoordinateScalarVariable("coordsY", 1)
calc.AddCoordinateScalarVariable("coordsZ", 2)

```

That should do the trick to make your expression work.

As a side note, if you are familiar with NumYy, you may be interested in VTK’s NumPy interface. An excellent tutorial on this interface starts with this blog post by Berk Geveci: [Improved VTK – numpy integration](https://blog.kitware.com/improved-vtk-numpy-integration/).

---

<div class="post-metadata">

### Author: ![garcfd](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@garcfd](https://discourse.vtk.org/u/garcfd)
#### Post date: [December 12, 2022, 5:57pm UTC](https://discourse.vtk.org/t/python-reading-calculate-write-out/1138/7 "2022-12-12T17:57:25Z")

</div>

thanks Bane, thats a really useful example.

as a pyVista “newbie”…  
how would I do the exact same, import vtk, calc function, and export vtk  
but I want a new function vnew = mag(U) + 10  
where mag U is the velocity magnitude, say
