Reveal underlying codes when executing commands in the paraView GUI

This is probably a noob question, but not unlikely from a inexperienced python/ParaView/C++ programmer & user of ParaView –

I would like to automate some stuffs (python or C++ scripting?) but dunno how to start since I’m new to scripting. Take the example case:

  1. Need to run my custom programmable filter to generate a spreadsheet with data, &
  2. Export do disk, such .csvs at each timepoint automatically.

I am able to work though the first goal, with this link – https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html#adding-a-new-point-cell-data-array-based-on-an-input-array

Content of my Programmable filter:

input0=inputs[0]

#Calcualtion
volCell=volume(input0)
#volCellP=input0.CellData["volCell"]*volFracP
volFracP=input0.CellData["kinematicCloudTheta"]*1000
volCellP=volCell*volFracP
vol1P=4/3*22/7*((5e-6)**3)
numPCell=volCellP/vol1P

# add to output
output.CellData.append(volFracP, 'volFracP')
output.CellData.append(volCellP, 'volCellP')
output.CellData.append(numPCell, 'numPCell')

For the second goal, I went to: https://www.paraview.org/Wiki/ParaView/Users_Guide/List_of_writers#CSVWriter, but not sure where to add this & how to execute. I was wondering, I might be able to follow the underlying codes that are running if I can somehow make ParaView show the commands when e.g. I click on the export button on the generated spreadsheet & then adapt into a script. In longterm, that can be useful in learning scripting while working with different modules.

Anyone has any suggestion?

You are looking for python scripting. You can generate your python script by using Tools → Start Trace.

1 Like

Thanks! My bad, found that’s mentioned here: https://docs.paraview.org/en/v5.8/UsersGuide/introduction.html?highlight=pvpython#tracing-actions-for-scripting.

Oh, but this ain’t simple copy-paste, as it results in error:

>>> # get animation scene
animationScene1 = GetAnimationScene()
# Properties modified on animationScene1
animationScene1.AnimationTime = 1.0
# get the time-keeper
timeKeeper1 = GetTimeKeeper()
# export view
ExportView('/Users/massisenergy/Downloads/22GBQC_particleFoam_mT250duration_pPS2500_T10sec.csv', view=spreadSheetView1, RealNumberNotation='Mixed',
RealNumberPrecision=8)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'spreadSheetView1' is not defined
>>> 

well,

spreadSheetView1=GetActiveView

should fix it

1 Like

Solved, thanks to @mwestphal, and @mohammad_faghih for this post discussion: https://discourse.paraview.org/t/writing-a-pyhon-script-to-semi-automate-generation-of-csv-file-for-streamlines/6827.

For future reference, I am already using paraView in remoteServer-localClient mode, there are two ways to do it:

  1. Exported into local client disk:

Use GUI menu option view > Python Shell, run the code below:

>>> # get animation scene
animationScene1 = GetAnimationScene()

# Properties modified on animationScene1
animationScene1.AnimationTime = 9.0

# get the time-keeper and the active view
timeKeeper1 = GetTimeKeeper()
spreadSheetView1=GetActiveView()
# export view
ExportView('/Users/massisenergy/Downloads/22GBQC_particleFoam_mT250duration_pPS2500_T10sec.csv', view=spreadSheetView1, RealNumberNotation='Mixed',
    RealNumberPrecision=8)
>>> # get animation scene
animationScene1 = GetAnimationScene()
# Properties modified on animationScene1
animationScene1.AnimationTime = 9.0
# get the time-keeper 
timeKeeper1 = GetTimeKeeper()
# get the active view
spreadSheetView1=GetActiveView()
# export view
ExportView('/Users/massisenergy/Downloads/22GBQC_particleFoam_mT250duration_pPS2500_T9sec.csv', view=spreadSheetView1, RealNumberNotation='Mixed',
    RealNumberPrecision=8)

the resulting .csv file will be for single time step and written in the specified local directory.

  1. Exported into remote server disk:
  • this can be revealed done via the GUI like this: Select the data in the GUI to be exported, in my case, ProgrammableFilter1 > File > Save Data (⌘S) > Choose csv > CSVWriter pops up.

  • the reproducible way is to use the Trace tool mentioned before, from where the code can be copy-pasted into the Python Shell & fine-tuned more:

# save data
SaveData('/home/massisenergy/autoMount/Data_Documents/Dox_ownCloud/openFoamDataSR/E3DBP_Nozzle/ResTimeDistribution/22G_blunted/22GBQuarterCyl_simpleFoam/22GBQC_particleFoam/VTK_22GBQC_particleFoam_mT250duration_pPS2500/22GBQC_particleFoam_mT250duration_pPS2500.csv', proxy=programmableFilter1, WriteTimeSteps=1,
    Filenamesuffix='_%d',
    ChooseArraysToWrite=0,
    PointDataArrays=[],
    CellDataArrays=['U', 'cellID', 'kinematicCloudTheta', 'numPCell', 'shearStress', 'volCellP', 'volFracP'],
    FieldDataArrays=[],
    VertexDataArrays=[],
    EdgeDataArrays=[],
    RowDataArrays=[],
    Precision=5,
    UseScientificNotation=1,
    FieldAssociation='Cell Data',
    AddMetaData=1,
    AddTime=1)

Documentation for saving data is here: https://docs.paraview.org/en/v5.8/UsersGuide/savingResults.html#saving-datasets
This saves my fingers from 3or4 clicks each for 60 time-steps :joy:

The code with description can be found here: particleFraction_numPCell_sSMag_UMag.ProgrammableFilter_script

1 Like

For the people coming here in future, another handy tips from Jean Favre of CSCS:

  • File > Save State save as “.py”, not as “.pvsm”
  • Easily understand the code in human friendly python.