142 lines
5.6 KiB
Python
142 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureCollectionDeselectArmature"
|
|
OBJECT_NAME = "WebGapArmatureCollectionDeselectObject"
|
|
ACTIVE_COLLECTION = "WebGapArmatureCollectionDeselectActive"
|
|
OTHER_COLLECTION = "WebGapArmatureCollectionDeselectOther"
|
|
ACTIVE_BONE = "WebGapArmatureCollectionDeselectActiveBone"
|
|
OTHER_BONE = "WebGapArmatureCollectionDeselectOtherBone"
|
|
|
|
|
|
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_deselect fixture is missing")
|
|
was_edit_mode = obj.mode == "EDIT"
|
|
if not was_edit_mode:
|
|
bpy.context.view_layer.objects.active = obj
|
|
obj.select_set(True)
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
bones = []
|
|
for bone in armature.edit_bones:
|
|
bones.append({"name": bone.name, "selected": bool(bone.select)})
|
|
if not was_edit_mode:
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
return {
|
|
"object": OBJECT_NAME,
|
|
"armature": ARMATURE_NAME,
|
|
"activeIndex": armature.collections.active_index,
|
|
"bones": bones,
|
|
}
|
|
|
|
|
|
def select_fixture_bones():
|
|
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]
|
|
bpy.ops.armature.select_all(action="DESELECT")
|
|
for name in (ACTIVE_BONE, OTHER_BONE):
|
|
bone = armature.edit_bones[name]
|
|
bone.select = True
|
|
bone.select_head = True
|
|
bone.select_tail = True
|
|
armature.bones.active = armature.bones[ACTIVE_BONE]
|
|
|
|
|
|
def run_operator(already_deselected):
|
|
if already_deselected:
|
|
obj = bpy.data.objects[OBJECT_NAME]
|
|
bpy.context.view_layer.objects.active = obj
|
|
obj.select_set(True)
|
|
if bpy.context.object.mode != "EDIT":
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
armature = bpy.data.armatures[ARMATURE_NAME]
|
|
bpy.ops.armature.select_all(action="DESELECT")
|
|
other_bone = armature.edit_bones[OTHER_BONE]
|
|
other_bone.select = True
|
|
other_bone.select_head = True
|
|
other_bone.select_tail = True
|
|
else:
|
|
select_fixture_bones()
|
|
armature = bpy.data.armatures[ARMATURE_NAME]
|
|
poll = bool(bpy.ops.armature.collection_deselect.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_collection_deselect poll failed")
|
|
if not already_deselected:
|
|
result = bpy.ops.armature.collection_deselect()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_collection_deselect returned {result}")
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
return poll, not already_deselected
|
|
|
|
|
|
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-deselect-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()
|
|
if before["activeIndex"] != 0:
|
|
raise RuntimeError(f"unexpected active collection: {before}")
|
|
already_deselected = all(
|
|
not bone["selected"] for bone in before["bones"] if bone["name"] == ACTIVE_BONE
|
|
)
|
|
poll, changed = run_operator(already_deselected)
|
|
after = state_report()
|
|
selected = {bone["name"]: bone["selected"] for bone in after["bones"]}
|
|
if selected.get(ACTIVE_BONE) is not False or selected.get(OTHER_BONE) is not True:
|
|
raise RuntimeError(f"collection_deselect produced unexpected selection: {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-collection-deselect-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_deselect save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00224",
|
|
"operation": "ARMATURE_COLLECTION_DESELECT_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"alreadyDeselected": already_deselected,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "ACTIVE_COLLECTION_BONES_DESELECTED" if changed else "ACTIVE_COLLECTION_BONES_ALREADY_DESELECTED",
|
|
"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-deselect-desktop-ok poll=true activeCollection=deselected saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-collection-deselect-desktop-failed: {error}")
|
|
raise SystemExit(1)
|