Skip to content

Instantly share code, notes, and snippets.

@robinknowles
Last active July 13, 2023 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinknowles/58bea8c932e62602e5c1c4f5de49ff59 to your computer and use it in GitHub Desktop.
Save robinknowles/58bea8c932e62602e5c1c4f5de49ff59 to your computer and use it in GitHub Desktop.
A semi-automatic way to explode assemblies in ParaView & create simple plots that reveal the hidden parts in your models.

Exploding Assemblies in ParaView

Accompanies OnCFD Newsletter #155: "Exploding assemblies in ParaView"

Attached is a minimal Python state file that can be used to re-create the required pipeline.

Needs ParaView v5.11 (or greater).

Save the state file locally, edit it to read one of your own geometry files & open it with File > Load State.

Then cross your fingers & hope for the best 🫣

For more info - see the original article 👋

# state file generated using paraview version 5.11.1
import paraview
paraview.compatibility.major = 5
paraview.compatibility.minor = 11
#### import the simple module from the paraview
from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()
# ----------------------------------------------------------------
# setup the data processing pipelines
#
# Edit the inputGeom to use your own geometry file
# ----------------------------------------------------------------
# create a new 'Wavefront OBJ Reader'
inputGeom = WavefrontOBJReader(registrationName='InputGeom.obj', FileName='/PATH/TO/YOUR_GEOMETRY_FILE_GOES_HERE.obj')
# create a new 'Compute Connected Surface Properties'
computeConnectedSurfaceProperties = ComputeConnectedSurfaceProperties(registrationName='ComputeConnectedSurfaceProperties', Input=inputGeom)
# create a new 'Programmable Filter'
programmableFilter = ProgrammableFilter(registrationName='ProgrammableFilter', Input=computeConnectedSurfaceProperties)
programmableFilter.Script = """
# Using the output of the previous filter
input0 = inputs[0]
# Grab an object's centroid based on its ID & store it in a new field
data = input0.FieldData["ObjectCentroids"][input0.CellData["ObjectIds"]]
# Append the centroid field as cell data for use later
output.CellData.append(data, "Centroid")
# Append the object ID as cell data for use later
output.CellData.append(input0.CellData["ObjectIds"], "Id")
"""
# create a new 'Cell Data to Point Data'
cellDatatoPointData = CellDatatoPointData(registrationName='CellDatatoPointData', Input=programmableFilter)
# create a modified version of the original 'Centroid' vector
# this version exagerates the explosion in the Y & Z-directions & tones it down in the X-direction
# modify this to suit your requirements
calculator = Calculator(registrationName='Calculator', Input=cellDatatoPointData)
calculator.ResultArrayName = 'ToWarp'
calculator.Function = '(0.75*Centroid_X*iHat)+(1.1*Centroid_Y*jHat)+(2.2*Centroid_Z*kHat)'
# explode our assembly using 'Warp By Vector' & our new/tweaked 'ToWarp' Vector
warpByVector = WarpByVector(registrationName='WarpByVector', Input=calculator)
warpByVector.Vectors = ['POINTS', 'ToWarp']
warpByVector.ScaleFactor = 1.0
# ----------------------------------------------------------------
# setup views used in the visualization
# ----------------------------------------------------------------
# Create a new 'Render View'
renderView1 = CreateView('RenderView')
renderView1.CameraPosition = [-2525, 1327, 440]
renderView1.CameraFocalPoint = [-390, 58, -190]
renderView1.CameraViewUp = [0, 0, 1]
renderView1.CameraFocalDisk = 1.0
renderView1.CameraParallelScale = 663.607
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 visualization in view 'renderView1'
# show data from warpByVector
warpByVectorDisplay = Show(warpByVector, renderView1, 'GeometryRepresentation')
# get 2D transfer function for 'Id'
idTF2D = GetTransferFunction2D('Id')
# get color transfer function/color map for 'Id'
idLUT = GetColorTransferFunction('Id')
idLUT.TransferFunction2D = idTF2D
idLUT.RGBPoints = [0.0, 0.231373, 0.298039, 0.752941, 7.5, 0.865003, 0.865003, 0.865003, 15.0, 0.705882, 0.0156863, 0.14902]
idLUT.ScalarRangeInitialized = 1.0
# set some basic display properties
warpByVectorDisplay.Representation = 'Surface'
warpByVectorDisplay.ColorArrayName = ['POINTS', 'Id']
warpByVectorDisplay.LookupTable = idLUT
# ----------------------------------------------------------------
# restore active source
SetActiveSource(warpByVector)
renderView1.ResetCamera(True)
# ----------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment