Continue Blender Web parity task handoff
This commit is contained in:
164
tools/web/check-action-armature-parent-set-desktop.py
Normal file
164
tools/web/check-action-armature-parent-set-desktop.py
Normal file
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env python3
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import bpy
|
||||
|
||||
|
||||
ARMATURE_NAME = "WebGapArmatureParentSetArmature"
|
||||
OBJECT_NAME = "WebGapArmatureParentSetObject"
|
||||
PARENT_BONE = "WebGapArmatureParentSetParent"
|
||||
CHILD_BONE = "WebGapArmatureParentSetChild"
|
||||
OTHER_BONE = "WebGapArmatureParentSetOther"
|
||||
|
||||
|
||||
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.parent_set 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": [round(value, 6) for value in bone.head],
|
||||
"tail": [round(value, 6) for value in 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) != {PARENT_BONE, CHILD_BONE, OTHER_BONE}:
|
||||
raise RuntimeError(f"unexpected armature.parent_set bones: {before}")
|
||||
parent, child, other = by_name[PARENT_BONE], by_name[CHILD_BONE], by_name[OTHER_BONE]
|
||||
if parent["parent"] is None and child["parent"] is None and not parent["connected"] and not child["connected"] and parent["selected"] and child["selected"] and not other["selected"]:
|
||||
return False
|
||||
if child["parent"] == PARENT_BONE and child["connected"] and parent["selected"] and child["selected"] and other["parent"] is None:
|
||||
return True
|
||||
raise RuntimeError(f"unexpected armature.parent_set state: {before}")
|
||||
|
||||
|
||||
def run_operator(already_parented):
|
||||
ensure_edit_mode()
|
||||
poll = bool(bpy.ops.armature.parent_set.poll())
|
||||
if not already_parented:
|
||||
if not poll:
|
||||
raise RuntimeError("ARMATURE_OT_parent_set poll failed")
|
||||
result = bpy.ops.armature.parent_set(type="CONNECTED")
|
||||
if result != {"FINISHED"}:
|
||||
raise RuntimeError(f"ARMATURE_OT_parent_set returned {result}")
|
||||
return poll, not already_parented
|
||||
|
||||
|
||||
def validate_after(after):
|
||||
by_name = {bone["name"]: bone for bone in after["bones"]}
|
||||
parent, child, other = by_name[PARENT_BONE], by_name[CHILD_BONE], by_name[OTHER_BONE]
|
||||
if child["parent"] != PARENT_BONE or not child["connected"] or not child["selected"]:
|
||||
raise RuntimeError(f"armature.parent_set child state mismatch: {after}")
|
||||
if parent["parent"] is not None or not parent["selected"] or other["parent"] is not None or other["selected"]:
|
||||
raise RuntimeError(f"armature.parent_set changed independent bones: {after}")
|
||||
if child["head"] != [0.0, 1.0, 0.0] or child["tail"] != [0.0, 2.0, 0.0]:
|
||||
raise RuntimeError(f"armature.parent_set changed child geometry: {after}")
|
||||
if parent["head"] != [0.0, 0.0, 0.0] or parent["tail"] != [0.0, 1.0, 0.0]:
|
||||
raise RuntimeError(f"armature.parent_set changed parent geometry: {after}")
|
||||
if other["head"] != [2.0, 0.0, 0.0] or other["tail"] != [2.0, 1.0, 0.0]:
|
||||
raise RuntimeError(f"armature.parent_set changed independent 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-parent-set-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_parented = validate_input(before)
|
||||
poll, changed = run_operator(already_parented)
|
||||
after = state_report()
|
||||
validate_after(after)
|
||||
|
||||
bpy.ops.object.mode_set(mode="OBJECT")
|
||||
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-parent-set-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.parent_set save/reopen drift: {after} != {reopened}")
|
||||
shutil.copyfile(temporary_path, fixture)
|
||||
report = {
|
||||
"schemaVersion": 1,
|
||||
"task": "M16-GAP-00247",
|
||||
"operation": "ARMATURE_PARENT_SET_DESKTOP",
|
||||
"fixture": str(fixture),
|
||||
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
||||
"before": before,
|
||||
"after": reopened,
|
||||
"parentBone": PARENT_BONE,
|
||||
"childBone": CHILD_BONE,
|
||||
"setType": "CONNECTED",
|
||||
"poll": poll,
|
||||
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
||||
"mainMutation": "SELECTED_CHILD_PARENT_SET_CONNECTED" if changed else "SELECTED_CHILD_PARENT_ALREADY_CONNECTED",
|
||||
"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-parent-set-desktop-ok child={CHILD_BONE} parent={PARENT_BONE} connected=true saveReopen=exact")
|
||||
finally:
|
||||
temporary_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as error:
|
||||
print(f"armature-parent-set-desktop-failed: {error}")
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user