#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import sys import tempfile import bpy ARMATURE_NAME = "WebGapArmatureDuplicateMoveArmature" OBJECT_NAME = "WebGapArmatureDuplicateMoveObject" SOURCE_BONE = "WebGapArmatureDuplicateMoveSource" OTHER_BONE = "WebGapArmatureDuplicateMoveOther" DUPLICATE_BONE = "WebGapArmatureDuplicateMoveSource.001" MOVE = [1.0, 2.0, 3.0] EXPECTED_AFTER = [SOURCE_BONE, OTHER_BONE, DUPLICATE_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.duplicate_move 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 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.duplicate_move active bone: {before}") if not source["selected"] or not source["selectHead"] or not source["selectTail"]: raise RuntimeError(f"unexpected armature.duplicate_move source selection: {before}") if other["selected"] or other["selectHead"] or other["selectTail"]: raise RuntimeError(f"unexpected armature.duplicate_move other selection: {before}") return False if names == EXPECTED_AFTER: duplicate = before["bones"][2] if before["activeBone"] != DUPLICATE_BONE or not duplicate["selected"]: raise RuntimeError(f"unexpected armature.duplicate_move completed selection: {before}") return True raise RuntimeError(f"unexpected armature.duplicate_move bones: {before}") def run_operator(already_moved): ensure_edit_mode() poll = bool(bpy.ops.armature.duplicate_move.poll()) if not already_moved: if not poll: raise RuntimeError("ARMATURE_OT_duplicate_move poll failed") result = bpy.ops.armature.duplicate_move( TRANSFORM_OT_translate={"value": tuple(MOVE)} ) if result != {"FINISHED"}: raise RuntimeError(f"ARMATURE_OT_duplicate_move returned {result}") return poll, not already_moved def validate_after(after): if [bone["name"] for bone in after["bones"]] != EXPECTED_AFTER: raise RuntimeError(f"armature.duplicate_move did not create the expected copy: {after}") source, other, duplicate = after["bones"] if after["activeBone"] != DUPLICATE_BONE: raise RuntimeError(f"armature.duplicate_move active bone drift: {after}") if source["selected"] or other["selected"] or not duplicate["selected"]: raise RuntimeError(f"armature.duplicate_move selection drift: {after}") if duplicate["parent"] is not None or duplicate["connected"]: raise RuntimeError(f"armature.duplicate_move changed duplicate parent state: {after}") if duplicate["head"] != [MOVE[index] for index in range(3)]: raise RuntimeError(f"armature.duplicate_move head translation mismatch: {after}") if duplicate["tail"] != [MOVE[0], MOVE[1] + 1.0, MOVE[2]]: raise RuntimeError(f"armature.duplicate_move tail translation mismatch: {after}") if source["head"] != [0.0, 0.0, 0.0] or source["tail"] != [0.0, 1.0, 0.0]: raise RuntimeError(f"armature.duplicate_move 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.duplicate_move 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-duplicate-move-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_moved = validate_input(before) poll, changed = run_operator(already_moved) after = state_report() validate_after(after) bpy.ops.object.mode_set(mode="OBJECT") descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-duplicate-move-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.duplicate_move save/reopen drift: {after} != {reopened}") shutil.copyfile(temporary_path, fixture) report = { "schemaVersion": 1, "task": "M16-GAP-00237", "operation": "ARMATURE_DUPLICATE_MOVE_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": reopened, "sourceBone": SOURCE_BONE, "duplicateBone": DUPLICATE_BONE, "translation": MOVE, "poll": poll, "operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED", "mainMutation": "SELECTED_BONE_DUPLICATED_AND_MOVED" if changed else "SELECTED_BONE_ALREADY_DUPLICATED_AND_MOVED", "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-duplicate-move-desktop-ok duplicate={DUPLICATE_BONE} translation={MOVE} saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) if __name__ == "__main__": try: main() except Exception as error: print(f"armature-duplicate-move-desktop-failed: {error}") raise SystemExit(1)