#!/usr/bin/env python3 import hashlib import json import math import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureCalculateRollArmature" OBJECT_NAME = "WebGapArmatureCalculateRollObject" BONE_NAME = "WebGapArmatureCalculateRollBone" EXPECTED_ROLL = math.pi / 2.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.calculate_roll fixture is missing") bones = [ { "name": bone.name, "head": vector(bone.head_local), "tail": vector(bone.tail_local), "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, "bones": bones} def run_operator(before): if [bone["name"] for bone in before["bones"]] != [BONE_NAME]: raise RuntimeError(f"unexpected calculate_roll 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] already_calculated = abs(abs(edit_bone.roll) - EXPECTED_ROLL) <= 1e-5 edit_bone.select = True armature.bones.active = armature.bones[BONE_NAME] poll = bool(bpy.ops.armature.calculate_roll.poll()) if not poll: raise RuntimeError("ARMATURE_OT_calculate_roll poll failed") if not already_calculated: result = bpy.ops.armature.calculate_roll(type="GLOBAL_POS_X", axis_flip=False, axis_only=False) if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_calculate_roll returned {result}") roll = float(edit_bone.roll) bpy.ops.object.mode_set(mode="OBJECT") return poll, not already_calculated, roll def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-calculate-roll-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, roll = run_operator(before) after = state_report() if len(after["bones"]) != 1 or after["bones"][0]["name"] != BONE_NAME: raise RuntimeError(f"calculate_roll changed unexpected bones: {before} -> {after}") if abs(abs(roll) - EXPECTED_ROLL) > 1e-5: raise RuntimeError(f"calculate_roll did not produce the expected roll: {roll}") expected_matrix = [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] for actual, expected in zip(after["bones"][0]["matrix"], expected_matrix): if abs(actual - expected) > 1e-5: raise RuntimeError(f"calculate_roll matrix mismatch: {after}") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-calculate-roll-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.calculate_roll save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00219", "operation": "ARMATURE_CALCULATE_ROLL_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "alreadyCalculated": not changed, "roll": round(roll, 6), "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "ROLL_CALCULATED_GLOBAL_POS_X" if changed else "ALREADY_ROLL_CALCULATED_GLOBAL_POS_X", "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-calculate-roll-desktop-ok poll=true roll=global-pos-x saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-calculate-roll-desktop-failed: {error}") raise SystemExit(1)