使用python和paraview将三维数据可视化的方法

简介

paraview可以很好地实现三维数据的可视化,包括网格、温度场、流场等信息。但是,paraview的设置通常所手动的,对于需要将可视化自动化完成的任务则需要用到paraview的python脚本。事实上,paraview界面中的任何一项操作都可以在python中找到对应的代码,也就是说,原则上可以用一段python代码替换掉繁琐的鼠标点击设置操作

方法

参考自paraview手册

1 生成代码

我们可以使用Tools/Start Trace按钮开始录制宏(也就算python代码)。点击Tools/Start Trace按钮后的任何改动和设置都会被转换为python代码。如果希望结束录制,那么再点击Tools/Stop Trace按钮

以笔者为例,用paraview打开了一个vts三维数据文件,并显示在界面上

使用Start Trace生成的对应的python代码如下

python 复制代码
# trace generated using paraview version 5.6.0
#
# To ensure correct image size when batch processing, please search 
# for and uncomment the line `# renderView*.ViewSize = [*,*]`

#### import the simple module from the paraview
from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

# create a new 'XML Structured Grid Reader'
a10N5vts = XMLStructuredGridReader(FileName=['/home/myname/myfile.vts'])
a10N5vts.PointArrayStatus = ['u', 'define_marker']

# get active view
renderView1 = GetActiveViewOrCreate('RenderView')
# uncomment following to set a specific view size
# renderView1.ViewSize = [2374, 1278]

# show data in view
a10N5vtsDisplay = Show(a10N5vts, renderView1)

# trace defaults for the display properties.
a10N5vtsDisplay.Representation = 'Outline'
a10N5vtsDisplay.ColorArrayName = ['POINTS', '']
a10N5vtsDisplay.OSPRayScaleArray = 'u'
a10N5vtsDisplay.OSPRayScaleFunction = 'PiecewiseFunction'
a10N5vtsDisplay.SelectOrientationVectors = 'None'
a10N5vtsDisplay.ScaleFactor = 0.7462999820709229
a10N5vtsDisplay.SelectScaleArray = 'u'
a10N5vtsDisplay.GlyphType = 'Arrow'
a10N5vtsDisplay.GlyphTableIndexArray = 'u'
a10N5vtsDisplay.GaussianRadius = 0.037314999103546145
a10N5vtsDisplay.SetScaleArray = ['POINTS', 'u']
a10N5vtsDisplay.ScaleTransferFunction = 'PiecewiseFunction'
a10N5vtsDisplay.OpacityArray = ['POINTS', 'u']
a10N5vtsDisplay.OpacityTransferFunction = 'PiecewiseFunction'
a10N5vtsDisplay.DataAxesGrid = 'GridAxesRepresentation'
a10N5vtsDisplay.SelectionCellLabelFontFile = ''
a10N5vtsDisplay.SelectionPointLabelFontFile = ''
a10N5vtsDisplay.PolarAxes = 'PolarAxesRepresentation'
a10N5vtsDisplay.ScalarOpacityUnitDistance = 0.10074097267248679

# init the 'GridAxesRepresentation' selected for 'DataAxesGrid'
a10N5vtsDisplay.DataAxesGrid.XTitleFontFile = ''
a10N5vtsDisplay.DataAxesGrid.YTitleFontFile = ''
a10N5vtsDisplay.DataAxesGrid.ZTitleFontFile = ''
a10N5vtsDisplay.DataAxesGrid.XLabelFontFile = ''
a10N5vtsDisplay.DataAxesGrid.YLabelFontFile = ''
a10N5vtsDisplay.DataAxesGrid.ZLabelFontFile = ''

# init the 'PolarAxesRepresentation' selected for 'PolarAxes'
a10N5vtsDisplay.PolarAxes.PolarAxisTitleFontFile = ''
a10N5vtsDisplay.PolarAxes.PolarAxisLabelFontFile = ''
a10N5vtsDisplay.PolarAxes.LastRadialAxisTextFontFile = ''
a10N5vtsDisplay.PolarAxes.SecondaryRadialAxesTextFontFile = ''

# reset view to fit data
renderView1.ResetCamera()

# update the view to ensure updated data information
renderView1.Update()

# change representation type
a10N5vtsDisplay.SetRepresentationType('Surface')

# set scalar coloring
ColorBy(a10N5vtsDisplay, ('POINTS', 'u'))

# rescale color and/or opacity maps used to include current data range
a10N5vtsDisplay.RescaleTransferFunctionToDataRange(True, False)

# show color bar/color legend
a10N5vtsDisplay.SetScalarBarVisibility(renderView1, True)

# get color transfer function/color map for 'u'
uLUT = GetColorTransferFunction('u')

# get opacity transfer function/opacity map for 'u'
uPWF = GetOpacityTransferFunction('u')

# Apply a preset using its name. Note this may not work as expected when presets have duplicate names.
uLUT.ApplyPreset('jet', True)

#### saving camera placements for all active views

# current camera placement for renderView1
renderView1.CameraPosition = [-3.0002326812841265, 1.6327404724026686, 11.05952175767382]
renderView1.CameraFocalPoint = [1.0588007399094939, 0.19170381026056926, 4.052799553165938]
renderView1.CameraViewUp = [0.04393119170428755, 0.9832704546428532, -0.17677461192682026]
renderView1.CameraParallelScale = 3.7711602821057224

#### uncomment the following to render all views
# RenderAllViews()
# alternatively, if you want to write images, you can use SaveScreenshot(...).

有了python代码之后,我们可以加入自己希望自定义的操作

2 运行python代码

建议使用paraview提供的pvpython解释器完成对代码的运行,比如

bash 复制代码
pvpython mycode.py

这样运行可能会导致视窗一晃而过,可以在python代码中加入下面一行实现视窗的停留

python 复制代码
Interact()

还可以保存截图

python 复制代码
SaveScreenshot(working_dir+"/"+vts_file_name+"_SR.png", renderView1, ImageResolution=[2000, 2000],
        TransparentBackground=1)

3 paraview宏按钮

除了使用pvpython解释器运行代码,还可以使用宏。打开paraview的界面,点击Macros/Add new macro,添加python代码文件

这时工具栏会多出一个按钮。我们只需要按下按钮便会执行一边代码,这样即可实现paraview的二次开发

相关推荐
格林威25 分钟前
工业相机Chunk功能全解析:图像嵌入时间戳、编码器元数据(附堡盟C#代码)
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机
知识分享小能手40 分钟前
统计学学习教程,从入门到精通,参数估计 — 知识点详解与公式推导(10)
学习·机器学习·概率论
有Li43 分钟前
EvoMDT:用于多癌种结构化临床决策的自进化多智能体系统文献速递/医学智能体前沿
人工智能·学习·分类·文献·医学生
小大宇1 小时前
Python 集成 Conda
开发语言·python·conda
还是奇怪1 小时前
Simon Willison 用 DSPy 优化 Datasette Agent 提示词:提示工程正在变成可测试的软件工程
java·开发语言·软件工程
初学者,亦行者1 小时前
利用Pyecharts绘制堆叠柱状图
python·信息可视化·数据分析
林泽毅1 小时前
PyTRIO:当强化学习不再需要本地GPU
人工智能·python·深度学习·机器学习
小陈phd2 小时前
QAnything 阅读优化策略05——检索
人工智能·python·机器学习
ruofu332 小时前
wsl端是py312,但是项目是py311, 如何下载py311的依赖包(wheels)
python·docker
殷忆枫2 小时前
AI笔记三十七:AI边缘端道桥安全监测系统:YOLOv8n推理与Web监控全解析
笔记·yolo