You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Paraview Python脚本轴网格仅首次循环显示问题求助

Paraview批量切片截图时轴网格仅首次显示的解决方法

问题描述

已获取风模拟结果,需在Z方向7个不同高度切片,计算风速比并保存各高度截图。但轴网格仅在第一次循环中显示,后续循环仅显示风速比。

问题原因

  1. 循环内重复创建Calculator并调用Show()生成新显示对象,旧对象未被隐藏,新图层覆盖轴网格渲染层级
  2. 仅通过renderView1.AxesGrid = 'GridAxes3DActor'设置轴网格,未显式锁定其可见性,后续视图更新时可能被重置
  3. 冗余创建可视化管线对象,导致视图渲染状态混乱

解决代码

#### import the simple module from the paraview
from paraview.simple import *
import numpy as np

#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

# get the material library
materialLibrary1 = GetMaterialLibrary()

# Create a new 'Render View'
renderView1 = CreateView('RenderView')
renderView1.ViewSize = [1280, 720]
# 显式创建并获取轴网格对象,设置可见性
axesGrid = GetRenderViewAxesGrid(renderView1)
axesGrid.Visibility = True
renderView1.OrientationAxesVisibility = 0
renderView1.CenterOfRotation = [1000.0, 1145.0, 10.0]
renderView1.StereoType = 'Crystal Eyes'
renderView1.CameraPosition = [1000.0, 1145.0, 4864.234146297973]
renderView1.CameraFocalPoint = [1000.0, 1145.0, 10.0]
renderView1.CameraViewUp = [-1.0, 0.0, 0.0]
renderView1.CameraFocalDisk = 1.0
renderView1.CameraParallelScale = 1520.2055782031587
renderView1.BackEnd = 'OSPRay raycaster'
renderView1.OSPRayMaterialLibrary = materialLibrary1

SetActiveView(None)

# ----------------------------------------------------------------
# setup view layouts
# ----------------------------------------------------------------

# create new layout object 'Layout #1'
layout1 = CreateLayout(name='Layout #1')
layout1.AssignView(0, renderView1)

# ----------------------------------------------------------------
# restore active view
SetActiveView(renderView1)
# ----------------------------------------------------------------


# ----------------------------------------------------------------
# setup the inlet velocity profile
# ----------------------------------------------------------------
u_ref = 30
z_ref = 10
z0 = 0.0002
kappa = 0.41
u_star = u_ref*kappa/np.log((z_ref+z0)/z0)
height = [5,10, 15, 20, 25, 30, 35]
inlet_speed = [u_star / kappa * np.log((h + z0) / z0) for h in height]

# ----------------------------------------------------------------
# setup the data processing pipelines - 仅创建一次管线对象
# ----------------------------------------------------------------

# create a new 'OpenFOAMReader'
runfoam = OpenFOAMReader(FileName='run.foam')
runfoam.MeshRegions = ['internalMesh']
runfoam.CellArrays = ['U', 'epsilon', 'k', 'nut', 'p']


# Set active time step to the second timestep
reader = GetActiveSource()
view = GetActiveView()
times = reader.TimestepValues
view.ViewTime = times[1]

# create a new 'Slice'
slice1 = Slice(Input=runfoam)
slice1.SliceType = 'Plane'
slice1.HyperTreeGridSlicer = 'Plane'
slice1.SliceOffsetValues = [0.0]
slice1.SliceType.Normal = [0.0, 0.0, 1.0]
slice1.HyperTreeGridSlicer.Origin = [1000.0, 1145.0, 107.32312601804733]

# create a new 'Calculator' - 仅创建一次
calculator1 = Calculator(Input=slice1)
calculator1.ResultArrayName = 'Uratio'

# 初始化显示对象
calculator1Display = Show(calculator1, renderView1, 'GeometryRepresentation')

# get color transfer function/color map for 'Uratio'
uratioLUT = GetColorTransferFunction('Uratio')
uratioLUT.AutomaticRescaleRangeMode = 'Never'
uratioLUT.RGBPoints = [0.0, 0.0, 0.0, 0.5625, 0.1555554, 0.0, 0.0, 1.0, 0.5111112999999999, 0.0, 1.0, 1.0, 0.6888888999999999, 0.5, 1.0, 0.5, 0.8666664999999999, 1.0, 1.0, 0.0, 1.2222224, 1.0, 0.0, 0.0, 1.4, 0.5, 0.0, 0.0]
uratioLUT.ColorSpace = 'RGB'
uratioLUT.NumberOfTableValues = 14
uratioLUT.ScalarRangeInitialized = 1.0

