can't view the new data array in Paraview

Hi,

very new here…I’m trying to modify a data array from a vtk file. I’m using the python shell (v3.14) …everything looks ok (no errors) but I can’t see my new array in the Data Arrays table widget. I can only see the original one. My code is (very basic):
from paraview.simple import *
import paraview.vtk as vtk
reader = OpenDataFile("/home/fabio/Current_density-tm-mag-xyz_slice.vtk")
rv = CreateRenderView()
rep = servermanager.CreateRepresentation(reader, rv)
rep.Representation = “Wireframe”
rv.StillRender()
UpdatePipeline()
data = servermanager.Fetch(reader)
numPoints = data.GetNumberOfPoints()
print "Number Of Points: ", numPoints
numCells = data.GetNumberOfCells()
print “Number Of Cells: “, numCells
filtered_cd = vtk.vtkDoubleArray()
filtered_cd.SetName(“FilteredCD”)
filtered_cd.SetNumberOfComponents(1)
for i in xrange(numPoints):
p = data.GetPoint(i)
cd = data.GetPointData().GetArray(‘scalars’).GetValue(i)
print(”(” + repr(p[0]) + ", " + repr(p[1]) + "): " + repr(cd))
filtered_cd.InsertNextValue(cd)
if i == 10:
break
data.GetPointData().AddArray(filtered_cd)
UpdatePipeline()

any suggestion to what I’m doing wrong?

Thanks in advance
Fabio

Your question touches on probably the most confusing aspect of Python scripting in ParaView. There are effectively two Python environments to know about. One, defined in paraview.simple, enables manipulation of ParaView’s state. The other, defined in paraview.vtk, operates at the VTK layer, which is ignorant of the higher level defined in `paraview.simple.

In your example, you are bridging the two in a partially valid way. The servermanager.Fetch function brings the VTK data hidden below the ParaView layer out to where you can read it and manipulate it with paraview.vtk. Note, however, that Fetch is making a copy of the data. Hence, when you add an array to it, you are in fact changing the copy and not the original data. That’s why you don’t see the new array in the table widget. Why is it copying the data? Because ParaView is at heart a client/server application where the data resides on the server and a special function is needed to get it to the client where your Python interpreter is running. That’s what Fetch does. There is no equivalent function to push manipulated VTK data to the server.

Instead, what you need to do is have the VTK data object manipulation occur in the VTK layer controlled by ParaView by way of a ParaView filter. There are a couple ways to do it, but for this example where you are copying a scalar array the easiest way would be to use the Calculator filter, like this.

from paraview.simple import *
reader = OpenDataFile("/home/fabio/Current_density-tm-mag-xyz_slice.vtk")
rv = CreateRenderView()
rep = servermanager.CreateRepresentation(reader, rv)
rep.Representation = “Wireframe”
rv.StillRender()
UpdatePipeline()
calc = Calculator()
calc.AttributeType = 'Point Data'
calc.ResultArrayType = 'Double'
calc.ResultArrayName = "FilteredCD"
calc.Function = "inputArrayName"
UpdatePipeline()

Here, calc.Function is the string expression evaluated to produce the new data array.

Cory

Thanks a lot! Reading the documentation I was able to find out that Fetch would do a copy but I thought there would be a “similar” command to send back to the server what I modified/added.

Anyway, your code does the trick! I also (by chance…just after I posted the question, I was just adding keywords to my google search) stumbled on a code snippet that was using a ProgrammabeFilter command as well. I couldn’t see any example
in the documentation online on how to use that, but it looks like a bridge between the python shell and the scripting window in Paraview?

Thanks again for the help!

Fabio

Indeed, data flows one way in ParaView for the most part: from server to client.

No exactly. The Programmable Filter, described in Chapter 12 of the ParaView Guide. An excerpt:

In this guide so far, we have been looking at examples of Python scripts for pvpython. These scripts are used to script the actions you would perform using the paraview UI. The scripts you would write for Programmable Source and Programmable Filter are entirely different. The data processing API executes within the data processing pipeline and, thus, has access to the data being processed. In client-server mode, this means that such scripts are indeed executed on the server side, potentially in parallel, across several MPI ranks. Therefore, attempting to import the paraview.simple Python module in the Programmable Source script, for example, is not supported and will have unexpected consequences.

I suggest taking a look at that chapter if you want to learn more about how to use the Programmable Filter.

Ok! Thanks again for your help!

Fabio

1 Like