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

相关推荐
边洛洛36 分钟前
next.js项目部署流程
开发语言·前端·javascript
分布式存储与RustFS37 分钟前
RustFS:MinIO的“平替”还是“乱杀”?
python·rust·对象存储·minio·存储·企业存储·rustfs
爱幻想-hjyp43 分钟前
【Python】uv包管理器常用命令
开发语言·python·uv
用什么都重名1 小时前
UV工具安装配置使用教程
python·uv·包管理器
牛马大师兄1 小时前
STM32实现低功耗管理使用配置知识梳理笔记
笔记·stm32·单片机·嵌入式硬件·物联网·低功耗
Python×CATIA工业智造1 小时前
Python装饰器解包装技术详解:从原理到高级应用
python·pycharm
哈皮Superman1 小时前
【Research】MagicFuzzer: Scalable deadlock detection for large-scale applications
java·开发语言·数据库
lly2024061 小时前
NoSQL 简介
开发语言
千里马-horse1 小时前
Boost.Iostreams 简介
开发语言·c++·boost
王哈哈^_^1 小时前
PyTorch vs TensorFlow:从入门到落地的全方位对比
人工智能·pytorch·python·深度学习·计算机视觉·tensorflow·1024程序员节