141 lines
5.8 KiB
Python
141 lines
5.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 = "WebGapArmatureCollectionUnassignArmature"
|
|
OBJECT_NAME = "WebGapArmatureCollectionUnassignObject"
|
|
SOURCE_COLLECTION = "WebGapArmatureCollectionUnassignSource"
|
|
RETAINED_COLLECTION = "WebGapArmatureCollectionUnassignRetained"
|
|
BONE_NAME = "WebGapArmatureCollectionUnassignBone"
|
|
|
|
|
|
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_unassign 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()
|
|
selected_bones = sorted(bone.name for bone in armature.edit_bones if bone.select)
|
|
bones = sorted(bone.name for bone in armature.edit_bones)
|
|
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 = bpy.data.objects[OBJECT_NAME]
|
|
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_unassigned):
|
|
armature = ensure_edit_mode()
|
|
poll = bool(bpy.ops.armature.collection_unassign.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_collection_unassign poll failed")
|
|
if not already_unassigned:
|
|
result = bpy.ops.armature.collection_unassign()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_collection_unassign returned {result}")
|
|
return poll, not already_unassigned
|
|
|
|
|
|
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-unassign-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 = [SOURCE_COLLECTION, RETAINED_COLLECTION]
|
|
if [collection["name"] for collection in before["collections"]] != expected_collections:
|
|
raise RuntimeError(f"unexpected collection_unassign collections: {before}")
|
|
if before["activeIndex"] != 0 or before["selectedBones"] != [BONE_NAME]:
|
|
raise RuntimeError(f"unexpected collection_unassign active/selection input: {before}")
|
|
source_before = before["collections"][0]["bones"]
|
|
retained_before = before["collections"][1]["bones"]
|
|
if source_before == [] and retained_before == [BONE_NAME]:
|
|
already_unassigned = True
|
|
elif source_before == [BONE_NAME] and retained_before == [BONE_NAME]:
|
|
already_unassigned = False
|
|
else:
|
|
raise RuntimeError(f"unexpected collection_unassign membership input: {before}")
|
|
poll, changed = run_operator(already_unassigned)
|
|
after = state_report()
|
|
if after["activeIndex"] != 0 or after["selectedBones"] != [BONE_NAME]:
|
|
raise RuntimeError(f"collection_unassign changed active or selection state: {after}")
|
|
actual_members = {collection["name"]: collection["bones"] for collection in after["collections"]}
|
|
expected_members = {SOURCE_COLLECTION: [], RETAINED_COLLECTION: [BONE_NAME]}
|
|
if actual_members != expected_members:
|
|
raise RuntimeError(f"collection_unassign did not remove only the active membership: {after}")
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-unassign-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_unassign save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00230",
|
|
"operation": "ARMATURE_COLLECTION_UNASSIGN_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"activeCollection": SOURCE_COLLECTION,
|
|
"unassignedBone": BONE_NAME,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "ACTIVE_COLLECTION_MEMBERSHIP_REMOVED" if changed else "ACTIVE_COLLECTION_ALREADY_EMPTY",
|
|
"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-unassign-desktop-ok poll=true source=empty retained=bone saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-collection-unassign-desktop-failed: {error}")
|
|
raise SystemExit(1)
|