121 lines
5.1 KiB
Python
121 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
OBJECTS = ("WebGapAnimReplaceOldA", "WebGapAnimReplaceOldB", "WebGapAnimReplaceNewUser")
|
|
OLD_ACTION = "WebGapAnimReplaceOldAction"
|
|
NEW_ACTION = "WebGapAnimReplaceNewAction"
|
|
|
|
|
|
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 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": {name: int(bpy.data.actions[name].users) for name in (OLD_ACTION, NEW_ACTION)},
|
|
}
|
|
|
|
|
|
def is_replaced(state):
|
|
return all(value["name"] == NEW_ACTION for value in state["objects"].values())
|
|
|
|
|
|
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-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"] != "WebGapAnimReplaceOldA":
|
|
raise RuntimeError(f"replace_action active object is wrong: {before}")
|
|
if before["objects"]["WebGapAnimReplaceOldA"]["name"] != OLD_ACTION or before["objects"]["WebGapAnimReplaceOldB"]["name"] != OLD_ACTION:
|
|
raise RuntimeError(f"replace_action source actions are wrong: {before}")
|
|
old_action = bpy.data.actions.get(OLD_ACTION)
|
|
new_action = bpy.data.actions.get(NEW_ACTION)
|
|
if old_action is None or new_action is None:
|
|
raise RuntimeError("replace_action actions are missing")
|
|
poll = bool(bpy.ops.anim.replace_action.poll())
|
|
if not poll:
|
|
raise RuntimeError("ANIM_OT_replace_action poll failed")
|
|
result = bpy.ops.anim.replace_action(old_session_uid=old_action.session_uid, new_session_uid=new_action.session_uid)
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ANIM_OT_replace_action returned {result}")
|
|
after = state_report()
|
|
if not is_replaced(after):
|
|
raise RuntimeError(f"replace_action did not replace all users: {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-replace-action-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 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-00202",
|
|
"operation": "ANIM_REPLACE_ACTION_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED",
|
|
"mainMutation": "ACTIONS_REPLACED" 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-desktop-ok poll=true status=FINISHED mainMutation=actions_replaced saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"anim-replace-action-desktop-failed: {error}")
|
|
raise SystemExit(1)
|