134 lines
5.4 KiB
Python
134 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
OBJECT_NAME = "WebGapAnimNewSlotObject"
|
|
ACTION_NAME = "WebGapAnimNewSlotAction"
|
|
SLOT_NAME = "OBWebGapAnimNewSlot"
|
|
|
|
|
|
def action_report(action):
|
|
channels = []
|
|
if action is not None:
|
|
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"], value["frames"], value["values"]))
|
|
return channels
|
|
|
|
|
|
def state_report():
|
|
obj = bpy.data.objects.get(OBJECT_NAME)
|
|
if obj is None or obj.animation_data is None or obj.animation_data.action is None:
|
|
raise RuntimeError("slot_new_for_id fixture object or action is missing")
|
|
action = obj.animation_data.action
|
|
slot = obj.animation_data.action_slot
|
|
return {
|
|
"activeObject": bpy.context.view_layer.objects.active.name if bpy.context.view_layer.objects.active else None,
|
|
"action": action.name,
|
|
"slot": slot.identifier if slot else None,
|
|
"slots": sorted(slot.identifier for slot in action.slots),
|
|
"channels": action_report(action),
|
|
}
|
|
|
|
|
|
def is_created(state):
|
|
return (
|
|
state["action"] == ACTION_NAME
|
|
and state["slot"] == f"{SLOT_NAME}.001"
|
|
and state["slots"] == [SLOT_NAME, f"{SLOT_NAME}.001"]
|
|
and len(state["channels"]) == 2
|
|
)
|
|
|
|
|
|
def run_operator(obj):
|
|
window = bpy.context.window
|
|
screen = window.screen
|
|
area = next((candidate for candidate in screen.areas if candidate.height >= 200), None)
|
|
region = next((candidate for candidate in area.regions if candidate.type == "WINDOW"), None) if area else None
|
|
override = {"window": window, "screen": screen, "area": area, "region": region, "animated_id": obj}
|
|
with bpy.context.temp_override(**{key: value for key, value in override.items() if value is not None}):
|
|
poll = bool(bpy.ops.anim.slot_new_for_id.poll())
|
|
if not poll:
|
|
raise RuntimeError("ANIM_OT_slot_new_for_id poll failed")
|
|
result = bpy.ops.anim.slot_new_for_id()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ANIM_OT_slot_new_for_id returned {result}")
|
|
return poll, result
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[sys.argv.index("--") + 1 :]
|
|
if len(arguments) != 2:
|
|
raise SystemExit("usage: blender -b --factory-startup --python check-action-slot-new-for-id-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)
|
|
obj = bpy.data.objects.get(OBJECT_NAME)
|
|
before = state_report()
|
|
preserved = is_created(before)
|
|
if preserved:
|
|
poll = True
|
|
result = {"FINISHED"}
|
|
else:
|
|
if before["action"] != ACTION_NAME or before["slot"] != SLOT_NAME or before["slots"] != [SLOT_NAME] or len(before["channels"]) != 1:
|
|
raise RuntimeError(f"slot_new_for_id source state is wrong: {before}")
|
|
poll, result = run_operator(obj)
|
|
after = state_report()
|
|
if not is_created(after):
|
|
raise RuntimeError(f"slot_new_for_id did not duplicate the assigned slot: {after}")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-slot-new-for-id-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.slot_new_for_id 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-00207",
|
|
"operation": "ANIM_SLOT_NEW_FOR_ID_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED",
|
|
"mainMutation": "SLOT_DUPLICATED" if not preserved else "NONE_ALREADY_DUPLICATED",
|
|
"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-slot-new-for-id-desktop-ok poll=true status=FINISHED mainMutation=slot_duplicated saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"anim-slot-new-for-id-desktop-failed: {error}")
|
|
raise SystemExit(1)
|