# 设置显示属性
calculator1Display.Representation = 'Surface'
calculator1Display.ColorArrayName = ['POINTS', 'Uratio']
calculator1Display.LookupTable = uratioLUT
calculator1Display.OSPRayScaleArray = 'p'
calculator1Display.OSPRayScaleFunction = 'PiecewiseFunction'
calculator1Display.SelectOrientationVectors = 'Uratio'
calculator1Display.ScaleFactor = 229.0
calculator1Display.SelectScaleArray = 'p'
calculator1Display.GlyphType = 'Arrow'
calculator1Display.GlyphTableIndexArray = 'p'
calculator1Display.GaussianRadius = 11.450000000000001
calculator1Display.SetScaleArray = ['POINTS', 'p']
calculator1Display.ScaleTransferFunction = 'PiecewiseFunction'
calculator1Display.OpacityArray = ['POINTS', 'p']
calculator1Display.OpacityTransferFunction = 'PiecewiseFunction'
calculator1Display.DataAxesGrid = 'GridAxesRepresentation'
calculator1Display.PolarAxes = 'PolarAxesRepresentation'

# init the 'PiecewiseFunction' selected for 'ScaleTransferFunction'
calculator1Display.ScaleTransferFunction.Points = [-1396.27392578125, 0.0, 0.5, 0.0, 570.5612182617188, 1.0, 0.5, 0.0]

# init the 'PiecewiseFunction' selected for 'OpacityTransferFunction'
calculator1Display.OpacityTransferFunction.Points = [-1396.27392578125, 0.0, 0.5, 0.0, 570.5612182617188, 1.0, 0.5, 0.0]

# 设置轴网格样式
axesGrid.XTitle = ''
axesGrid.YTitle = ''
axesGrid.ZTitle = ''
axesGrid.XTitleBold = 1
axesGrid.XTitleFontSize = 28
axesGrid.YTitleBold = 1
axesGrid.YTitleFontSize = 28
axesGrid.AxesToLabel = 3
axesGrid.XLabelBold = 1
axesGrid.XLabelFontSize = 28
axesGrid.YLabelBold = 1
axesGrid.YLabelFontSize = 28
axesGrid.ZLabelBold = 1

# setup the color legend parameters
uratioLUTColorBar = GetScalarBar(uratioLUT, renderView1)
uratioLUTColorBar.WindowLocation = 'AnyLocation'
uratioLUTColorBar.Position = [0.7813682909604519, 0.12386300027987682]
uratioLUTColorBar.Title = 'U/Uinlet'
uratioLUTColorBar.ComponentTitle = ''
uratioLUTColorBar.TitleBold = 1
uratioLUTColorBar.TitleFontSize = 24
uratioLUTColorBar.LabelBold = 1
uratioLUTColorBar.LabelFontSize = 24
uratioLUTColorBar.RangeLabelFormat = '%-#6.1f'
uratioLUTColorBar.ScalarBarLength = 0.4345340050377833
uratioLUTColorBar.Visibility = 1
calculator1Display.SetScalarBarVisibility(renderView1, True)

# get opacity transfer function/opacity map for 'Uratio'
uratioPWF = GetOpacityTransferFunction('Uratio')
uratioPWF.Points = [0.0, 0.0, 0.5, 0.0, 1.4, 1.0, 0.5, 0.0]
uratioPWF.ScalarRangeInitialized = 1

# ----------------------------------------------------------------
# 循环处理不同高度切片
# ----------------------------------------------------------------
for ii in range(len(height)):
    # 更新切片高度
    slice1.SliceType.Origin = [1000.0, 1145.0, height[ii]]
    # 更新风速比计算公式
    calculator1.Function = f'U/{inlet_speed[ii]}'
    
    # 强制更新视图和管线
    renderView1.Update()
    calculator1.UpdatePipeline()
    
    # 保存截图
    SaveScreenshot(f'test{height[ii]}.tiff', renderView1, ImageResolution=[2560, 1440], 
        # TIFF options
        Compression='None')

关键修改点

  • 提前创建Slice、Calculator和显示对象,循环内仅更新参数,避免重复创建对象
  • 显式获取GridAxes3DActor并设置Visibility=True,锁定轴网格可见状态
  • 移除循环内冗余的可视化配置代码,统一在循环外初始化

内容的提问来源于stack exchange,提问作者Hansel Kuo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:07:47