#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureAlignArmature" OBJECT_NAME = "WebGapArmatureAlignObject" PARENT_NAME = "WebGapArmatureAlignParent" CHILD_NAME = "WebGapArmatureAlignChild" def vector(value): return [round(float(component), 6) for component in value] def state_report(): armature = bpy.data.armatures.get(ARMATURE_NAME) obj = bpy.data.objects.get(OBJECT_NAME) if armature is None or obj is None or obj.type != "ARMATURE" or obj.data != armature: raise RuntimeError("armature.align fixture is missing") bones = [] for bone in armature.bones: bones.append({ "name": bone.name, "parent": bone.parent.name if bone.parent else None, "head": vector(bone.head_local), "tail": vector(bone.tail_local), }) bones.sort(key=lambda value: value["name"]) return {"object": OBJECT_NAME, "armature": ARMATURE_NAME, "bones": bones} def axes_aligned(state): parent = next(bone for bone in state["bones"] if bone["name"] == PARENT_NAME) child = next(bone for bone in state["bones"] if bone["name"] == CHILD_NAME) axis = [parent["tail"][index] - parent["head"][index] for index in range(3)] child_axis = [child["tail"][index] - child["head"][index] for index in range(3)] cross = [axis[1] * child_axis[2] - axis[2] * child_axis[1], axis[2] * child_axis[0] - axis[0] * child_axis[2], axis[0] * child_axis[1] - axis[1] * child_axis[0]] return max(abs(value) for value in cross) <= 1e-5 def run_operator(): bpy.context.view_layer.objects.active = bpy.data.objects[OBJECT_NAME] bpy.data.objects[OBJECT_NAME].select_set(True) if bpy.context.object.mode != "EDIT": bpy.ops.object.mode_set(mode="EDIT") armature = bpy.data.armatures[ARMATURE_NAME] parent = armature.edit_bones[PARENT_NAME] child = armature.edit_bones[CHILD_NAME] parent.select = True child.select = True armature.bones.active = armature.bones[PARENT_NAME] poll = bool(bpy.ops.armature.align.poll()) if not poll: raise RuntimeError("ARMATURE_OT_align poll failed") result = bpy.ops.armature.align() if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_align returned {result}") bpy.ops.object.mode_set(mode="OBJECT") 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-armature-align-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() before_child = next(bone for bone in before["bones"] if bone["name"] == CHILD_NAME) poll, _result = run_operator() after = state_report() already_aligned = axes_aligned(before) parent = next(bone for bone in after["bones"] if bone["name"] == PARENT_NAME) child = next(bone for bone in after["bones"] if bone["name"] == CHILD_NAME) if child["parent"] is not None or child["head"] != before_child["head"]: raise RuntimeError(f"armature.align changed unexpected parent/head state: {before} -> {after}") axis = [parent["tail"][index] - parent["head"][index] for index in range(3)] aligned_axis = [child["tail"][index] - child["head"][index] for index in range(3)] cross = [axis[1] * aligned_axis[2] - axis[2] * aligned_axis[1], axis[2] * aligned_axis[0] - axis[0] * aligned_axis[2], axis[0] * aligned_axis[1] - axis[1] * aligned_axis[0]] if max(abs(value) for value in cross) > 1e-5: raise RuntimeError(f"armature.align did not make child axis parallel: {before} -> {after}") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-align-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"armature.align save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00215", "operation": "ARMATURE_ALIGN_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "alreadyAligned": already_aligned, "poll": poll, "operatorStatus": "FINISHED", "mainMutation": "CHILD_ALIGNED_TO_PARENT", "saveReopen": "EXACT", "blenderVersion": bpy.app.version_string, } output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8") print("armature-align-desktop-ok poll=true status=FINISHED mainMutation=child_aligned_to_parent saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-align-desktop-failed: {error}") raise SystemExit(1)