5

I am trying to export in x3d format OpenFOAM results using paraview-python script. When I do it via paraview graphical interface it works and results can be visualized in Blender, see the following picture

enter image description here

However, when I try to do the same operation using the following script

from paraview.simple import *
import fnmatch
import os
import shutil

#create alist of all vtk files
vtkFiles = []
for root, dirnames, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, '*.vtk'):
        vtkFiles.append(os.path.join(root, filename))


vtkFilesGroups=[
    'U',
]


def ResetSession():
    pxm = servermanager.ProxyManager()
    pxm.UnRegisterProxies()
    del pxm
    Disconnect()
    Connect()


def x3dExport(output,r):
    #export in x3d format

    exporters = servermanager.createModule("exporters")
    Show(r)
    view = GetActiveView()
    render = Render()
    x3dExporter = exporters.X3DExporter(FileName=output)
    x3dExporter.SetView(view)
    x3dExporter.Write()
    ResetSession()

# group VTK files by gruop (fields in openfoam "vtkFilesGroups")
# then loop over all and save it into different formats
groupedVtkFiles=[]
for group in vtkFilesGroups:

    vtkDir = os.path.join('.', group, 'vtk')
    if not os.path.exists(vtkDir):
        os.makedirs(vtkDir)

    vtuDir = os.path.join('.', group, 'vtu')
    if not os.path.exists(vtuDir):
        os.makedirs(vtuDir)

    x3dDir = os.path.join('.', group, 'x3d')
    if not os.path.exists(x3dDir):
        os.makedirs(x3dDir)



    for stepFile in vtkFiles:
        tmp = stepFile.split(os.sep)
        oldFileName = tmp[-1].split('.')[0]
        time = tmp[-2]
        fileNameVtk = '{}_{}.vtk'.format(oldFileName, time)
        fileNameVtp = '{}_{}.vtp'.format(oldFileName, time)
        fileNameX3d = '{}_{}.x3d'.format(oldFileName, time)

        r = LegacyVTKReader(FileNames=[stepFile])
        w = XMLUnstructuredGridWriter()
        w.FileName = os.path.join(vtuDir, fileNameVtp)
        w.UpdatePipeline()

        x3dExport(os.path.join(x3dDir, fileNameX3d), r)

the field values (velocity U) are not exported as you can see from this picture!

enter image description here

Can someone tell me what I am doing wrong? Thank you!

Sim81
  • 1,574
  • 2
  • 9
  • 15

2 Answers2

3

Your problem is that the .foam file it's not a scientific visualization file, as VTK, .foam file is only used for ParaView (by its extension, not by its content) to identify the reader OpenFOAMReader and then us it for post-processing.

I have two solutions for you:

  1. Read the reader documentation to find a way to do this.
  2. Convert the results into VTK files with FoamToVTK and then loop over the results.

EDIT

I Use this code to transform do that thing long time ago:

from paraview.simple import *
import fnmatch
import os
import shutil

#create alist of all vtk files
vtkFiles = []
for root, dirnames, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, '*.vtk'):
        vtkFiles.append(os.path.join(root, filename))


vtkFilesGroups=('p', 'U')


def ResetSession():
    pxm = servermanager.ProxyManager()
    pxm.UnRegisterProxies()
    del pxm
    Disconnect()
    Connect()


def x3dExport(output,r):
    #export in x3d format

    exporters = servermanager.createModule("exporters")
    Show(r)
    view = GetActiveView()
    render = Render()
    x3dExporter = exporters.X3DExporter(FileName=output)
    x3dExporter.SetView(view)
    x3dExporter.Write()
    ResetSession()

# group VTK files by gruop (fields in openfoam "vtkFilesGroups")
# then loop over all and save it into different formats
for group in vtkFilesGroups:
    x3dDir = os.path.join('.', group, 'x3d')
    if not os.path.exists(x3dDir):
        os.makedirs(x3dDir)

    for stepFile in (f for f in vtkFiles if group in f):
        tmp = stepFile.split(os.sep)
        oldFileName = tmp[-1].split('.')[0]
        time = tmp[-2]
        fileNameX3d = '{}_{}.x3d'.format(oldFileName, time)

        x3dExport(os.path.join(x3dDir, fileNameX3d), r)
efirvida
  • 4,592
  • 3
  • 42
  • 68
  • Hi! Thank you very much for your answer. I think I am getting closer to the solution. How do I load a VTK from script ? – Sim81 May 06 '19 at 21:26
  • 1
    @Sim81, I edit my answer, if you only need x3d format you only need the x3dExport function inside the loop. and remove all the pvdFile's lines – efirvida May 06 '19 at 21:39
  • OK - I will try now and let you know how things go, thank you ! – Sim81 May 06 '19 at 22:03
  • 1
    maybe you need to edit some more things because I use it to process vtk fields created with runtime post-processing surface function, take a look to the propeller example, but the idea is the same – efirvida May 06 '19 at 22:07
  • Unfortunately I have the same problem. It exports the grid but not the contour field. So I have the same situation as the second picture show! How do you run that script? do you use pvpython script.py command line? – Sim81 May 06 '19 at 23:33
  • I have edited my question with the script you suggested. I set vtkFilesGroups=[ 'U', ] as I would like to extract 'U'. I also modified the loop for stepFile in vtkFiles. Please let me know what you think. Thank you! – Sim81 May 06 '19 at 23:42
  • 1
    @Sim81, looks good to me, and if it work, then its good.haha. but if you only need the x3d file you can remove "fileNameVtk", "fileNameVtp", "r","w" – efirvida May 07 '19 at 08:32
1

You need to color your data in your script, with something like :

ColorBy(yourRep, ('POINTS', ('YourScalar', 'YourComp'))

Documentation

Mathieu Westphal
  • 2,544
  • 1
  • 19
  • 33
  • Thank you for your answer. Could you please provide a link to some documentation/example for this function? Thank you ! – Sim81 Apr 15 '20 at 04:54