Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active October 13, 2024 03:29
Show Gist options
  • Save BigRoy/28a05a2728db11cb068f4460e3f0be6d to your computer and use it in GitHub Desktop.
Save BigRoy/28a05a2728db11cb068f4460e3f0be6d to your computer and use it in GitHub Desktop.
Blackmagic Design Fusion save image from active comp view
import itertools
def iter_viewers(comp):
"""Iterate GLPreview views for Composition.
It prefers the active view first, then yields views
from the current frame (active window), then from the comp,
then from floating views last.
It does not return the "Monitor view" however.
Yields:
GLImageViewer: image viewers for the comp
"""
processed = set()
# Prefer current view
frame = comp.CurrentFrame
view = frame.CurrentViewer
if view:
yield "Active view", view
processed.add(str(view))
for name, preview in itertools.chain(
# all views for current active window/frame
frame.GetPreviewList().items(),
# all comp views
comp.GetPreviewList().items(),
# floating views for a comp are only returned by `fusion.GetPreviewList()
# so we will need to iterate those as a last resort too
fusion.GetPreviewList().items()
):
# Avoid duplicates
key = str(preview)
if key in processed:
continue
processed.add(key)
# Only allow GLPreview
if preview.GetID() != "GLPreview":
continue
view = preview.View
if not view:
continue
if str(view.Composition()) != str(comp):
continue
for viewer in itertools.chain([view.CurrentViewer], view.GetViewerList().values()):
if not viewer:
# CurrentViewer might be None
continue
key = str(viewer)
if key in processed:
continue
processed.add(key)
yield name, viewer
def get_comp_thumbnail(comp, destination):
"""Save current image from first active viewer from Composition"""
for name, viewer in iter_viewers(comp):
print(viewer.GetID())
if viewer.SaveFile(destination):
# Image only gets saved if has a tool set and only returns
# True in the case it has a valid image to save
print(f"Saved {name}: {destination}")
break
else:
print("Not saved")
get_comp_thumbnail(fusion.CurrentComp, destination="E:/test.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment