使用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的二次开发

相关推荐
крон1 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan2 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊2 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之3 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?3 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
scdifsn4 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
军训猫猫头4 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
lyaihao4 小时前
使用python实现奔跑的线条效果
python·绘图
恰薯条的屑海鸥4 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习