110 lines
4.8 KiB
Python
110 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureCollectionAddArmature"
|
|
OBJECT_NAME = "WebGapArmatureCollectionAddObject"
|
|
SOURCE_COLLECTION = "WebGapArmatureCollectionAddExisting"
|
|
NEW_COLLECTION = "Bones"
|
|
BONE_NAME = "WebGapArmatureCollectionAddBone"
|
|
|
|
|
|
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_add 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, "collections": collections}
|
|
|
|
|
|
def run_operator(already_added):
|
|
bpy.context.view_layer.objects.active = bpy.data.objects[OBJECT_NAME]
|
|
bpy.data.objects[OBJECT_NAME].select_set(True)
|
|
armature = bpy.data.armatures[ARMATURE_NAME]
|
|
if not already_added:
|
|
armature.collections.active_index = 0
|
|
poll = bool(bpy.ops.armature.collection_add.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_collection_add poll failed")
|
|
if not already_added:
|
|
result = bpy.ops.armature.collection_add()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_collection_add returned {result}")
|
|
return poll, not already_added
|
|
|
|
|
|
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-add-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()
|
|
names = {collection["name"] for collection in before["collections"]}
|
|
if names == {SOURCE_COLLECTION, NEW_COLLECTION}:
|
|
already_added = True
|
|
elif names == {SOURCE_COLLECTION}:
|
|
already_added = False
|
|
else:
|
|
raise RuntimeError(f"unexpected collection_add input: {before}")
|
|
poll, changed = run_operator(already_added)
|
|
after = state_report()
|
|
if len(after["collections"]) != 2:
|
|
raise RuntimeError(f"collection_add did not produce exactly two collections: {before} -> {after}")
|
|
source = next((collection for collection in after["collections"] if collection["name"] == SOURCE_COLLECTION), None)
|
|
added = next((collection for collection in after["collections"] if collection["name"] == NEW_COLLECTION), None)
|
|
if source is None or added is None or source["index"] != 0 or added["index"] != 1:
|
|
raise RuntimeError(f"collection_add produced unexpected collections: {after}")
|
|
if source["bones"] != [BONE_NAME] or added["bones"]:
|
|
raise RuntimeError(f"collection_add changed collection membership: {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-add-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_add save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00221",
|
|
"operation": "ARMATURE_COLLECTION_ADD_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"alreadyAdded": not changed,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "BONE_COLLECTION_ADDED" if changed else "ALREADY_BONE_COLLECTION_ADDED",
|
|
"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-add-desktop-ok poll=true collection=Bones saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-collection-add-desktop-failed: {error}")
|
|
raise SystemExit(1)
|