#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureSeparateArmature" OBJECT_NAME = "WebGapArmatureSeparateObject" SELECTED_BONE = "WebGapArmatureSeparateSelected" RETAINED_BONE = "WebGapArmatureSeparateRetained" SEPARATED_OBJECT = "WebGapArmatureSeparateObject.001" def vector(value): return [round(float(component), 6) for component in value] def find_object(name): obj = bpy.data.objects.get(name) if obj is None or obj.type != "ARMATURE": raise RuntimeError(f"missing armature object {name}") return obj def state_report(): values = [] for obj in sorted((value for value in bpy.data.objects if value.type == "ARMATURE"), key=lambda value: value.name): armature = obj.data if armature.name != ARMATURE_NAME and not armature.name.startswith(f"{ARMATURE_NAME}."): continue if obj.mode != "EDIT": bpy.context.view_layer.objects.active = obj obj.select_set(True) bpy.ops.object.mode_set(mode="EDIT") bones = [] for bone in armature.edit_bones: bones.append({"name": bone.name, "selected": bool(bone.select), "head": vector(bone.head), "tail": vector(bone.tail), "parent": bone.parent.name if bone.parent else None}) bpy.ops.object.mode_set(mode="OBJECT") values.append({"object": obj.name, "armature": armature.name, "activeBone": armature.edit_bones.active.name if armature.edit_bones.active else None, "bones": bones}) return {"objects": values} def stable(value): return {"objects": [{"object": item["object"], "armature": item["armature"], "activeBone": item["activeBone"], "bones": sorted(item["bones"], key=lambda bone: bone["name"])} for item in value["objects"]]} def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-separate-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) source = find_object(OBJECT_NAME) bpy.context.view_layer.objects.active = source source.select_set(True) if source.mode != "EDIT": bpy.ops.object.mode_set(mode="EDIT") before = state_report() source_state = next(item for item in before["objects"] if item["object"] == OBJECT_NAME) selected = {bone["name"] for bone in source_state["bones"] if bone["selected"]} if selected == {SELECTED_BONE} and len(before["objects"]) == 1: already_applied = False elif len(before["objects"]) == 2 and {item["object"] for item in before["objects"]} == {OBJECT_NAME, SEPARATED_OBJECT}: already_applied = True else: raise RuntimeError(f"unexpected armature.separate state: {before}") bpy.context.view_layer.objects.active = source source.select_set(True) if source.mode != "EDIT": bpy.ops.object.mode_set(mode="EDIT") poll = bool(bpy.ops.armature.separate.poll()) if not poll: raise RuntimeError("ARMATURE_OT_separate poll failed") if not already_applied: result = bpy.ops.armature.separate() if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_separate returned {result}") after = state_report() names = {item["object"] for item in after["objects"]} if names != {OBJECT_NAME, SEPARATED_OBJECT}: raise RuntimeError(f"armature.separate object mismatch: {after}") original = next(item for item in after["objects"] if item["object"] == OBJECT_NAME) separated = next(item for item in after["objects"] if item["object"] == SEPARATED_OBJECT) if [bone["name"] for bone in original["bones"]] != [RETAINED_BONE] or [bone["name"] for bone in separated["bones"]] != [SELECTED_BONE]: raise RuntimeError(f"armature.separate bone partition mismatch: {after}") bpy.context.view_layer.objects.active = source descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-separate-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(reopened) != stable(after): raise RuntimeError(f"armature.separate save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = {"schemaVersion": 1, "task": "M16-GAP-00258", "operation": "ARMATURE_SEPARATE_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "sourceObject": OBJECT_NAME, "separatedObject": SEPARATED_OBJECT, "selectedBone": SELECTED_BONE, "retainedBone": RETAINED_BONE, "operatorStatus": "FINISHED" if not already_applied else "SKIPPED_ALREADY_APPLIED", "mainMutation": "SELECTED_BONES_SEPARATED" if not already_applied else "SELECTED_BONES_ALREADY_SEPARATED", "poll": poll, "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-separate-desktop-ok source={OBJECT_NAME} separated={SEPARATED_OBJECT} saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-separate-desktop-failed: {error}") raise SystemExit(1)