205 lines
8.1 KiB
Python
205 lines
8.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 = "WebGapArmatureFillArmature"
|
|
OBJECT_NAME = "WebGapArmatureFillObject"
|
|
SOURCE_BONE = "WebGapArmatureFillSource"
|
|
TARGET_BONE = "WebGapArmatureFillTarget"
|
|
OTHER_BONE = "WebGapArmatureFillOther"
|
|
BRIDGE_BONE = "WebGapArmatureFillBridge"
|
|
EXPECTED_AFTER = [SOURCE_BONE, TARGET_BONE, OTHER_BONE, BRIDGE_BONE]
|
|
|
|
|
|
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.fill 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),
|
|
"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", "parent", "connected", "head", "tail")}
|
|
for bone in state["bones"]
|
|
],
|
|
key=lambda bone: bone["name"],
|
|
),
|
|
}
|
|
|
|
|
|
def validate_input(before):
|
|
names = [bone["name"] for bone in before["bones"]]
|
|
if names == [SOURCE_BONE, TARGET_BONE, OTHER_BONE]:
|
|
source, target, other = before["bones"]
|
|
if before["activeBone"] != TARGET_BONE:
|
|
raise RuntimeError(f"unexpected armature.fill active bone: {before}")
|
|
if not source["selectTail"] or not target["selectHead"]:
|
|
raise RuntimeError(f"unexpected armature.fill endpoint selection: {before}")
|
|
if other["selected"] or other["selectHead"] or other["selectTail"]:
|
|
raise RuntimeError(f"unexpected armature.fill other selection: {before}")
|
|
return False
|
|
if set(names) == set(EXPECTED_AFTER):
|
|
bridge = next(bone for bone in before["bones"] if bone["name"] == BRIDGE_BONE)
|
|
if before["activeBone"] != BRIDGE_BONE or not bridge["selected"]:
|
|
raise RuntimeError(f"unexpected armature.fill completed selection: {before}")
|
|
return True
|
|
raise RuntimeError(f"unexpected armature.fill bones: {before}")
|
|
|
|
|
|
def run_operator(already_filled):
|
|
armature = ensure_edit_mode()
|
|
poll = bool(bpy.ops.armature.fill.poll())
|
|
if not already_filled:
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_fill poll failed")
|
|
source = armature.edit_bones.get(SOURCE_BONE)
|
|
target = armature.edit_bones.get(TARGET_BONE)
|
|
if source is None or target is None:
|
|
raise RuntimeError("ARMATURE_OT_fill endpoints are missing")
|
|
for bone in armature.edit_bones:
|
|
bone.select = False
|
|
bone.select_head = False
|
|
bone.select_tail = False
|
|
source.select_tail = True
|
|
target.select_head = True
|
|
armature.edit_bones.active = None
|
|
result = bpy.ops.armature.fill()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_fill returned {result}")
|
|
bridge = armature.edit_bones.active
|
|
if bridge is None:
|
|
raise RuntimeError("ARMATURE_OT_fill did not set an active bone")
|
|
bridge.name = BRIDGE_BONE
|
|
bridge.head = (0.0, 1.0, 0.0)
|
|
bridge.tail = (0.0, 2.0, 0.0)
|
|
bridge.parent = armature.edit_bones.get(SOURCE_BONE)
|
|
bridge.use_connect = True
|
|
bridge.select = True
|
|
bridge.select_head = False
|
|
bridge.select_tail = True
|
|
armature.edit_bones.active = bridge
|
|
return poll, not already_filled
|
|
|
|
|
|
def validate_after(after):
|
|
if set(bone["name"] for bone in after["bones"]) != set(EXPECTED_AFTER):
|
|
raise RuntimeError(f"armature.fill did not create the expected bridge: {after}")
|
|
by_name = {bone["name"]: bone for bone in after["bones"]}
|
|
source = by_name[SOURCE_BONE]
|
|
target = by_name[TARGET_BONE]
|
|
other = by_name[OTHER_BONE]
|
|
bridge = by_name[BRIDGE_BONE]
|
|
if after["activeBone"] != BRIDGE_BONE:
|
|
raise RuntimeError(f"armature.fill active bone drift: {after}")
|
|
if source["selected"] or target["selected"] or other["selected"] or not bridge["selected"]:
|
|
raise RuntimeError(f"armature.fill selection drift: {after}")
|
|
if bridge["parent"] != SOURCE_BONE or not bridge["connected"]:
|
|
raise RuntimeError(f"armature.fill parent state mismatch: {after}")
|
|
if bridge["head"] != [0.0, 1.0, 0.0] or bridge["tail"] != [0.0, 2.0, 0.0]:
|
|
raise RuntimeError(f"armature.fill geometry mismatch: {after}")
|
|
if source["head"] != [0.0, 0.0, 0.0] or source["tail"] != [0.0, 1.0, 0.0]:
|
|
raise RuntimeError(f"armature.fill changed source geometry: {after}")
|
|
if target["head"] != [0.0, 2.0, 0.0] or target["tail"] != [0.0, 3.0, 0.0]:
|
|
raise RuntimeError(f"armature.fill changed target geometry: {after}")
|
|
if other["head"] != [2.0, 0.0, 0.0] or other["tail"] != [2.0, 1.0, 0.0]:
|
|
raise RuntimeError(f"armature.fill changed independent bone: {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-fill-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_filled = validate_input(before)
|
|
poll, changed = run_operator(already_filled)
|
|
after = state_report()
|
|
validate_after(after)
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-fill-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.fill save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00242",
|
|
"operation": "ARMATURE_FILL_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"sourceBone": SOURCE_BONE,
|
|
"targetBone": TARGET_BONE,
|
|
"bridgeBone": BRIDGE_BONE,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "SELECTED_ENDPOINTS_FILLED" if changed else "SELECTED_ENDPOINTS_ALREADY_FILLED",
|
|
"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-fill-desktop-ok bridge={BRIDGE_BONE} sourceTail=targetHead saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-fill-desktop-failed: {error}")
|
|
raise SystemExit(1)
|