#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureAssignArmature" OBJECT_NAME = "WebGapArmatureAssignObject" SOURCE_COLLECTION = "WebGapArmatureAssignSource" TARGET_COLLECTION = "WebGapArmatureAssignTarget" PARENT_NAME = "WebGapArmatureAssignParent" CHILD_NAME = "WebGapArmatureAssignChild" 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.assign_to_collection 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] parent = armature.edit_bones[PARENT_NAME] child = armature.edit_bones[CHILD_NAME] bpy.ops.armature.select_all(action="DESELECT") parent.select = False child.select = True child.select_head = True child.select_tail = True armature.bones.active = armature.bones[CHILD_NAME] poll = bool(bpy.ops.armature.assign_to_collection.poll()) if not poll: raise RuntimeError("ARMATURE_OT_assign_to_collection poll failed") if not already_assigned: result = bpy.ops.armature.assign_to_collection(collection_index=1) if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_assign_to_collection returned {result}") bpy.ops.object.mode_set(mode="OBJECT") return poll def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-assign-to-collection-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) already_assigned = CHILD_NAME in target_before["bones"] poll = run_operator(already_assigned) after = state_report() target_after = next(collection for collection in after["collections"] if collection["name"] == TARGET_COLLECTION) if CHILD_NAME not in target_after["bones"]: raise RuntimeError(f"assign_to_collection did not assign child: {before} -> {after}") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-assign-collection-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.assign_to_collection save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00216", "operation": "ARMATURE_ASSIGN_TO_COLLECTION_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "alreadyAssigned": already_assigned, "poll": poll, "operatorStatus": "FINISHED", "mainMutation": "CHILD_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-assign-to-collection-desktop-ok poll=true status=FINISHED mainMutation=child_assigned_to_target_collection saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-assign-to-collection-desktop-failed: {error}") raise SystemExit(1)