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

相关推荐
BanLul5 分钟前
进程与线程 (三)——线程间通信
c语言·开发语言·算法
十八朵郁金香9 分钟前
【JavaScript】深入理解模块化
开发语言·javascript·ecmascript
Hello.Reader18 分钟前
深入理解 Rust 的 `Rc<T>`:实现多所有权的智能指针
开发语言·后端·rust
程序员阿鹏21 分钟前
jdbc批量插入数据到MySQL
java·开发语言·数据库·mysql·intellij-idea
yoona102021 分钟前
Rust编程语言入门教程(八)所有权 Stack vs Heap
开发语言·后端·rust·区块链·学习方法
莲动渔舟22 分钟前
国产编辑器EverEdit - 在编辑器中对文本进行排序
java·开发语言·编辑器
伊一大数据&人工智能学习日志29 分钟前
自然语言处理NLP 04案例——苏宁易购优质评论与差评分析
人工智能·python·机器学习·自然语言处理·数据挖掘
陈无左耳、33 分钟前
HarmonyOS学习第4天: DevEco Studio初体验
学习·华为·harmonyos
刀客12334 分钟前
python3+TensorFlow 2.x(六)自编码器
人工智能·python·tensorflow
微刻时光35 分钟前
影刀RPA中级证书-Excel进阶-开票清单
经验分享·python·低代码·rpa·影刀·影刀证书·影刀实战