gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:18:01 28 4
gpt4 key购买 nike

我正在尝试使用 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

有人能告诉我我做错了什么吗?谢谢!

最佳答案

您的问题是 .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)

关于python - Paraview - 使用 python 脚本以 x3d 格式导出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55528157/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com