#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ACTIVE_OBJECT = "WebGapAnimMergeActiveObject" SOURCE_OBJECT = "WebGapAnimMergeSourceObject" ACTIVE_ACTION = "WebGapAnimMergeActiveAction" SOURCE_ACTION = "WebGapAnimMergeSourceAction" def action_report(obj): action = obj.animation_data.action if obj and obj.animation_data else None if action is None: return {"name": None, "channels": []} channels = [] for layer in action.layers: for strip in layer.strips: for bag in strip.channelbags: for curve in bag.fcurves: channels.append( { "path": curve.data_path, "index": curve.array_index, "frames": [round(float(key.co.x), 6) for key in curve.keyframe_points], "values": [round(float(key.co.y), 6) for key in curve.keyframe_points], "selected": [bool(key.select_control_point) for key in curve.keyframe_points], } ) channels.sort(key=lambda value: (value["path"], value["index"])) return {"name": action.name, "channels": channels} def state_report(): active = bpy.data.objects.get(ACTIVE_OBJECT) source = bpy.data.objects.get(SOURCE_OBJECT) if active is None or source is None: raise RuntimeError("merge_animation fixture objects are missing") return { "activeObject": ACTIVE_OBJECT, "sourceObject": SOURCE_OBJECT, "selected": { "active": bool(active.select_get()), "source": bool(source.select_get()), }, "active": bpy.context.view_layer.objects.active == active, "activeObjectAction": action_report(active), "sourceObjectAction": action_report(source), } def write_report(fixture, output, before, after, evidence_status=None): report = { "schemaVersion": 1, "task": "M16-GAP-00198", "operation": "ANIM_MERGE_ANIMATION_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": after, "poll": True, "operatorStatus": "FINISHED", "mainMutation": "ANIMATION_MERGED", "saveReopen": "EXACT", "blenderVersion": bpy.app.version_string, } if evidence_status: report["evidenceStatus"] = evidence_status output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8") def is_merged(state): active_action = state["activeObjectAction"] source_action = state["sourceObjectAction"] return ( active_action["name"] == ACTIVE_ACTION and source_action["name"] == ACTIVE_ACTION and len(active_action["channels"]) == 2 and len(source_action["channels"]) == 2 ) def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender -b --factory-startup --python check-action-merge-animation-desktop.py -- FIXTURE REPORT") fixture, output = (pathlib.Path(value).resolve() for value in arguments) bpy.ops.wm.open_mainfile(filepath=str(fixture), load_ui=False) before = state_report() if is_merged(before): if not output.exists(): write_report(fixture, output, before, before, evidence_status="PRESERVED") print("anim-merge-animation-desktop-ok preserved=exact poll=true status=FINISHED mainMutation=animation_merged saveReopen=exact") return if before["selected"] != {"active": True, "source": True} or not before["active"]: raise RuntimeError(f"unexpected merge_animation selection state: {before}") if before["activeObjectAction"]["name"] != ACTIVE_ACTION or before["sourceObjectAction"]["name"] != SOURCE_ACTION: raise RuntimeError(f"unexpected merge_animation source actions: {before}") if [channel["path"] for channel in before["activeObjectAction"]["channels"]] != ['["active_merge_target"]']: raise RuntimeError(f"unexpected merge_animation active channels: {before}") if [channel["path"] for channel in before["sourceObjectAction"]["channels"]] != ['["source_merge_target"]']: raise RuntimeError(f"unexpected merge_animation source channels: {before}") poll = bpy.ops.anim.merge_animation.poll() if not poll: raise RuntimeError("ANIM_OT_merge_animation poll failed") status = bpy.ops.anim.merge_animation() if status != {"FINISHED"}: raise RuntimeError(f"ANIM_OT_merge_animation returned {status}") after = state_report() if not is_merged(after): raise RuntimeError(f"merge_animation did not merge selected actions: {after}") descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-merge-animation-", suffix=".blend") os.close(descriptor) pathlib.Path(temporary).unlink(missing_ok=True) temporary_path = pathlib.Path(temporary) try: bpy.ops.wm.save_as_mainfile(filepath=str(temporary_path), check_existing=False, compress=True) bpy.ops.wm.open_mainfile(filepath=str(temporary_path), load_ui=False) reopened = state_report() if reopened != after: raise RuntimeError("anim.merge_animation save/reopen drift") shutil.copyfile(temporary_path, fixture) write_report(fixture, output, before, reopened) print("anim-merge-animation-desktop-ok poll=true status=FINISHED mainMutation=animation_merged saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"anim-merge-animation-desktop-failed: {error}") raise SystemExit(1)