Auto-Load OpenFOAM files and State [SCRIPTS INCLUDED]

I need some help understanding where exactly my code logic goes wrong. If I run this script without the loop it loads fine, however with the loop it is saying it cannot find the file when it updates. I receive this error after running: vtkSMLoadStateOptionsProxy (000001F39377AF70): Error parsing state file XML from C:\Users\Christian\Desktop\ProgamsForSamik\Dakota\design.1\Runner\State.pvsm: File was not found.

It works fine without the loop in my script, but not with the loop. {} updates the number I have design.1, design.2, …etc. Same geometry and each one has a finer mesh to compare isosurfaces. I therefore keep using the same state file, but I have it set to only use the available files in the directory it updated to. Any help would be appreciated.

from paraview.simple import *
import os
for dnum in range(1,2):                          # dnum means design number
    paraview.simple._DisableFirstRenderCameraReset()
    renderView1 = GetActiveViewOrCreate('RenderView')
    Delete(renderView1)
    del renderView1    
    LoadState('C:\Users\Christian\Desktop\ProgamsForSamik\Dakota\design.1\Runner\State.pvsm', 
        LoadStateDataFileOptions='Search files under specified directory',
        DataDirectory='C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design. 
        {}/Runner'.format(dnum),
        OnlyUseFilesInDataDirectory=1,
        RunnerfoamFileName='C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design. 
        {}/Runner/Runner.foam'.format(dnum))
    renderView2 = FindViewOrCreate('RenderView2', viewtype='RenderView')
    SetActiveView(renderView2)
    a4Isosurface = FindSource('4. Isosurface')
    SetActiveSource(a4Isosurface)
    SaveScreenshot('C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design. 
          {}/Runner/iso.tiff'.format(dnum), renderView2, ImageResolution=[1600, 1600])

As opposed to this working script in a different folder:

from paraview.simple import *

paraview.simple._DisableFirstRenderCameraReset()
renderView1 = GetActiveViewOrCreate('RenderView')
Delete(renderView1)
del renderView1
LoadState('C:\Users\Christian\Desktop\ProgramsForSamik\Runner\State.pvsm', LoadStateDataFileOptions='Search files under specified directory',
    DataDirectory='C:/Users/Christian/Desktop/ProgramsForSamik/Runner',
    OnlyUseFilesInDataDirectory=1,
    RunnerfoamFileName='C:/Users/Christian/Desktop/ProgramsForSamik/Runner/Runner.foam')
renderView2 = FindViewOrCreate('RenderView2', viewtype='RenderView')
SetActiveView(renderView2)
a4Isosurface = FindSource('4. Isosurface')
SetActiveSource(a4Isosurface)
SaveScreenshot('C:/Users/Christian/Desktop/ProgramsForSamik/Runner/iso.tiff', renderView2, ImageResolution=[1600, 1600])

What about Reseting the session at the end of your loop ?

Mathieu thanks for the tip I ended up thinking maybe instead of ResetSession() to check the inputs command. Seeing the different input options via a screen-trace, it led me to changing the input commands from “search files under specific dir” to “choose file names”. Using this option and slight mods, my script is automatically outputting all screenshots/exe all commands. I believe it has to do with the input format option although I am just inferencing. Here is the code to help future guys out. Make sure to create a state file with all your layouts, mock-run your screenshots for screen-trace python script and add the loop with {} on folder number and use ResetSession() commands. Best of luck and thanks again Mathieu.

from paraview.simple import *

for dnum in range(1,26):

paraview.simple._DisableFirstRenderCameraReset()
# get active view
renderView1 = GetActiveViewOrCreate('RenderView')
Delete(renderView1)
del renderView1
# Test Funtions
# path = 'C:/Users/Christian/Desktop/ProgramsForSamik/Dakota'
# subd = 'C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design.{}/Runner'.format(dnum)
# stated = 'C:\\Users\\Christian\\Desktop\\ProgamsForSamik\\Dakota\\design.{}\\Runner\\State.pvsm'.format(dnum)    
LoadState('C:\Users\Christian\Desktop\ProgramsForSamik\Dakota\design.1\Runner\State.pvsm', LoadStateDataFileOptions='Choose File Names',
    DataDirectory='C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design.{}/Runner'.format(dnum),
    RunnerfoamFileName='C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design.{}/Runner/Runner.foam'.format(dnum))
SetActiveView(None)
#CreateLayout('Layout #1')
# get layout
# place view in the layout
renderView2 = FindViewOrCreate('RenderView2', viewtype='RenderView')
#layout1 = GetLayout()
#layout1.AssignView(0, renderView2)    
SetActiveView(renderView2)
a4Isosurface = FindSource('4. Isosurface')
SetActiveSource(a4Isosurface)
# save screenshot
SaveScreenshot('C:/Users/Christian/Desktop/ProgramsForSamik/Dakota/design.{}/Runner/iso.tiff'.format(dnum), renderView2, ImageResolution=[1600, 1600])
ResetSession()
1 Like