157 lines
6.8 KiB
Python
157 lines
6.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 = "WebGapArmatureSelectLinkedArmature"
|
|
OBJECT_NAME = "WebGapArmatureSelectLinkedObject"
|
|
ROOT_BONE = "WebGapArmatureSelectLinkedRoot"
|
|
CHILD_BONE = "WebGapArmatureSelectLinkedChild"
|
|
GRANDCHILD_BONE = "WebGapArmatureSelectLinkedGrandchild"
|
|
OTHER_BONE = "WebGapArmatureSelectLinkedOther"
|
|
|
|
|
|
def vector(value):
|
|
return [round(float(component), 6) for component in value]
|
|
|
|
|
|
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.select_linked 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 bone_report(bone):
|
|
return {
|
|
"name": bone.name,
|
|
"selected": bool(bone.select),
|
|
"hidden": bool(bone.hide),
|
|
"selectHead": bool(bone.select_head),
|
|
"selectTail": bool(bone.select_tail),
|
|
"parent": bone.parent.name if bone.parent else None,
|
|
"connected": bool(bone.use_connect),
|
|
"head": vector(bone.head),
|
|
"tail": vector(bone.tail),
|
|
}
|
|
|
|
|
|
def state_report():
|
|
armature = ensure_edit_mode()
|
|
return {
|
|
"object": OBJECT_NAME,
|
|
"armature": ARMATURE_NAME,
|
|
"activeBone": armature.edit_bones.active.name if armature.edit_bones.active else None,
|
|
"bones": [bone_report(bone) for bone in armature.edit_bones],
|
|
}
|
|
|
|
|
|
def stable_state(state):
|
|
return {
|
|
"object": state["object"],
|
|
"armature": state["armature"],
|
|
"activeBone": state["activeBone"],
|
|
"bones": sorted(
|
|
[{key: bone[key] for key in ("name", "selected", "hidden", "parent", "connected", "head", "tail")} for bone in state["bones"]],
|
|
key=lambda bone: bone["name"],
|
|
),
|
|
}
|
|
|
|
|
|
def validate_input(before):
|
|
by_name = {bone["name"]: bone for bone in before["bones"]}
|
|
if set(by_name) != {ROOT_BONE, CHILD_BONE, GRANDCHILD_BONE, OTHER_BONE}:
|
|
raise RuntimeError(f"unexpected armature.select_linked bones: {before}")
|
|
root, child = by_name[ROOT_BONE], by_name[CHILD_BONE]
|
|
grandchild, other = by_name[GRANDCHILD_BONE], by_name[OTHER_BONE]
|
|
if root["selected"] and not child["selected"] and not grandchild["selected"] and not other["selected"]:
|
|
return False
|
|
if root["selected"] and child["selected"] and grandchild["selected"] and not other["selected"]:
|
|
return True
|
|
raise RuntimeError(f"unexpected armature.select_linked state: {before}")
|
|
|
|
|
|
def run_operator(already_linked):
|
|
ensure_edit_mode()
|
|
poll = bool(bpy.ops.armature.select_linked.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_select_linked poll failed")
|
|
if not already_linked:
|
|
result = bpy.ops.armature.select_linked(all_forks=False)
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_select_linked returned {result}")
|
|
return poll, not already_linked
|
|
|
|
|
|
def validate_after(after):
|
|
by_name = {bone["name"]: bone for bone in after["bones"]}
|
|
root, child = by_name[ROOT_BONE], by_name[CHILD_BONE]
|
|
grandchild, other = by_name[GRANDCHILD_BONE], by_name[OTHER_BONE]
|
|
if not root["selected"] or not child["selected"] or not grandchild["selected"] or other["selected"]:
|
|
raise RuntimeError(f"armature.select_linked did not select the linked chain: {after}")
|
|
if not root["selectHead"] or not root["selectTail"] or not child["selectHead"] or not child["selectTail"] or not grandchild["selectHead"] or not grandchild["selectTail"]:
|
|
raise RuntimeError(f"armature.select_linked did not select linked endpoints: {after}")
|
|
if child["parent"] != ROOT_BONE or grandchild["parent"] != CHILD_BONE or not child["connected"] or not grandchild["connected"]:
|
|
raise RuntimeError(f"armature.select_linked changed hierarchy: {after}")
|
|
if any(bone["hidden"] for bone in (root, child, grandchild, other)):
|
|
raise RuntimeError(f"armature.select_linked changed visibility: {after}")
|
|
expected = {
|
|
ROOT_BONE: ([0.0, 0.0, 0.0], [0.0, 1.0, 0.0]),
|
|
CHILD_BONE: ([0.0, 1.0, 0.0], [0.0, 2.0, 0.0]),
|
|
GRANDCHILD_BONE: ([0.0, 2.0, 0.0], [0.0, 3.0, 0.0]),
|
|
OTHER_BONE: ([2.0, 0.0, 0.0], [2.0, 1.0, 0.0]),
|
|
}
|
|
for name, (head, tail) in expected.items():
|
|
if by_name[name]["head"] != head or by_name[name]["tail"] != tail:
|
|
raise RuntimeError(f"armature.select_linked changed {name} geometry: {after}")
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[sys.argv.index("--") + 1 :]
|
|
if len(arguments) != 2:
|
|
raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-select-linked-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()
|
|
already_linked = validate_input(before)
|
|
poll, changed = run_operator(already_linked)
|
|
after = state_report()
|
|
validate_after(after)
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-select-linked-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 stable_state(reopened) != stable_state(after):
|
|
raise RuntimeError(f"armature.select_linked save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {"schemaVersion": 1, "task": "M16-GAP-00253", "operation": "ARMATURE_SELECT_LINKED_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "selectedChain": [ROOT_BONE, CHILD_BONE, GRANDCHILD_BONE], "unselectedBone": OTHER_BONE, "allForks": False, "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "LINKED_CHAIN_SELECTED" if changed else "LINKED_CHAIN_ALREADY_SELECTED", "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(f"armature-select-linked-desktop-ok chain=3 allForks=false saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-select-linked-desktop-failed: {error}")
|
|
raise SystemExit(1)
|