131 lines
5.5 KiB
Python
131 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureCollectionRemoveUnusedArmature"
|
|
OBJECT_NAME = "WebGapArmatureCollectionRemoveUnusedObject"
|
|
FIRST_COLLECTION = "WebGapArmatureCollectionRemoveUnusedFirst"
|
|
UNUSED_FIRST_COLLECTION = "WebGapArmatureCollectionRemoveUnusedUnusedFirst"
|
|
LAST_COLLECTION = "WebGapArmatureCollectionRemoveUnusedLast"
|
|
UNUSED_LAST_COLLECTION = "WebGapArmatureCollectionRemoveUnusedUnusedLast"
|
|
FIRST_BONE = "WebGapArmatureCollectionRemoveUnusedFirstBone"
|
|
LAST_BONE = "WebGapArmatureCollectionRemoveUnusedLastBone"
|
|
|
|
|
|
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_unused 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_unused.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_collection_remove_unused poll failed")
|
|
if not already_removed:
|
|
result = bpy.ops.armature.collection_remove_unused()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_collection_remove_unused 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-unused-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,
|
|
UNUSED_FIRST_COLLECTION,
|
|
LAST_COLLECTION,
|
|
UNUSED_LAST_COLLECTION,
|
|
] and before["activeIndex"] == 2:
|
|
already_removed = False
|
|
else:
|
|
raise RuntimeError(f"unexpected collection_remove_unused 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_unused produced unexpected collections: {after}")
|
|
if after["activeIndex"] != 1 or after["bones"] != sorted([FIRST_BONE, LAST_BONE]):
|
|
raise RuntimeError(f"collection_remove_unused 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_unused changed retained collection membership: {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-remove-unused-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_unused save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00227",
|
|
"operation": "ARMATURE_COLLECTION_REMOVE_UNUSED_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"removedCollections": [UNUSED_FIRST_COLLECTION, UNUSED_LAST_COLLECTION],
|
|
"alreadyRemoved": already_removed,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "UNUSED_COLLECTIONS_REMOVED" if changed else "UNUSED_COLLECTIONS_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-unused-desktop-ok poll=true removed=2 saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-collection-remove-unused-desktop-failed: {error}")
|
|
raise SystemExit(1)
|