161 lines
5.8 KiB
Python
161 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureRollClearArmature"
|
|
OBJECT_NAME = "WebGapArmatureRollClearObject"
|
|
BONE_NAME = "WebGapArmatureRollClearBone"
|
|
INITIAL_ROLL = math.pi / 4.0
|
|
TARGET_ROLL = 0.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 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.roll_clear 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 state_report():
|
|
armature = ensure_edit_mode()
|
|
bone = armature.edit_bones.get(BONE_NAME)
|
|
if bone is None:
|
|
raise RuntimeError("armature.roll_clear bone is missing")
|
|
return {
|
|
"object": OBJECT_NAME,
|
|
"armature": ARMATURE_NAME,
|
|
"activeBone": armature.edit_bones.active.name if armature.edit_bones.active else None,
|
|
"bones": [{
|
|
"name": bone.name,
|
|
"roll": round(float(bone.roll), 6),
|
|
"selected": bool(bone.select),
|
|
"hidden": bool(bone.hide),
|
|
"head": vector(bone.head),
|
|
"tail": vector(bone.tail),
|
|
"matrix": matrix_flat(bone.matrix),
|
|
}],
|
|
}
|
|
|
|
|
|
def stable_state(state):
|
|
return {
|
|
"object": state["object"],
|
|
"armature": state["armature"],
|
|
"activeBone": state["activeBone"],
|
|
"bones": [{key: state["bones"][0][key] for key in ("name", "roll", "selected", "hidden", "head", "tail", "matrix")}],
|
|
}
|
|
|
|
|
|
def validate_input(before):
|
|
if len(before["bones"]) != 1 or before["bones"][0]["name"] != BONE_NAME:
|
|
raise RuntimeError(f"unexpected armature.roll_clear bones: {before}")
|
|
bone = before["bones"][0]
|
|
if abs(bone["roll"] - INITIAL_ROLL) <= 1e-5 and bone["selected"] and not bone["hidden"]:
|
|
return False
|
|
if abs(bone["roll"] - TARGET_ROLL) <= 1e-5 and bone["selected"] and not bone["hidden"]:
|
|
return True
|
|
raise RuntimeError(f"unexpected armature.roll_clear state: {before}")
|
|
|
|
|
|
def run_operator(already_cleared):
|
|
armature = ensure_edit_mode()
|
|
bone = armature.edit_bones[BONE_NAME]
|
|
bone.select = True
|
|
armature.edit_bones.active = bone
|
|
poll = bool(bpy.ops.armature.roll_clear.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_roll_clear poll failed")
|
|
if not already_cleared:
|
|
result = bpy.ops.armature.roll_clear(roll=TARGET_ROLL)
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_roll_clear returned {result}")
|
|
roll = float(bone.roll)
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
return poll, not already_cleared, roll
|
|
|
|
|
|
def validate_after(after, roll):
|
|
bone = after["bones"][0]
|
|
if abs(roll - TARGET_ROLL) > 1e-5 or abs(bone["roll"] - TARGET_ROLL) > 1e-5:
|
|
raise RuntimeError(f"armature.roll_clear did not clear roll: {after}")
|
|
if not bone["selected"] or bone["hidden"]:
|
|
raise RuntimeError(f"armature.roll_clear changed selection/visibility: {after}")
|
|
if bone["head"] != [0.0, 0.0, 0.0] or bone["tail"] != [0.0, 2.0, 0.0]:
|
|
raise RuntimeError(f"armature.roll_clear changed geometry: {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-roll-clear-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_cleared = validate_input(before)
|
|
poll, changed, roll = run_operator(already_cleared)
|
|
after = state_report()
|
|
validate_after(after, roll)
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-roll-clear-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.roll_clear save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00249",
|
|
"operation": "ARMATURE_ROLL_CLEAR_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"bone": BONE_NAME,
|
|
"roll": round(roll, 6),
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "ROLL_CLEARED_TO_ZERO" if changed else "ROLL_ALREADY_ZERO",
|
|
"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-roll-clear-desktop-ok bone={BONE_NAME} roll=0 saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-roll-clear-desktop-failed: {error}")
|
|
raise SystemExit(1)
|