简体   繁体   English

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

[英]Paraview - Using python script to export data in x3d format

I am trying to export in x3d format OpenFOAM results using paraview-python script.我正在尝试使用 paraview-python 脚本以 x3d 格式导出 OpenFOAM 结果。 When I do it via paraview graphical interface it works and results can be visualized in Blender, see the following picture当我通过 paraview 图形界面进行操作时,它可以工作并且结果可以在 Blender 中可视化,请参见下图

在此处输入图片说明

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!正如您从这张图片中看到的那样,字段值(速度 U)不会被导出!

在此处输入图片说明

Can someone tell me what I am doing wrong?有人能告诉我我做错了什么吗? Thank you!谢谢!

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.您的问题是.foam文件它不是科学的可视化文件,因为 VTK, .foam文件仅用于 ParaView(通过其扩展名,而不是通过其内容)来识别阅读器OpenFOAMReader ,然后将其用于后期处理。

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.使用FoamToVTK将结果转换为 VTK 文件,然后循环遍历结果。

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)

You need to color your data in your script, with something like :您需要在脚本中为数据着色,例如:

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

Documentation 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM