pythonvtkparaview

Programmable filter to add time dependent Cell/Point data to vtu/pvtu unstructured grid in paraview


I wrote a python programmable filter in paraview that, given an unstructured grid object (vtu/pvtu original format) and a paraview extraction of points/cells, modifies the value of the user-selected cell/point data in the selected points of the grid:

dof = "ax_bc"
value = 1.0

import vtk
import numpy as np
# Example usage
input0 = inputs[0]
input1 = inputs[1]

# Get the input data objects
if input0 is None or input1 is None:
    raise ValueError("One or both input VTK objects are missing.")

# Get the point data arrays
point_data0 = input0.GetPointData()
point_data1 = input1.GetPointData()

# Get the "array one" point data array in the first input
array_one = point_data0.GetArray(dof)
if array_one is None:
    raise ValueError("The first input VTK object does not have a " + dof + " array.")

# Get the vtkOriginalPointIds array from the second input
original_ids = point_data1.GetArray("vtkOriginalPointIds")
if original_ids is None:
    raise ValueError(
        "The second input VTK object does not have a 'vtkOriginalPointIds' array."
    )

# Convert VTK arrays to NumPy arrays
if np.any((original_ids < 0) | (original_ids >= input0.GetNumberOfPoints())):
    raise IndexError("One or more original IDs are out of bounds for the first input VTK object.")

# Assign values to the dofs based on the original ids
array_one[original_ids] = value

point_data0.append(array_one, dof)
point_data0.Modified()

Now I would like to use another programmable filter (or if not possible, a paraview shell script or python script) to add (set new cell/point data and modify) time-dependent cell/point data to my grid. I've found very poor documentation on how to use time in python/vtu unstructured grids and make it readable for paraview. For instance, I would want to add a point data array "ax_bc" as a time dependent array. Which way should I follow?

I've found some solutions involving multiblock datasets, but I've also read of subsequent problems when parallelization is involved (I will need to export to pvtu format).


Solution

  • In a programmable filter, you can get the current pipeline time and then set the data array accordingly.

    executive = self.GetExecutive()
    outInfo = executive.GetOutputInformation(0)
    return outInfo.Get(executive.UPDATE_TIME_STEP()) if outInfo.Has(executive.UPDATE_TIME_STEP()) else None
    

    If you want to create timesteps (i.e. your input is not temporal but the output should be), then you should add a RequestInformation script. In this script, set the TIME_STEPS() and TIME_RANGE() information key to provide to full time values list.

    See the example here: https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html#reading-a-csv-file-series