#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureCollectionRemoveArmature" OBJECT_NAME = "WebGapArmatureCollectionRemoveObject" FIRST_COLLECTION = "WebGapArmatureCollectionRemoveFirst" REMOVED_COLLECTION = "WebGapArmatureCollectionRemoveRemoved" LAST_COLLECTION = "WebGapArmatureCollectionRemoveLast" FIRST_BONE = "WebGapArmatureCollectionRemoveFirstBone" REMOVED_BONE = "WebGapArmatureCollectionRemoveRemovedBone" LAST_BONE = "WebGapArmatureCollectionRemoveLastBone" 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_remove 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, "activeIndex": armature.collections.active_index, "bones": sorted(bone.name for bone in armature.bones), "collections": collections, } def run_operator(already_removed): obj = bpy.data.objects[OBJECT_NAME] bpy.context.view_layer.objects.active = obj obj.select_set(True) poll = bool(bpy.ops.armature.collection_remove.poll()) if not poll: raise RuntimeError("ARMATURE_OT_collection_remove poll failed") if not already_removed: result = bpy.ops.armature.collection_remove() if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_collection_remove returned {result}") return poll, not already_removed 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-remove-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() before_names = [collection["name"] for collection in before["collections"]] if before_names == [FIRST_COLLECTION, LAST_COLLECTION] and before["activeIndex"] == 1: already_removed = True elif before_names == [FIRST_COLLECTION, REMOVED_COLLECTION, LAST_COLLECTION] and before["activeIndex"] == 1: already_removed = False else: raise RuntimeError(f"unexpected collection_remove input: {before}") poll, changed = run_operator(already_removed) after = state_report() if [collection["name"] for collection in after["collections"]] != [FIRST_COLLECTION, LAST_COLLECTION]: raise RuntimeError(f"collection_remove produced unexpected collections: {after}") if after["activeIndex"] != 1 or after["bones"] != sorted([FIRST_BONE, REMOVED_BONE, LAST_BONE]): raise RuntimeError(f"collection_remove changed active index or armature bones: {after}") expected_members = {FIRST_COLLECTION: [FIRST_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_remove retained removed collection membership: {after}") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-remove-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_remove save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00226", "operation": "ARMATURE_COLLECTION_REMOVE_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "alreadyRemoved": already_removed, "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "ACTIVE_COLLECTION_REMOVED" if changed else "ACTIVE_COLLECTION_ALREADY_REMOVED", "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-remove-desktop-ok poll=true removed=active saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-collection-remove-desktop-failed: {error}") raise SystemExit(1)