0

I have been using Paraview to visualize and analyse VTU files. I find the calculate gradient filter quite useful. I would like to know if there is a python API for Paraview which I can use to use this filter.

I'm looking for something like this.

import paraview as pv

MyFile = "Myfile0001.vtu"

Divergence = pv.filters.GradientOfUnstructuredDataset.(Myfile)

brownser
  • 545
  • 7
  • 25
  • For posterity, the discussion about it on the [ParaView forum](https://discourse.paraview.org/t/calculate-the-divergence-of-a-vector-field-using-paraview-filter/6617) – Nico Vuaille Mar 08 '21 at 07:26

1 Answers1

1

ParaView is fully scriptable in python. Each part of this doc has a 'do it in python' version.

Whereas API doc does not necessary exist, you can use the Python Trace (in Tool menu), that records action from the GUI and save it as a python script.

EDIT

To get back data as an array, it needs some additional steps as ParaView works on a client/server mode. You should Fetch the data and then you can manipulate the vtkObject, extract the array and convert it to numpy.

Something like

from paraview.simple import *
from vtk.numpy_interface import dataset_adapter as dsa

gridvtu = XMLUnstructuredGridReader(registrationName='grid', FileName=['grid.vtu'])
gradient = GradientOfUnstructuredDataSet(registrationName='Gradient', Input=gridvtu)
vtk_grid = servermanager.Fetch(gradient)
wraped_grid = dsa.WrapObject(vtk_grid)
divergence_array = wraped_grid.PointData["Divergence"]

Note that divergence_array is a numpy.ndarray

You also can write pure vtk code, as in this example on SO

Nico Vuaille
  • 2,310
  • 1
  • 6
  • 14
  • Hi, Thank you for your response. However, it does not answer my question. What I'm looking for is a way to calculate the divergence if I have a vector array. – brownser Mar 04 '21 at 14:01
  • I mean, to pass the VTU file and the field as an argument and get the divergence back as an array. – brownser Mar 04 '21 at 14:13
  • It was not clear that you want to manipulate the resulting array. I updated my answer – Nico Vuaille Mar 04 '21 at 15:54