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

相关推荐
xixiaoyunya3 小时前
Python 异步编程入门:用 asyncio 轻松实现并发任务
开发语言·python·php
把我格子衫拿来3 小时前
Dice 很高就够了吗?分割评价中的 IoU、表面距离与 HD95
python·图像分割·模型评估·医学影像·dice
学逆向的3 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
曲径海盗3 小时前
【Python 学习第 31 天】复习:变量与数据类型 等 3 题
python·学习笔记·编程入门
Data_Journal3 小时前
如何绕过 Cloudflare:网页抓取的最佳方法
网络·python
小坏讲微服务3 小时前
刚发布一个月的JDK 27,为什么我建议你立刻升级?
java·开发语言·spring boot·spring·架构·求职招聘·springboot4
我命由我123454 小时前
智能助手通用问答边界
人工智能·python·学习·ai·学习方法·ai编程·智能体
鬼手点金4 小时前
Agent 的学习机制:后训练、上下文学习与外部化学习
java·人工智能·git·学习·自动化·nanogpt
Angeldream1235 小时前
第 4 章 医学影像预处理
图像处理·人工智能·python·医学影像·医学影像处理
几个几个n5 小时前
LaTeX 入门学习
学习