#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureCollectionSelectArmature" OBJECT_NAME = "WebGapArmatureCollectionSelectObject" FIRST_COLLECTION = "WebGapArmatureCollectionSelectFirst" ACTIVE_COLLECTION = "WebGapArmatureCollectionSelectActive" LAST_COLLECTION = "WebGapArmatureCollectionSelectLast" FIRST_BONE = "WebGapArmatureCollectionSelectFirstBone" ACTIVE_BONE = "WebGapArmatureCollectionSelectActiveBone" LAST_BONE = "WebGapArmatureCollectionSelectLastBone" 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.collection_select 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 state_report(): armature = ensure_edit_mode() obj = bpy.data.objects[OBJECT_NAME] bones = sorted(bone.name for bone in armature.edit_bones) selected_bones = sorted(bone.name for bone in armature.edit_bones if bone.select) bpy.ops.object.mode_set(mode="OBJECT") collections = [] for index, collection in enumerate(armature.collections): collections.append( { "name": collection.name, "index": index, "bones": sorted(bone.name for bone in collection.bones), } ) bpy.context.view_layer.objects.active = obj bpy.ops.object.mode_set(mode="EDIT") return { "object": OBJECT_NAME, "armature": ARMATURE_NAME, "activeIndex": armature.collections.active_index, "bones": bones, "selectedBones": selected_bones, "collections": collections, } def run_operator(already_selected): ensure_edit_mode() poll = bool(bpy.ops.armature.collection_select.poll()) if not poll: raise RuntimeError("ARMATURE_OT_collection_select poll failed") if not already_selected: result = bpy.ops.armature.collection_select() if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_collection_select returned {result}") return poll, not already_selected 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-select-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() expected_collections = [FIRST_COLLECTION, ACTIVE_COLLECTION, LAST_COLLECTION] if [collection["name"] for collection in before["collections"]] != expected_collections: raise RuntimeError(f"unexpected collection_select collections: {before}") if before["activeIndex"] != 1: raise RuntimeError(f"unexpected collection_select active index: {before}") selected_before = set(before["selectedBones"]) if selected_before == {FIRST_BONE, ACTIVE_BONE}: already_selected = True elif selected_before == {FIRST_BONE}: already_selected = False else: raise RuntimeError(f"unexpected collection_select selection input: {before}") poll, changed = run_operator(already_selected) after = state_report() if after["activeIndex"] != 1 or after["selectedBones"] != sorted([FIRST_BONE, ACTIVE_BONE]): raise RuntimeError(f"collection_select did not select the active collection: {after}") expected_members = { FIRST_COLLECTION: [FIRST_BONE], ACTIVE_COLLECTION: [ACTIVE_BONE], LAST_COLLECTION: [LAST_BONE], } actual_members = {collection["name"]: collection["bones"] for collection in after["collections"]} if actual_members != expected_members: raise RuntimeError(f"collection_select changed collection membership: {after}") bpy.ops.object.mode_set(mode="OBJECT") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-select-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_select save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00228", "operation": "ARMATURE_COLLECTION_SELECT_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "selectedCollection": ACTIVE_COLLECTION, "alreadySelected": already_selected, "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "ACTIVE_COLLECTION_SELECTED" if changed else "ACTIVE_COLLECTION_ALREADY_SELECTED", "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-select-desktop-ok poll=true selected=active saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-collection-select-desktop-failed: {error}") raise SystemExit(1)