260 lines
10 KiB
Python
260 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
class DriverButtonExperimentPanel(bpy.types.Panel):
|
|
bl_label = "Driver Button Experiment"
|
|
bl_idname = "WEBGAP_PT_driver_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, '["drive_target"]', text="drive_target")
|
|
|
|
|
|
def driver_report(obj):
|
|
if obj is None or obj.animation_data is None:
|
|
return []
|
|
drivers = []
|
|
for curve in obj.animation_data.drivers:
|
|
driver = curve.driver
|
|
drivers.append(
|
|
{
|
|
"path": curve.data_path,
|
|
"index": curve.array_index,
|
|
"expression": driver.expression if driver else "",
|
|
"type": driver.type if driver else "",
|
|
"variableCount": len(driver.variables) if driver else 0,
|
|
}
|
|
)
|
|
drivers.sort(key=lambda value: (value["path"], value["index"]))
|
|
return drivers
|
|
|
|
|
|
def state_report(obj):
|
|
if obj is None:
|
|
raise RuntimeError("driver-button-add fixture object is missing")
|
|
return {"drivers": driver_report(obj), "value": round(float(obj["drive_target"]), 6)}
|
|
|
|
|
|
def foreground_driver_button(fixture, output, obj, before):
|
|
bpy.utils.register_class(DriverButtonExperimentPanel)
|
|
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} 1080 537; "
|
|
"xdotool click 1; "
|
|
"xdotool key Return"
|
|
)
|
|
subprocess.Popen(["sh", "-c", sequence], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
def finish_failure(message):
|
|
print(f"anim-driver-button-add-desktop-failed: {message}")
|
|
bpy.ops.wm.quit_blender()
|
|
return None
|
|
|
|
def poll_result():
|
|
current_obj = bpy.data.objects.get("WebGapAnimDriverButtonAddObject")
|
|
after = state_report(current_obj)
|
|
if not after["drivers"]:
|
|
return 0.25
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-driver-button-add-ui-", 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(bpy.data.objects.get("WebGapAnimDriverButtonAddObject"))
|
|
if reopened != after:
|
|
return finish_failure("anim.driver_button_add save/reopen drift")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00174",
|
|
"operation": "ANIM_DRIVER_BUTTON_ADD_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"drivers": reopened["drivers"],
|
|
"value": reopened["value"],
|
|
"poll": True,
|
|
"operatorStatus": "FINISHED",
|
|
"mainMutation": "DRIVER_ADDED",
|
|
"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("anim-driver-button-add-desktop-ok poll=true status=FINISHED mainMutation=driver_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
|
|
if window is None:
|
|
return 0.25
|
|
area = next((candidate for candidate in window.screen.areas if candidate.type == "PROPERTIES"), 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():
|
|
current_obj = bpy.data.objects.get("WebGapAnimDriverButtonAddObject")
|
|
if current_obj is None or state_report(current_obj) == before:
|
|
return finish_failure("UI driver_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 preserved_driver_report(fixture, output, obj, current):
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-driver-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(bpy.data.objects.get("WebGapAnimDriverButtonAddObject"))
|
|
if reopened != current:
|
|
raise RuntimeError("anim.driver_button_add preserved fixture save/reopen drift")
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00174",
|
|
"operation": "ANIM_DRIVER_BUTTON_ADD_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"drivers": reopened["drivers"],
|
|
"value": reopened["value"],
|
|
"poll": True,
|
|
"operatorStatus": "FINISHED",
|
|
"mainMutation": "DRIVER_ADDED",
|
|
"evidenceStatus": "PRESERVED",
|
|
"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("anim-driver-button-add-desktop-ok preserved=exact status=FINISHED mainMutation=driver_added saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[sys.argv.index("--") + 1 :]
|
|
if len(arguments) != 2:
|
|
raise SystemExit(
|
|
"usage: blender -b --python check-action-driver-button-add-desktop.py -- FIXTURE REPORT"
|
|
)
|
|
fixture, output = (pathlib.Path(value).resolve() for value in arguments)
|
|
output.unlink(missing_ok=True)
|
|
bpy.ops.wm.open_mainfile(filepath=str(fixture), load_ui=False)
|
|
obj = bpy.data.objects.get("WebGapAnimDriverButtonAddObject")
|
|
before = state_report(obj)
|
|
expected_driver = [{
|
|
"path": '["drive_target"]',
|
|
"index": 0,
|
|
"expression": "var + 4.5",
|
|
"type": "SCRIPTED",
|
|
"variableCount": 1,
|
|
}]
|
|
if before["value"] != 4.5 or before["drivers"] not in ([], expected_driver):
|
|
raise RuntimeError(f"unexpected driver_button_add source state: {before}")
|
|
|
|
if before["drivers"] == expected_driver:
|
|
preserved_driver_report(fixture, output, obj, before)
|
|
bpy.ops.wm.quit_blender()
|
|
return
|
|
|
|
if not bpy.app.background:
|
|
foreground_driver_button(fixture, output, obj, before)
|
|
return
|
|
|
|
window = bpy.context.window
|
|
screen = window.screen
|
|
area = next(candidate for candidate in screen.areas if candidate.type == "PROPERTIES")
|
|
region = next(candidate for candidate in area.regions if candidate.type == "WINDOW")
|
|
with bpy.context.temp_override(window=window, screen=screen, area=area, region=region):
|
|
poll = bool(bpy.ops.anim.driver_button_add.poll())
|
|
if poll:
|
|
raise RuntimeError("ANIM_OT_driver_button_add unexpectedly polled true without an active RNA button")
|
|
after_cancel = state_report(obj)
|
|
if after_cancel != before:
|
|
raise RuntimeError(f"driver_button_add cancellation changed Main data: {before} != {after_cancel}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-anim-driver-button-add-reopen-", suffix=".blend")
|
|
os.close(descriptor)
|
|
pathlib.Path(temporary).unlink(missing_ok=True)
|
|
try:
|
|
bpy.ops.wm.save_as_mainfile(filepath=temporary, check_existing=False, compress=True)
|
|
bpy.ops.wm.open_mainfile(filepath=temporary, load_ui=False)
|
|
after = state_report(bpy.data.objects.get("WebGapAnimDriverButtonAddObject"))
|
|
finally:
|
|
pathlib.Path(temporary).unlink(missing_ok=True)
|
|
if before != after:
|
|
raise RuntimeError("anim.driver_button_add save/reopen drift")
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00174",
|
|
"operation": "ANIM_DRIVER_BUTTON_ADD_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"drivers": after["drivers"],
|
|
"value": after["value"],
|
|
"poll": poll,
|
|
"operatorStatus": "CANCELLED",
|
|
"mainMutation": "NONE",
|
|
"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("anim-driver-button-add-desktop-ok poll=false status=CANCELLED mainMutation=none saveReopen=exact")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"anim-driver-button-add-desktop-failed: {error}")
|
|
raise SystemExit(1)
|