Using WarpbyVector in python script

Hello everyone,
I wanted to implement a python script, which will deform a “.vtu” mesh by a vector called “Displacement” that I saved from another “.vtu” and stored as an array of [3*number of points]. The two meshes have the same number of nodes. Then save the new deformed mesh (preserving the existing other data contained in it).
I haven’t figured it out completely yet…
What am i missing?

import vtk
from vtk.numpy_interface import dataset_adapter as dsa
reader2 = vtk.vtkXMLUnstructuredGridReader()
reader2.SetFileName(fname)
reader2.Update()
output = reader2.GetOutput()

MS_D=dsa.WrapDataObject(output)
MS_D.PointData.append(Displacement,“Disp”)

warp = vtk.vtkWarpVector()
warp.SetInputConnection(reader2.GetOutputPort())
#warp.SetInputData(MS_D.GetPointData().SetActiveVectors(‘Disp’))
warp.SetInputArrayToProcess(???)
warp.SetScaleFactor(1)
warp.Update()
outputw = warp.GetUnstructuredGridOutput()

writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetFileName(“Disp_def_” + fname)
writer.SetInputData(outputw.VTKObject)
writer.Write()

I’m a newbie here, so I’m sorry if it’s a dumb question… :smiley:

I suggest you use paraview Tools—>Start Trace to do the job.

image

I already tried but the syntax is slightly different and doesn’t really work in my python script…

ParaView Python tracing will produce Python scripts that operate at the ParaView application level, hence the difference in syntax. You can use that do reach your objective, but writing VTK code as you have should also work fine if you run it via pvpython.

Your code looks reasonable, but the part you are missing seems to me to be:

warp.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, 'Disp')

Try that and see if your code works.

You can also streamline the code to

import vtk
reader2 = vtk.vtkXMLUnstructuredGridReader()
reader2.SetFileName(fname)

warp = vtk.vtkWarpVector()
warp.SetInputConnection(reader2.GetOutputPort())
warp.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, 'Disp')
warp.SetScaleFactor(1)

writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetFileName(“Disp_def_” + fname)
writer.SetInputConnection(warp.GetOutputPort())
writer.Write()
1 Like

Thank you so much! it works! :smiley:
I was using “SetInputData” without “SetInputArrayToProcess”

This gives me an empty (deformed) grid (.vtu) - how can I preserve the previous data arrays contained in the undeformed vtu mesh file? (if it’s possible)
Otherwise, I will use the “append” command and re-assign all the variables to the new points of the grid.

That’s odd - the input data arrays should be preserved in the output without you having to do anything.

Ok yes, i got it, my bad! I was inspecting the wrong file

Thank you for the help! :slight_smile: :slight_smile: