#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureCollectionAssignArmature" OBJECT_NAME = "WebGapArmatureCollectionAssignObject" SOURCE_COLLECTION = "WebGapArmatureCollectionAssignSource" TARGET_COLLECTION = "WebGapArmatureCollectionAssignTarget" BONE_NAME = "WebGapArmatureCollectionAssignBone" 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.collection_assign fixture is missing") collections = [] for index, collection in enumerate(armature.collections): collections.append({"name": collection.name, "index": index, "bones": sorted(bone.name for bone in collection.bones)}) return {"object": OBJECT_NAME, "armature": ARMATURE_NAME, "collections": collections} def run_operator(already_assigned): 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] edit_bone = armature.edit_bones[BONE_NAME] bpy.ops.armature.select_all(action="DESELECT") edit_bone.select = True edit_bone.select_head = True edit_bone.select_tail = True armature.bones.active = armature.bones[BONE_NAME] poll = bool(bpy.ops.armature.collection_assign.poll()) if not poll: raise RuntimeError("ARMATURE_OT_collection_assign poll failed") if not already_assigned: result = bpy.ops.armature.collection_assign(name=TARGET_COLLECTION) if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_collection_assign returned {result}") bpy.ops.object.mode_set(mode="OBJECT") return poll, not already_assigned def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-collection-assign-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() target_before = next((collection for collection in before["collections"] if collection["name"] == TARGET_COLLECTION), None) source_before = next((collection for collection in before["collections"] if collection["name"] == SOURCE_COLLECTION), None) if source_before is None or target_before is None or source_before["bones"] != [BONE_NAME]: raise RuntimeError(f"unexpected collection_assign input: {before}") already_assigned = BONE_NAME in target_before["bones"] poll, changed = run_operator(already_assigned) after = state_report() source_after = next((collection for collection in after["collections"] if collection["name"] == SOURCE_COLLECTION), None) target_after = next((collection for collection in after["collections"] if collection["name"] == TARGET_COLLECTION), None) if source_after is None or target_after is None or BONE_NAME not in target_after["bones"]: raise RuntimeError(f"collection_assign did not assign the bone: {before} -> {after}") if BONE_NAME not in source_after["bones"]: raise RuntimeError(f"collection_assign unexpectedly removed the source membership: {after}") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-assign-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.collection_assign save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00222", "operation": "ARMATURE_COLLECTION_ASSIGN_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "alreadyAssigned": already_assigned, "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "BONE_ASSIGNED_TO_TARGET_COLLECTION" if changed else "ALREADY_BONE_ASSIGNED_TO_TARGET_COLLECTION", "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-collection-assign-desktop-ok poll=true target=assigned saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-collection-assign-desktop-failed: {error}") raise SystemExit(1)