Files
workinf_Blender_Wasm/tools/web/check-action-armature-select-hierarchy-desktop.py
mes123456 9f43244982
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
Continue Blender Web parity task handoff
2026-08-23 05:47:21 -04:00

188 lines
7.1 KiB
Python

#!/usr/bin/env python3
import hashlib
import json
import os
import pathlib
import shutil
import sys
import tempfile
import bpy
ARMATURE_NAME = "WebGapArmatureSelectHierarchyArmature"
OBJECT_NAME = "WebGapArmatureSelectHierarchyObject"
ROOT_BONE = "WebGapArmatureSelectHierarchyRoot"
CHILD_BONE = "WebGapArmatureSelectHierarchyChild"
OTHER_BONE = "WebGapArmatureSelectHierarchyOther"
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_hierarchy 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, OTHER_BONE}:
raise RuntimeError(f"unexpected armature.select_hierarchy bones: {before}")
root, child, other = by_name[ROOT_BONE], by_name[CHILD_BONE], by_name[OTHER_BONE]
if (
before["activeBone"] == ROOT_BONE
and root["selected"]
and not child["selected"]
and not other["selected"]
and child["parent"] == ROOT_BONE
and child["connected"]
):
return False
if (
before["activeBone"] == CHILD_BONE
and not root["selected"]
and child["selected"]
and not other["selected"]
and child["parent"] == ROOT_BONE
and child["connected"]
):
return True
raise RuntimeError(f"unexpected armature.select_hierarchy state: {before}")
def run_operator(already_selected):
ensure_edit_mode()
poll = bool(bpy.ops.armature.select_hierarchy.poll())
if not poll:
raise RuntimeError("ARMATURE_OT_select_hierarchy poll failed")
if not already_selected:
result = bpy.ops.armature.select_hierarchy(direction="CHILD", extend=False)
if result != {"FINISHED"}:
raise RuntimeError(f"ARMATURE_OT_select_hierarchy returned {result}")
return poll, not already_selected
def validate_after(after):
by_name = {bone["name"]: bone for bone in after["bones"]}
root, child, other = by_name[ROOT_BONE], by_name[CHILD_BONE], by_name[OTHER_BONE]
if after["activeBone"] != CHILD_BONE or root["selected"] or not child["selected"] or other["selected"]:
raise RuntimeError(f"armature.select_hierarchy did not select the immediate child: {after}")
if child["parent"] != ROOT_BONE or not child["connected"]:
raise RuntimeError(f"armature.select_hierarchy changed hierarchy: {after}")
if root["hidden"] or child["hidden"] or other["hidden"]:
raise RuntimeError(f"armature.select_hierarchy changed visibility: {after}")
if root["head"] != [0.0, 0.0, 0.0] or root["tail"] != [0.0, 1.0, 0.0]:
raise RuntimeError(f"armature.select_hierarchy changed root geometry: {after}")
if child["head"] != [0.0, 1.0, 0.0] or child["tail"] != [0.0, 2.0, 0.0]:
raise RuntimeError(f"armature.select_hierarchy changed child geometry: {after}")
if other["head"] != [2.0, 0.0, 0.0] or other["tail"] != [2.0, 1.0, 0.0]:
raise RuntimeError(f"armature.select_hierarchy changed other 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-hierarchy-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_selected = validate_input(before)
poll, changed = run_operator(already_selected)
after = state_report()
validate_after(after)
bpy.ops.object.mode_set(mode="OBJECT")
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-select-hierarchy-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_hierarchy save/reopen drift: {after} != {reopened}")
shutil.copyfile(temporary_path, fixture)
report = {
"schemaVersion": 1,
"task": "M16-GAP-00251",
"operation": "ARMATURE_SELECT_HIERARCHY_DESKTOP",
"fixture": str(fixture),
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
"before": before,
"after": reopened,
"activeBoneBefore": before["activeBone"],
"activeBoneAfter": reopened["activeBone"],
"direction": "CHILD",
"extend": False,
"rootBone": ROOT_BONE,
"childBone": CHILD_BONE,
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
"mainMutation": "IMMEDIATE_CHILD_SELECTED" if changed else "IMMEDIATE_CHILD_ALREADY_SELECTED",
"poll": poll,
"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-hierarchy-desktop-ok direction=child child={CHILD_BONE} saveReopen=exact")
finally:
temporary_path.unlink(missing_ok=True)
if __name__ == "__main__":
try:
main()
except Exception as error:
print(f"armature-select-hierarchy-desktop-failed: {error}")
raise SystemExit(1)