135 lines
5.3 KiB
Python
135 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureClickExtrudeArmature"
|
|
OBJECT_NAME = "WebGapArmatureClickExtrudeObject"
|
|
BONE_NAME = "WebGapArmatureClickExtrudeBone"
|
|
CHILD_NAME = f"{BONE_NAME}.001"
|
|
CURSOR = [1.5, 2.0, 1.0]
|
|
|
|
|
|
def vector(value):
|
|
return [round(float(component), 6) for component in value]
|
|
|
|
|
|
def matrix_flat(value):
|
|
return [round(float(component), 6) for column in value for component in column]
|
|
|
|
|
|
def state_report():
|
|
armature = bpy.data.armatures.get(ARMATURE_NAME)
|
|
obj = bpy.data.objects.get(OBJECT_NAME)
|
|
if armature is None or obj is None or obj.type != "ARMATURE" or obj.data != armature:
|
|
raise RuntimeError("armature.click_extrude fixture is missing")
|
|
bones = [
|
|
{
|
|
"name": bone.name,
|
|
"parent": bone.parent.name if bone.parent else None,
|
|
"head": vector(bone.head_local),
|
|
"tail": vector(bone.tail_local),
|
|
"headRaw": vector(bone.head),
|
|
"tailRaw": vector(bone.tail),
|
|
"useConnect": bool(bone.use_connect),
|
|
"matrix": matrix_flat(bone.matrix_local),
|
|
}
|
|
for bone in armature.bones
|
|
]
|
|
bones.sort(key=lambda value: value["name"])
|
|
return {"object": OBJECT_NAME, "armature": ARMATURE_NAME, "cursor": vector(bpy.context.scene.cursor.location), "bones": bones}
|
|
|
|
|
|
def run_operator(before):
|
|
names = {bone["name"] for bone in before["bones"]}
|
|
if names == {BONE_NAME, CHILD_NAME}:
|
|
already_extruded = True
|
|
elif names == {BONE_NAME}:
|
|
already_extruded = False
|
|
else:
|
|
raise RuntimeError(f"unexpected click_extrude input: {before}")
|
|
bpy.context.view_layer.objects.active = bpy.data.objects[OBJECT_NAME]
|
|
bpy.data.objects[OBJECT_NAME].select_set(True)
|
|
if bpy.context.object.mode != "EDIT":
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
armature = bpy.data.armatures[ARMATURE_NAME]
|
|
edit_bone = armature.edit_bones[BONE_NAME]
|
|
edit_bone.select = True
|
|
edit_bone.select_tail = True
|
|
armature.edit_bones.active = edit_bone
|
|
poll = bool(bpy.ops.armature.click_extrude.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_click_extrude poll failed")
|
|
if not already_extruded:
|
|
result = bpy.ops.armature.click_extrude()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_click_extrude returned {result}")
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
return poll, not already_extruded
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[sys.argv.index("--") + 1 :]
|
|
if len(arguments) != 2:
|
|
raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-click-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()
|
|
poll, changed = run_operator(before)
|
|
after = state_report()
|
|
if len(after["bones"]) != 2:
|
|
raise RuntimeError(f"click_extrude did not create exactly one child bone: {before} -> {after}")
|
|
parent = next((bone for bone in after["bones"] if bone["name"] == BONE_NAME), None)
|
|
child = next((bone for bone in after["bones"] if bone["name"] == CHILD_NAME), None)
|
|
if parent is None or child is None:
|
|
raise RuntimeError(f"click_extrude produced unexpected bones: {after}")
|
|
expected_head = parent["tail"]
|
|
if child["parent"] != BONE_NAME or not child["useConnect"] or child["head"] != expected_head or child["tail"] != CURSOR:
|
|
raise RuntimeError(f"click_extrude produced unexpected child: {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-click-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 reopened != after:
|
|
raise RuntimeError(f"armature.click_extrude save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00220",
|
|
"operation": "ARMATURE_CLICK_EXTRUDE_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"alreadyExtruded": not changed,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "CHILD_EXTRUDED_TO_CURSOR" if changed else "ALREADY_CHILD_EXTRUDED_TO_CURSOR",
|
|
"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("armature-click-extrude-desktop-ok poll=true child=cursor saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-click-extrude-desktop-failed: {error}")
|
|
raise SystemExit(1)
|