Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paraview - Using python script to export data in x3d format

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!

like image 634
Sim81 Avatar asked Apr 05 '19 04:04

Sim81


2 Answers

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)
like image 163
efirvida Avatar answered Oct 18 '22 18:10

efirvida


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

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

Documentation

like image 1
Mathieu Westphal Avatar answered Oct 18 '22 18:10

Mathieu Westphal