Use the output of the calculator filter in pvpython

Hi All,
I have been trying to use the output of the Python Calculator for the clip value within pvpython. I would like it to be a variable that can be added to the script, so when I change the formula, the new value will be automatically calculated when the script is run. For example, here is a simple pvpython script I have been working on. Notice the clip1.value is hardcoded in. I would like this value to come from the result of the python calculator. This could also be derived form the python annotation as well.

from paraview.simple import *

disk_out_refexo = IOSSReader(registrationName='disk_out_ref.exo', FileName=['/.../disk_out_ref/disk_out_ref.exo'])

renderView1 = GetActiveViewOrCreate('RenderView')

disk_out_refexoDisplay = Show(disk_out_refexo, renderView1, 'UnstructuredGridRepresentation')

renderView1.ResetCamera(False)

renderView1.CameraPosition = [0.0, 50.0, 0.0]
renderView1.CameraViewUp = [0.0, 0.0, 1.0]

SetActiveSource(disk_out_refexo)

pythonCalculator1 = PythonCalculator(registrationName='PythonCalculator1', Input=disk_out_refexo)

pythonCalculator1.Expression = 'mean(Temp)'
pythonCalculator1.ArrayName = 'mean_temp'

pythonCalculator1Display = GetDisplayProperties(pythonCalculator1, view=renderView1)

ColorBy(pythonCalculator1Display, ('POINTS', 'Temp'))

pythonCalculator1Display.RescaleTransferFunctionToDataRange(True, False)

pythonCalculator1Display.SetScalarBarVisibility(renderView1, True)

pythonAnnotation1 = PythonAnnotation(registrationName='PythonAnnotation1', Input=disk_out_refexo)

pythonAnnotation1.ArrayAssociation = 'Point Data'
pythonAnnotation1.Expression = 'mean(Temp)'

pythonAnnotation1Display = Show(pythonAnnotation1, renderView1, 'TextSourceRepresentation')

SetActiveSource(pythonAnnotation1)

clip1 = Clip(registrationName='Clip1', Input=pythonCalculator1)

clip1.ClipType = 'Scalar'
clip1.Scalars = ['POINTS', 'Temp']
**clip1.Value = 425**
clip1.Invert = 0

clip1Display = Show(clip1, renderView1, 'UnstructuredGridRepresentation')

clip1Display.Representation = 'Surface'

Hide(disk_out_refexo)
Hide(pythonCalculator1)

Hi,

In such cases, servermanager.Fetch is available. For example, it could be as follows:

calc_vtk = servermanager.Fetch(pythonCalculator1)
mean_temp = calc_vtk.GetPointData().GetArray('mean_temp').GetTuple1(0)

clip1 = Clip(registrationName=‘Clip1’, Input=pythonCalculator1)

clip1.ClipType = ‘Scalar’
clip1.Scalars = [‘POINTS’, ‘Temp’]
clip1.Value = mean_temp
clip1.Invert = 0
1 Like

Phil,

There isn’t a direct way to plug a value computed by a Calculator into a property of a filter. Since you are scripting, it is a little bit easier. Python Annotation is actually nice here, because it produces what is expected to be a single string value.

First, you’ll need to update the pipeline up to PythonAnnotation1:

pythonAnnotation1.UpdatePipeline()

Then, you’ll need to fetch the data value from it:

stringValue = FetchData(pythonAnnotation1)[0].GetRowData().GetAbstractArray(0).GetValue(0)

Use GetRowData() because the dataset output of the Python Annotation filter is a vtkTable. Use GetAbstractArray() because a vtkStringArray is an abstract array and not a simple vtkDataArray. Use GetValue(0) to get the one string in the vtkStringArray.

You can convert the string value to float with

float(stringValue)

and plug the result into clip1.Value.

Hope that helps!

2 Likes

Hi Cory,
Yes this worked for builtin, but when using a remote server, it crashes ParaView. Is there a way for us to get that value using remote server?

@cory.quammen

Thanks as always,
-Phil

Hmmm, FetchData should handle both built-in and remote servers. Testing locally, I that seems to be the case.

I suggest trying @Kenichiro-Yoshimi’s solution, which is a similar approach. Note that Fetch (different from the FetchData I mentioned) is defined in paraview.servermanager.Fetch.

Hi Cory… Yes that was may mistake your solution appears to work for both now. I just made a stupid mistake and now all works.

Thanks again!

@vgweirs Greg, I know you were having issues with fetches a while back. Sounds like it now works.
Alan