Continue Blender Web parity task handoff
This commit is contained in:
151
tools/web/check-action-armature-select-similar-desktop.py
Normal file
151
tools/web/check-action-armature-select-similar-desktop.py
Normal file
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env python3
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import bpy
|
||||
|
||||
|
||||
ARMATURE_NAME = "WebGapArmatureSelectSimilarArmature"
|
||||
OBJECT_NAME = "WebGapArmatureSelectSimilarObject"
|
||||
ACTIVE_BONE = "WebGapArmatureSelectSimilarActive"
|
||||
SIMILAR_BONE = "WebGapArmatureSelectSimilarSameLength"
|
||||
DIFFERENT_BONE = "WebGapArmatureSelectSimilarDifferentLength"
|
||||
|
||||
|
||||
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_similar 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),
|
||||
"length": round(float(bone.length), 6),
|
||||
}
|
||||
|
||||
|
||||
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", "length")}
|
||||
for bone in state["bones"]
|
||||
],
|
||||
key=lambda bone: bone["name"],
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def validate_input(before):
|
||||
by_name = {bone["name"]: bone for bone in before["bones"]}
|
||||
expected = {ACTIVE_BONE, SIMILAR_BONE, DIFFERENT_BONE}
|
||||
if set(by_name) != expected or before["activeBone"] != ACTIVE_BONE:
|
||||
raise RuntimeError(f"unexpected armature.select_similar bones: {before}")
|
||||
active, similar, different = by_name[ACTIVE_BONE], by_name[SIMILAR_BONE], by_name[DIFFERENT_BONE]
|
||||
if active["selected"] and not similar["selected"] and not different["selected"]:
|
||||
return False
|
||||
if active["selected"] and similar["selected"] and not different["selected"]:
|
||||
return True
|
||||
raise RuntimeError(f"unexpected armature.select_similar state: {before}")
|
||||
|
||||
|
||||
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-similar-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_applied = validate_input(before)
|
||||
ensure_edit_mode()
|
||||
poll = bool(bpy.ops.armature.select_similar.poll())
|
||||
if not poll:
|
||||
raise RuntimeError("ARMATURE_OT_select_similar poll failed")
|
||||
if not already_applied:
|
||||
result = bpy.ops.armature.select_similar(type="LENGTH", threshold=0.1)
|
||||
if result != {"FINISHED"}:
|
||||
raise RuntimeError(f"ARMATURE_OT_select_similar returned {result}")
|
||||
after = state_report()
|
||||
by_name = {bone["name"]: bone for bone in after["bones"]}
|
||||
if not by_name[ACTIVE_BONE]["selected"] or not by_name[SIMILAR_BONE]["selected"] or by_name[DIFFERENT_BONE]["selected"]:
|
||||
raise RuntimeError(f"armature.select_similar length mismatch: {after}")
|
||||
bpy.ops.object.mode_set(mode="OBJECT")
|
||||
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-select-similar-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_similar save/reopen drift: {after} != {reopened}")
|
||||
shutil.copyfile(temporary_path, fixture)
|
||||
report = {
|
||||
"schemaVersion": 1,
|
||||
"task": "M16-GAP-00257",
|
||||
"operation": "ARMATURE_SELECT_SIMILAR_DESKTOP",
|
||||
"fixture": str(fixture),
|
||||
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
||||
"before": before,
|
||||
"after": reopened,
|
||||
"type": "LENGTH",
|
||||
"threshold": 0.1,
|
||||
"activeBone": ACTIVE_BONE,
|
||||
"similarBone": SIMILAR_BONE,
|
||||
"differentBone": DIFFERENT_BONE,
|
||||
"operatorStatus": "FINISHED" if not already_applied else "SKIPPED_ALREADY_APPLIED",
|
||||
"mainMutation": "SIMILAR_LENGTH_SELECTED" if not already_applied else "SIMILAR_LENGTH_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-similar-desktop-ok type=length same=1 different=2 saveReopen=exact")
|
||||
finally:
|
||||
temporary_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as error:
|
||||
print(f"armature-select-similar-desktop-failed: {error}")
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user