Paraview-使用python脚本以x3d格式导出数据

时间:2019-04-05 04:13:22

标签: python blender paraview x3d openfoam

我正在尝试使用paraview-python脚本以x3d格式导出OpenFOAM结果。当我通过paraview图形界面进行操作时,它可以工作并且可以在Blender中可视化结果,请参见下图

enter image description here

但是,当我尝试使用以下脚本执行相同的操作

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)

从图中可以看到,没有导出字段值(速度U)!

enter image description here

有人可以告诉我我做错了吗? 谢谢!

2 个答案:

答案 0 :(得分:3)

您的问题是 .foam 文件不是科学的可视化文件,因为VTK, .foam 文件仅用于ParaView(扩展名为,而不是其内容)以识别读者OpenFOAMReader,然后将其用于后期处理。

我为您提供两种解决方案:

  1. 阅读读者文档以找到实现此目的的方法。
  2. 使用FoamToVTK将结果转换为VTK文件,然后遍历结果。

编辑

我很久以前就使用此代码进行转换:

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)

答案 1 :(得分:1)

您需要使用以下命令为脚本中的数据着色:

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

Documentation