154 lines
6.1 KiB
Python
154 lines
6.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 = "WebGapArmatureDissolveArmature"
|
|
OBJECT_NAME = "WebGapArmatureDissolveObject"
|
|
ROOT_BONE = "WebGapArmatureDissolveRoot"
|
|
MIDDLE_BONE = "WebGapArmatureDissolveMiddle"
|
|
TIP_BONE = "WebGapArmatureDissolveTip"
|
|
OTHER_BONE = "WebGapArmatureDissolveOther"
|
|
EXPECTED_AFTER = [ROOT_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.dissolve 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),
|
|
"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 run_operator(already_dissolved):
|
|
ensure_edit_mode()
|
|
poll = bool(bpy.ops.armature.dissolve.poll())
|
|
if not already_dissolved:
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_dissolve poll failed")
|
|
result = bpy.ops.armature.dissolve()
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_dissolve returned {result}")
|
|
return poll, not already_dissolved
|
|
|
|
|
|
def validate_input(before):
|
|
names = [bone["name"] for bone in before["bones"]]
|
|
if names == [ROOT_BONE, MIDDLE_BONE, TIP_BONE, OTHER_BONE]:
|
|
if before["activeBone"] != MIDDLE_BONE:
|
|
raise RuntimeError(f"unexpected armature.dissolve active bone: {before}")
|
|
selected = [bone["name"] for bone in before["bones"] if bone["selected"]]
|
|
if selected != [MIDDLE_BONE, TIP_BONE]:
|
|
raise RuntimeError(f"unexpected armature.dissolve selection: {before}")
|
|
if not all(bone["connected"] for bone in before["bones"][1:3]):
|
|
raise RuntimeError(f"unexpected armature.dissolve connections: {before}")
|
|
return False
|
|
if names == EXPECTED_AFTER:
|
|
if before["activeBone"] is not None:
|
|
raise RuntimeError(f"unexpected armature.dissolve completed active bone: {before}")
|
|
if any(bone["selected"] for bone in before["bones"]):
|
|
raise RuntimeError(f"unexpected armature.dissolve completed selection: {before}")
|
|
return True
|
|
raise RuntimeError(f"unexpected armature.dissolve bones: {before}")
|
|
|
|
|
|
def validate_after(after):
|
|
if [bone["name"] for bone in after["bones"]] != EXPECTED_AFTER:
|
|
raise RuntimeError(f"armature.dissolve did not remove the selected tip: {after}")
|
|
root, other = after["bones"]
|
|
if after["activeBone"] is not None or any(bone["selected"] for bone in after["bones"]):
|
|
raise RuntimeError(f"armature.dissolve left unexpected active or selection state: {after}")
|
|
if root["parent"] is not None or other["parent"] is not None or other["connected"]:
|
|
raise RuntimeError(f"armature.dissolve changed unexpected parent state: {after}")
|
|
if root["head"] != [0.0, 0.0, 0.0] or root["tail"] != [0.0, 3.0, 0.0]:
|
|
raise RuntimeError(f"armature.dissolve changed root geometry: {after}")
|
|
if other["head"] != [2.0, 0.0, 0.0] or other["tail"] != [2.0, 1.0, 0.0]:
|
|
raise RuntimeError(f"armature.dissolve changed the 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-dissolve-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_dissolved = validate_input(before)
|
|
poll, changed = run_operator(already_dissolved)
|
|
after = state_report()
|
|
validate_after(after)
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-dissolve-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.dissolve save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00235",
|
|
"operation": "ARMATURE_DISSOLVE_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"dissolvedBone": TIP_BONE,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "CONNECTED_TIP_DISSOLVED" if changed else "CONNECTED_TIP_ALREADY_DISSOLVED",
|
|
"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-dissolve-desktop-ok dissolved={TIP_BONE} saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-dissolve-desktop-failed: {error}")
|
|
raise SystemExit(1)
|