190 lines
7.5 KiB
Python
190 lines
7.5 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureExtrudeArmature"
|
|
OBJECT_NAME = "WebGapArmatureExtrudeObject"
|
|
SOURCE_BONE = "WebGapArmatureExtrudeSource"
|
|
OTHER_BONE = "WebGapArmatureExtrudeOther"
|
|
EXTRUDE_BONE = "WebGapArmatureExtrudeSource.001"
|
|
EXPECTED_AFTER = [SOURCE_BONE, EXTRUDE_BONE, OTHER_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.extrude 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, OTHER_BONE]:
|
|
source, other = before["bones"]
|
|
if before["activeBone"] != SOURCE_BONE:
|
|
raise RuntimeError(f"unexpected armature.extrude active bone: {before}")
|
|
if not source["selected"] or not source["selectTail"]:
|
|
raise RuntimeError(f"unexpected armature.extrude source selection: {before}")
|
|
if other["selected"] or other["selectHead"] or other["selectTail"]:
|
|
raise RuntimeError(f"unexpected armature.extrude other selection: {before}")
|
|
return False
|
|
if set(names) == set(EXPECTED_AFTER):
|
|
extrude = next(bone for bone in before["bones"] if bone["name"] == EXTRUDE_BONE)
|
|
if before["activeBone"] != EXTRUDE_BONE or not extrude["selected"]:
|
|
raise RuntimeError(f"unexpected armature.extrude completed selection: {before}")
|
|
return True
|
|
raise RuntimeError(f"unexpected armature.extrude bones: {before}")
|
|
|
|
|
|
def run_operator(already_extruded):
|
|
armature = ensure_edit_mode()
|
|
poll = bool(bpy.ops.armature.extrude.poll())
|
|
if not already_extruded:
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_extrude poll failed")
|
|
result = bpy.ops.armature.extrude()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_extrude returned {result}")
|
|
extrude = armature.edit_bones.active
|
|
if extrude is None or extrude.name != EXTRUDE_BONE:
|
|
raise RuntimeError(f"ARMATURE_OT_extrude active bone mismatch: {extrude}")
|
|
extrude.head = (0.0, 1.0, 0.0)
|
|
extrude.tail = (0.0, 2.0, 0.0)
|
|
extrude.select = True
|
|
extrude.select_head = False
|
|
extrude.select_tail = True
|
|
armature.edit_bones.active = extrude
|
|
return poll, not already_extruded
|
|
|
|
|
|
def validate_after(after):
|
|
if set(bone["name"] for bone in after["bones"]) != set(EXPECTED_AFTER):
|
|
raise RuntimeError(f"armature.extrude did not create the expected bone: {after}")
|
|
by_name = {bone["name"]: bone for bone in after["bones"]}
|
|
source = by_name[SOURCE_BONE]
|
|
extrude = by_name[EXTRUDE_BONE]
|
|
other = by_name[OTHER_BONE]
|
|
if after["activeBone"] != EXTRUDE_BONE:
|
|
raise RuntimeError(f"armature.extrude active bone drift: {after}")
|
|
if source["selected"] or other["selected"] or not extrude["selected"]:
|
|
raise RuntimeError(f"armature.extrude selection drift: {after}")
|
|
if extrude["parent"] != SOURCE_BONE or not extrude["connected"]:
|
|
raise RuntimeError(f"armature.extrude parent state mismatch: {after}")
|
|
if extrude["head"] != [0.0, 1.0, 0.0] or extrude["tail"] != [0.0, 2.0, 0.0]:
|
|
raise RuntimeError(f"armature.extrude 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.extrude changed source geometry: {after}")
|
|
if other["head"] != [2.0, 0.0, 0.0] or other["tail"] != [2.0, 1.0, 0.0]:
|
|
raise RuntimeError(f"armature.extrude 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-extrude-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_extruded = validate_input(before)
|
|
poll, changed = run_operator(already_extruded)
|
|
after = state_report()
|
|
validate_after(after)
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-extrude-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.extrude save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00239",
|
|
"operation": "ARMATURE_EXTRUDE_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"sourceBone": SOURCE_BONE,
|
|
"extrudeBone": EXTRUDE_BONE,
|
|
"translation": [0.0, 1.0, 0.0],
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "SELECTED_TAIL_EXTRUDED" if changed else "SELECTED_TAIL_ALREADY_EXTRUDED",
|
|
"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-extrude-desktop-ok extrude={EXTRUDE_BONE} translation=0,1,0 saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-extrude-desktop-failed: {error}")
|
|
raise SystemExit(1)
|