Last active
September 8, 2021 09:13
-
-
Save lhofmann/2ca7befb0bb0037fb9397c87c5936619 to your computer and use it in GitHub Desktop.
Clone ParaView Render View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Functions to replicate renderViews in ParaView. | |
v1: Theodore Baltis (https://discourse.paraview.org/u/theodorebaltis) | |
https://discourse.paraview.org/t/feature-request-clone-renderview/2370/3 | |
v2: Bastian Loehrer (https://discourse.paraview.org/u/bastian) | |
- merged the separate scripts into one file with the dedicated functions split_right() and split_down() | |
- added function clone_to_new_layout | |
v3: Lutz Hofmann (https://github.com/lhofmann/) | |
- support sources with multiple output ports | |
- use vtkSMProxyClipboard for copying properties | |
- only create representations for visible ports | |
- fix automatic rescaling of lookup tables | |
''' | |
from paraview import simple, servermanager | |
try: | |
# ParaView >= 5.8 | |
from paraview.modules.vtkRemotingMisc import vtkSMProxyClipboard | |
except ImportError: | |
# ParaView <= 5.7 | |
from paraview.modules.vtkPVServerManagerDefault import vtkSMProxyClipboard | |
def split_right(): | |
''' Split the current renderView horizontally by inserting a new | |
renderView to the right. Display properties of objects in the pipeline, | |
camera settings, etc. are cloned to the new renderView. | |
''' | |
simple._DisableFirstRenderCameraReset() | |
renderView1 = simple.GetActiveView() | |
layout1 = simple.GetLayout() | |
# Split cell | |
layout1.SplitViewHorizontal(renderView1) | |
# Create a new 'Render View' | |
renderView2 = simple.CreateView('RenderView') | |
# Assign to layout (needed in ParaView 5.7.0+) | |
pv_ver = simple.GetParaViewVersion() | |
if pv_ver >= 5.7: | |
simple.AssignViewToLayout(renderView2, layout1, hint=2) | |
clone_properties(renderView1, renderView2) | |
def split_down(): | |
''' Split the current renderView vertically by inserting a new | |
renderView at the bottom. Display properties of objects in the pipeline, | |
camera settings, etc. are cloned to the new renderView. | |
''' | |
simple._DisableFirstRenderCameraReset() | |
renderView1 = simple.GetActiveView() | |
layout1 = simple.GetLayout() | |
# Split cell | |
layout1.SplitViewVertical(renderView1) | |
# Create a new 'Render View' | |
renderView2 = simple.CreateView('RenderView') | |
# Assign to layout (needed in ParaView 5.7.0+) | |
pv_ver = simple.GetParaViewVersion() | |
if pv_ver >= 5.7: | |
simple.AssignViewToLayout(renderView2, layout1, hint=2) | |
clone_properties(renderView1, renderView2) | |
def clone_to_new_layout(): | |
''' Create a new layout and clone the current renderView into it. | |
This only works properly if the current layout holds nothing but one | |
renderView. | |
''' | |
simple._DisableFirstRenderCameraReset() | |
renderView1 = simple.GetActiveView() | |
layout1 = simple.GetLayout() | |
# Create new layout | |
layout2 = simple.CreateLayout('Layout') | |
# Create a new 'Render View' | |
renderView2 = simple.CreateView('RenderView') | |
# Assign to layout (needed in ParaView 5.7.0+) | |
pv_ver = simple.GetParaViewVersion() | |
if pv_ver >= 5.7: | |
simple.AssignViewToLayout(renderView2, layout2, hint=2) | |
clone_properties(renderView1, renderView2) | |
def clone_properties( renderView1, renderView2 ): | |
'''Configure everything in renderView2 like it is in renderView1. | |
''' | |
clipboard = vtkSMProxyClipboard() | |
# Copy view properties | |
clipboard.Copy(renderView1.SMProxy) | |
clipboard.Paste(renderView2.SMProxy) | |
# Get sources | |
sources = simple.GetSources() | |
output_ports = [servermanager.OutputPort(source, i) | |
for source in sources.values() | |
for i in range(source.SMProxy.GetNumberOfOutputPorts())] | |
controller = servermanager.ParaViewPipelineController() | |
# Copy representation properties | |
for proxy in output_ports: | |
visible = controller.GetVisibility(proxy.SMProxy, proxy.Port, renderView1.SMProxy) | |
if not visible: | |
continue | |
rep1 = simple.GetRepresentation(proxy=proxy, view=renderView1) | |
if rep1 is None: | |
continue | |
clipboard.Copy(rep1.SMProxy) | |
lut = rescale_mode = None | |
try: | |
array_name = rep1.ColorArrayName[1] | |
if array_name: | |
lut = simple.GetColorTransferFunction(array_name, rep1) | |
rescale_mode = lut.AutomaticRescaleRangeMode | |
lut.AutomaticRescaleRangeMode = 'Never' | |
except: | |
lut = rescale_mode = None | |
rep2 = simple.GetRepresentation(proxy=proxy, view=renderView2) | |
clipboard.Paste(rep2.SMProxy) | |
if rescale_mode is not None: | |
lut.AutomaticRescaleRangeMode = rescale_mode | |
# simple.UpdateScalarBars(renderView2) | |
# Explicitly copy camera properties | |
camera_props = ['Position', 'FocalPoint', 'ViewAngle', 'ViewUp', 'ParallelProjection', 'ParallelScale'] | |
for prop in camera_props: | |
value = getattr(renderView1, 'Camera' + prop) | |
setattr(renderView2, 'Camera' + prop, value) | |
simple.Render(renderView2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment