#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureFlipNamesArmature" OBJECT_NAME = "WebGapArmatureFlipNamesObject" LEFT_BONE = "WebGapArmatureFlipBone.L" RIGHT_BONE = "WebGapArmatureFlipBone.R" OTHER_BONE = "WebGapArmatureFlipOther" def ensure_edit_mode(): obj = bpy.data.objects.get(OBJECT_NAME) armature = bpy.data.armatures.get(ARMATURE_NAME) if armature is None or obj is None or obj.type != "ARMATURE" or obj.data != armature: raise RuntimeError("armature.flip_names fixture is missing") bpy.context.view_layer.objects.active = obj obj.select_set(True) if obj.mode != "EDIT": bpy.ops.object.mode_set(mode="EDIT") return armature def bone_report(bone): return { "name": bone.name, "selected": bool(bone.select), "selectHead": bool(bone.select_head), "selectTail": bool(bone.select_tail), "parent": bone.parent.name if bone.parent else None, "connected": bool(bone.use_connect), "head": [round(value, 6) for value in bone.head], "tail": [round(value, 6) for value in bone.tail], } def state_report(): armature = ensure_edit_mode() return { "object": OBJECT_NAME, "armature": ARMATURE_NAME, "activeBone": armature.edit_bones.active.name if armature.edit_bones.active else None, "bones": [bone_report(bone) for bone in armature.edit_bones], } def stable_state(state): return { "object": state["object"], "armature": state["armature"], "activeBone": state["activeBone"], "bones": sorted( [ {key: bone[key] for key in ("name", "selected", "parent", "connected", "head", "tail")} for bone in state["bones"] ], key=lambda bone: bone["name"], ), } def geometry_by_x(state): return {round(bone["head"][0], 6): bone for bone in state["bones"]} def validate_input(before): names = {bone["name"] for bone in before["bones"]} if names == {LEFT_BONE, RIGHT_BONE, OTHER_BONE}: by_x = geometry_by_x(before) left, right, other = by_x[-1.0], by_x[1.0], by_x[0.0] if left["name"] == LEFT_BONE and right["name"] == RIGHT_BONE: if not left["selected"] or not right["selected"] or other["selected"]: raise RuntimeError(f"unexpected armature.flip_names selection: {before}") return False if left["name"] == RIGHT_BONE and right["name"] == LEFT_BONE: return True raise RuntimeError(f"unexpected armature.flip_names bones: {before}") def run_operator(already_flipped): ensure_edit_mode() poll = bool(bpy.ops.armature.flip_names.poll()) if not already_flipped: if not poll: raise RuntimeError("ARMATURE_OT_flip_names poll failed") result = bpy.ops.armature.flip_names(do_strip_numbers=False) if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_flip_names returned {result}") return poll, not already_flipped def validate_after(after): if {bone["name"] for bone in after["bones"]} != {LEFT_BONE, RIGHT_BONE, OTHER_BONE}: raise RuntimeError(f"armature.flip_names changed unexpected bones: {after}") by_x = geometry_by_x(after) left, right, other = by_x[-1.0], by_x[1.0], by_x[0.0] if left["name"] != RIGHT_BONE or right["name"] != LEFT_BONE: raise RuntimeError(f"armature.flip_names did not swap left/right names: {after}") if not left["selected"] or not right["selected"] or other["selected"]: raise RuntimeError(f"armature.flip_names selection drift: {after}") if other["head"] != [0.0, 0.0, 2.0] or other["tail"] != [0.0, 1.0, 2.0]: raise RuntimeError(f"armature.flip_names changed independent bone: {after}") def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit( "usage: blender -b --factory-startup --python check-action-armature-flip-names-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() already_flipped = validate_input(before) poll, changed = run_operator(already_flipped) after = state_report() validate_after(after) bpy.ops.object.mode_set(mode="OBJECT") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-flip-names-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 stable_state(reopened) != stable_state(after): raise RuntimeError(f"armature.flip_names save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00243", "operation": "ARMATURE_FLIP_NAMES_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "leftGeometryBone": RIGHT_BONE, "rightGeometryBone": LEFT_BONE, "doStripNumbers": False, "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "SELECTED_LEFT_RIGHT_NAMES_FLIPPED" if changed else "SELECTED_LEFT_RIGHT_NAMES_ALREADY_FLIPPED", "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(f"armature-flip-names-desktop-ok left={RIGHT_BONE} right={LEFT_BONE} saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-flip-names-desktop-failed: {error}") raise SystemExit(1)