#!/usr/bin/env python3 import hashlib import json import os import pathlib import shutil import subprocess import sys import tempfile import bpy OBJECT_NAME = "WebGapAnimKeyingSetButtonAddObject" ACTION_NAME = "WebGapAnimKeyingSetButtonAddAction" KEYING_SET_NAME = "ButtonKeyingSet" class KeyingSetButtonExperimentPanel(bpy.types.Panel): bl_label = "Keying Set Button Experiment" bl_idname = "WEBGAP_PT_keying_set_button_experiment" bl_space_type = "PROPERTIES" bl_region_type = "WINDOW" bl_context = "object" def draw(self, context): if context.object is not None: self.layout.prop(context.object, '["button_target"]', text="button_target") def action_report(obj): action = obj.animation_data.action if obj and obj.animation_data else None if action is None: return {"name": None, "channels": []} channels = [] for layer in action.layers: for strip in layer.strips: for bag in strip.channelbags: for curve in bag.fcurves: channels.append({"path": curve.data_path, "index": curve.array_index, "frames": [round(float(key.co.x), 6) for key in curve.keyframe_points], "values": [round(float(key.co.y), 6) for key in curve.keyframe_points], "selected": [bool(key.select_control_point) for key in curve.keyframe_points]}) channels.sort(key=lambda value: (value["path"], value["index"])) return {"name": action.name, "channels": channels} def path_report(path): return {"dataPath": path.data_path, "arrayIndex": path.array_index, "idType": path.id_type, "group": path.group, "groupMethod": path.group_method, "useEntireArray": bool(path.use_entire_array)} def state_report(): scene = bpy.context.scene obj = bpy.data.objects.get(OBJECT_NAME) if obj is None: raise RuntimeError("keyingset_button_add fixture object is missing") active = scene.keying_sets.active paths = list(active.paths) if active else [] return {"selected": bool(obj.select_get()), "active": bpy.context.view_layer.objects.active == obj, "value": round(float(obj["button_target"]), 6), "keyingSetCount": len(scene.keying_sets), "activeKeyingSet": active.bl_idname if active else None, "pathCount": len(paths), "paths": [path_report(path) for path in paths], "action": action_report(obj)} def write_report(fixture, output, before, after, evidence_status=None): report = {"schemaVersion": 1, "task": "M16-GAP-00196", "operation": "ANIM_KEYINGSET_BUTTON_ADD_DESKTOP", "fixture": str(fixture), "fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "before": before, "after": after, "poll": True, "operatorStatus": "FINISHED", "mainMutation": "KEYING_SET_PATH_ADDED", "saveReopen": "EXACT", "blenderVersion": bpy.app.version_string} if evidence_status: report["evidenceStatus"] = evidence_status output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8") def preserved_report(fixture, output, current): descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-keyingset-button-add-preserved-", 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 != current: raise RuntimeError("anim.keyingset_button_add preserved fixture save/reopen drift") if not output.exists(): write_report(fixture, output, current, reopened, evidence_status="PRESERVED") print("anim-keyingset-button-add-desktop-ok preserved=exact status=FINISHED mainMutation=keying_set_path_added saveReopen=exact") finally: temporary_path.unlink(missing_ok=True) def foreground_button(fixture, output, before): bpy.utils.register_class(KeyingSetButtonExperimentPanel) state = {"started": False} def blender_window(): windows = subprocess.check_output(["xdotool", "search", "--name", "Blender"], text=True).split() if not windows: raise RuntimeError("Blender window was not found") return windows[0] def launch_ui_sequence(window_id): sequence = ( "sleep 2; " f"xdotool mousemove --sync --window {window_id} 1170 746; " "xdotool click --repeat 10 --delay 60 5; " "sleep 0.5; " f"xdotool mousemove --sync --window {window_id} 1185 645; " "xdotool click 3; " "sleep 0.5; " f"xdotool mousemove --sync --window {window_id} 1120 585; " "xdotool click 1; " "xdotool key Return" ) subprocess.Popen(["sh", "-c", sequence], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) def finish_failure(message): print(f"anim-keyingset-button-add-desktop-failed: {message}") bpy.ops.wm.quit_blender() return None def poll_result(): after = state_report() if after["keyingSetCount"] == 0 or after["pathCount"] == 0: return 0.25 descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-keyingset-button-add-", 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: return finish_failure("anim.keyingset_button_add save/reopen drift") shutil.copyfile(temporary_path, fixture) write_report(fixture, output, before, reopened) print("anim-keyingset-button-add-desktop-ok poll=true status=FINISHED mainMutation=keying_set_path_added saveReopen=exact") bpy.ops.wm.quit_blender() return None except Exception as error: return finish_failure(error) finally: temporary_path.unlink(missing_ok=True) def drive_button(): if state["started"]: return 0.25 window = bpy.context.window area = next((candidate for candidate in window.screen.areas if candidate.type == "PROPERTIES"), None) if window else None if area is None: return 0.25 area.spaces.active.context = "OBJECT" state["started"] = True try: bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1) launch_ui_sequence(blender_window()) except Exception as error: return finish_failure(f"UI automation failed: {error}") bpy.app.timers.register(poll_result, first_interval=0.5) return None def timeout(): if state_report() == before: return finish_failure("UI keyingset_button_add timed out without mutation") return None bpy.app.timers.register(drive_button, first_interval=1.5) bpy.app.timers.register(timeout, first_interval=30.0) def main(): arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 2: raise SystemExit("usage: blender --factory-startup --python check-action-keyingset-button-add-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() if before["selected"] is not True or before["active"] is not True or before["value"] != 3.0: raise RuntimeError(f"unexpected keyingset_button_add source state: {before}") if before["keyingSetCount"] == 1: if before["activeKeyingSet"] != KEYING_SET_NAME or before["pathCount"] != 1 or before["paths"][0]["dataPath"] != '["button_target"]': raise RuntimeError(f"unexpected keyingset_button_add post state: {before}") preserved_report(fixture, output, before) bpy.ops.wm.quit_blender() return if before["keyingSetCount"] != 0 or before["pathCount"] != 0: raise RuntimeError(f"unexpected keyingset_button_add source Keying Set state: {before}") if before["action"]["name"] != ACTION_NAME or len(before["action"]["channels"]) != 1 or before["action"]["channels"][0]["frames"] != [1.0, 3.0, 5.0]: raise RuntimeError(f"unexpected keyingset_button_add source action: {before}") if bpy.app.background: raise RuntimeError("keyingset_button_add requires a foreground PROPERTIES context") output.unlink(missing_ok=True) foreground_button(fixture, output, before) if __name__ == "__main__": try: main() except Exception as error: print(f"anim-keyingset-button-add-desktop-failed: {error}") raise SystemExit(1)