From 4246d5705cd561051e00683cbd7a6a2a06285885 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Thu, 2 Mar 2023 20:44:33 +0100 Subject: [PATCH] bbb-fusion-panzoom: Add FusionScript to deal with pan zoom events Signed-off-by: Sylvain Munaut --- bbb-fusion-panzoom.py | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 bbb-fusion-panzoom.py diff --git a/bbb-fusion-panzoom.py b/bbb-fusion-panzoom.py new file mode 100644 index 0000000..1e974e5 --- /dev/null +++ b/bbb-fusion-panzoom.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import bbb + +REF_SIZE=(1600,901) + +def keyframes_from_events(pze, conv_fn, merge_threshold=1.5, fps=24): + + rv = [] + le = None + + for e in pze: + # First ? + if le is None: + le = e + rv.append((0, conv_fn(e))) + continue + + # How much time elapsed ? + if e.ts - le.ts > merge_threshold: + rv.append( (int(round((e.ts * fps))) - 1, conv_fn(le)) ) + rv.append( (int(round((e.ts * fps))), conv_fn(e)) ) + + else: + rv.pop() + rv.append( (int(round(( e.ts * fps))), conv_fn(e)) ) + + # Last event + le = e + + return rv + + +# "Pan" : floating point values from -4.0*width to 4.0*width +# "Tilt" : floating point values from -4.0*height to 4.0*height +# "ZoomX" : floating point values from 0.0 to 100.0 +# "ZoomY" : floating point values from 0.0 to 100.0 + +def to_xform(e): + sz = REF_SIZE[0] / e.w + cx = sz * ( 0.5 - e.x / REF_SIZE[0]) + cy = sz * (-0.5 + (e.y + e.h) / REF_SIZE[1]) + return (cx, cy, sz) + + +def animate_transform(filename, xform, limits=(0,None)): + # Load raw events + pze = bbb.parse_panzooms_xml(filename) + + # Limits + pze = pze[limits[0]:limits[1]] + + # Convert to keyframes + kf = keyframes_from_events(pze, to_xform) + print(kf) + + # Apply to tool + tool = comp.FindTool(xform) + + # Switching Center/Size to animation will create a keyframe + # at the current time, so go to first time + comp.CurrentTime = kf[0][0] + + tool.Center = comp.Path() + tool.Size = comp.BezierSpline() + + for ts, (cx, cy, sz) in kf: + tool.Center[ts] = [cx, cy] + tool.Size[ts] = sz + +# Edit the following as needed and run using comp.RunScript in the Fusion console +print("Running ...") +animate_transform('panzooms.xml', 'Transform1') +print("Done")