117 lines
5.4 KiB
Python
117 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureCollectionCreateAssignArmature"
|
|
OBJECT_NAME = "WebGapArmatureCollectionCreateAssignObject"
|
|
SOURCE_COLLECTION = "WebGapArmatureCollectionCreateAssignSource"
|
|
NEW_COLLECTION = "WebGapArmatureCollectionCreateAssignNew"
|
|
BONE_NAME = "WebGapArmatureCollectionCreateAssignBone"
|
|
|
|
|
|
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_create_and_assign 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_created):
|
|
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]
|
|
edit_bone = armature.edit_bones[BONE_NAME]
|
|
bpy.ops.armature.select_all(action="DESELECT")
|
|
edit_bone.select = True
|
|
edit_bone.select_head = True
|
|
edit_bone.select_tail = True
|
|
armature.bones.active = armature.bones[BONE_NAME]
|
|
poll = bool(bpy.ops.armature.collection_create_and_assign.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_collection_create_and_assign poll failed")
|
|
if not already_created:
|
|
result = bpy.ops.armature.collection_create_and_assign(name=NEW_COLLECTION)
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_collection_create_and_assign returned {result}")
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
return poll, not already_created
|
|
|
|
|
|
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-create-assign-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_created = True
|
|
elif names == {SOURCE_COLLECTION}:
|
|
already_created = False
|
|
else:
|
|
raise RuntimeError(f"unexpected collection_create_and_assign input: {before}")
|
|
poll, changed = run_operator(already_created)
|
|
after = state_report()
|
|
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 added["bones"] != [BONE_NAME]:
|
|
raise RuntimeError(f"collection_create_and_assign produced unexpected collections: {after}")
|
|
if source["bones"] != [BONE_NAME]:
|
|
raise RuntimeError(f"collection_create_and_assign unexpectedly removed source membership: {after}")
|
|
if added["index"] != 1 or after["activeIndex"] != added["index"]:
|
|
raise RuntimeError(f"collection_create_and_assign did not activate the new collection: {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-create-assign-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_create_and_assign save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00223",
|
|
"operation": "ARMATURE_COLLECTION_CREATE_AND_ASSIGN_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"alreadyCreated": already_created,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "NEW_COLLECTION_CREATED_AND_BONE_ASSIGNED" if changed else "ALREADY_NEW_COLLECTION_CREATED_AND_BONE_ASSIGNED",
|
|
"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-create-assign-desktop-ok poll=true collection=created-and-assigned saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-collection-create-assign-desktop-failed: {error}")
|
|
raise SystemExit(1)
|