#!/usr/bin/env python3 import hashlib import json import os import pathlib import sys import tempfile import bpy OBJECTS = ("WebGapAnimReplaceNewOldA", "WebGapAnimReplaceNewOldB") OLD_ACTION = "WebGapAnimReplaceNewOldAction" 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], } ) channels.sort(key=lambda value: (value["path"], value["index"])) return {"name": action.name, "channels": channels} def state_report(): objects = {name: bpy.data.objects.get(name) for name in OBJECTS} if any(obj is None for obj in objects.values()): raise RuntimeError("replace_action_new fixture objects are missing") return { "activeObject": bpy.context.view_layer.objects.active.name if bpy.context.view_layer.objects.active else None, "objects": {name: action_report(obj) for name, obj in objects.items()}, "actions": {action.name: int(action.users) for action in bpy.data.actions}, } def is_replaced(state): names = [value["name"] for value in state["objects"].values()] return all(name and name != OLD_ACTION for name in names) and len(set(names)) == 1 def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender -b --factory-startup --python check-action-replace-action-new-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() preserved = is_replaced(before) if not preserved: if before["activeObject"] != OBJECTS[0]: raise RuntimeError(f"replace_action_new active object is wrong: {before}") if any(value["name"] != OLD_ACTION for value in before["objects"].values()): raise RuntimeError(f"replace_action_new source actions are wrong: {before}") old_action = bpy.data.actions.get(OLD_ACTION) if old_action is None: raise RuntimeError("replace_action_new old action is missing") if preserved: poll = True result = {"FINISHED"} else: poll = bool(bpy.ops.anim.replace_action_new.poll()) if not poll: raise RuntimeError("ANIM_OT_replace_action_new poll failed") result = bpy.ops.anim.replace_action_new(old_session_uid=old_action.session_uid) if result != {"FINISHED"}: raise RuntimeError(f"ANIM_OT_replace_action_new returned {result}") after = state_report() if not is_replaced(after): raise RuntimeError(f"replace_action_new did not replace all users: {after}") new_action_name = next(iter({value["name"] for value in after["objects"].values()})) descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-replace-action-new-reopen-", suffix=".blend") os.close(descriptor) 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(f"anim.replace_action_new save/reopen drift: {after} != {reopened}") if not preserved: bpy.ops.wm.save_as_mainfile(filepath=str(fixture), check_existing=False, compress=True) report = { "schemaVersion": 1, "task": "M16-GAP-00203", "operation": "ANIM_REPLACE_ACTION_NEW_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "newAction": new_action_name, "poll": poll, "operatorStatus": "FINISHED", "mainMutation": "ACTION_REPLACED_WITH_NEW" if not preserved else "NONE_ALREADY_REPLACED", "saveReopen": "EXACT", "blenderVersion": bpy.app.version_string, } if preserved: report["evidenceStatus"] = "PRESERVED" if not preserved or not output.exists(): output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8") print("anim-replace-action-new-desktop-ok poll=true status=FINISHED mainMutation=action_replaced_with_new saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"anim-replace-action-new-desktop-failed: {error}") raise SystemExit(1)