|
| 1 | +"""exit_pre post-exit sidecar — a runnable example. |
| 2 | +
|
| 3 | +Witnesses ``bpy.app.handlers.exit_pre`` from |
| 4 | +``skills/drivers-and-app-handlers``. The callback fires as Blender dies. |
| 5 | +This script does **not** write ``$BDT_SMOKE_SIDECAR`` in ``main``; the |
| 6 | +handler writes it. The host runner (``tests/smoke/run_example.py``) |
| 7 | +asserts the file after the process exits. |
| 8 | +
|
| 9 | +5.1+. 4.5 LTS has no ``exit_pre`` (AttributeError). Catalog |
| 10 | +``min_version`` 5.0 is wrong — the floor is 5.1. Skip: ``SMOKE_SKIP``. |
| 11 | +``--force-run`` bypasses the skip so 4.5 fails accessing ``exit_pre``. |
| 12 | +
|
| 13 | +No gallery still. There is no geometry. |
| 14 | +
|
| 15 | + blender --background --python exit_pre_sidecar.py -- |
| 16 | +""" |
| 17 | +import argparse |
| 18 | +import atexit |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | +import bpy |
| 23 | +from bpy.app.handlers import persistent |
| 24 | + |
| 25 | +SKIP_REASON = "exit_pre requires Blender 5.1+" |
| 26 | +MARKER = "exit_pre-ok" |
| 27 | +MAIN_MARKER = "from-main" |
| 28 | +WRONG_MARKER = "nope" |
| 29 | +ATEXIT_MARKER = "atexit-ok" |
| 30 | + |
| 31 | + |
| 32 | +def sidecar_path(): |
| 33 | + path = os.environ.get("BDT_SMOKE_SIDECAR") |
| 34 | + if not path: |
| 35 | + print("ERROR: BDT_SMOKE_SIDECAR unset", file=sys.stderr) |
| 36 | + return None |
| 37 | + return path |
| 38 | + |
| 39 | + |
| 40 | +def write_sidecar(text): |
| 41 | + path = sidecar_path() |
| 42 | + if not path: |
| 43 | + return False |
| 44 | + parent = os.path.dirname(path) |
| 45 | + if parent: |
| 46 | + os.makedirs(parent, exist_ok=True) |
| 47 | + with open(path, "w", encoding="utf-8") as fh: |
| 48 | + fh.write(text) |
| 49 | + if not text.endswith("\n"): |
| 50 | + fh.write("\n") |
| 51 | + return True |
| 52 | + |
| 53 | + |
| 54 | +@persistent |
| 55 | +def on_exit_write(*args): |
| 56 | + write_sidecar(MARKER) |
| 57 | + |
| 58 | + |
| 59 | +@persistent |
| 60 | +def on_exit_silent(*args): |
| 61 | + return |
| 62 | + |
| 63 | + |
| 64 | +@persistent |
| 65 | +def on_exit_wrong(*args): |
| 66 | + write_sidecar(WRONG_MARKER) |
| 67 | + |
| 68 | + |
| 69 | +def on_atexit(): |
| 70 | + write_sidecar(ATEXIT_MARKER) |
| 71 | + |
| 72 | + |
| 73 | +def maybe_skip(force_run): |
| 74 | + if bpy.app.version >= (5, 1, 0): |
| 75 | + return 0 |
| 76 | + if force_run: |
| 77 | + return 0 |
| 78 | + print(f"SMOKE_SKIP: {SKIP_REASON}", flush=True) |
| 79 | + return 77 |
| 80 | + |
| 81 | + |
| 82 | +def main(): |
| 83 | + argv = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] |
| 84 | + p = argparse.ArgumentParser() |
| 85 | + p.add_argument( |
| 86 | + "--force-run", |
| 87 | + action="store_true", |
| 88 | + help="bypass the 5.1 skip; 4.5 fails accessing handlers.exit_pre", |
| 89 | + ) |
| 90 | + p.add_argument( |
| 91 | + "--silent-handler", |
| 92 | + action="store_true", |
| 93 | + help="falsification: register exit_pre that writes nothing", |
| 94 | + ) |
| 95 | + p.add_argument( |
| 96 | + "--wrong-text", |
| 97 | + action="store_true", |
| 98 | + help="falsification: handler writes nope, not exit_pre-ok", |
| 99 | + ) |
| 100 | + p.add_argument( |
| 101 | + "--no-handler", |
| 102 | + action="store_true", |
| 103 | + help="falsification: do not register exit_pre, do not write in main", |
| 104 | + ) |
| 105 | + p.add_argument( |
| 106 | + "--write-in-main", |
| 107 | + action="store_true", |
| 108 | + help="falsification: write from-main in main without exit_pre", |
| 109 | + ) |
| 110 | + p.add_argument( |
| 111 | + "--atexit-instead", |
| 112 | + action="store_true", |
| 113 | + help="falsification: atexit writes atexit-ok, not exit_pre", |
| 114 | + ) |
| 115 | + args = p.parse_args(argv) |
| 116 | + |
| 117 | + skipped = maybe_skip(args.force_run) |
| 118 | + if skipped: |
| 119 | + return skipped |
| 120 | + |
| 121 | + if args.force_run and bpy.app.version < (5, 1, 0): |
| 122 | + try: |
| 123 | + bpy.app.handlers.exit_pre.append(on_exit_write) |
| 124 | + except AttributeError as exc: |
| 125 | + print(f"ERROR: {type(exc).__name__}: {exc}", file=sys.stderr) |
| 126 | + return 2 |
| 127 | + print("ERROR: exit_pre existed on this Blender; 4.5 should AttributeError", file=sys.stderr) |
| 128 | + return 2 |
| 129 | + |
| 130 | + if sidecar_path() is None: |
| 131 | + return 1 |
| 132 | + |
| 133 | + if args.write_in_main: |
| 134 | + write_sidecar(MAIN_MARKER) |
| 135 | + print("wrote sidecar from main (no exit_pre)", flush=True) |
| 136 | + return 0 |
| 137 | + |
| 138 | + if args.no_handler: |
| 139 | + print("no exit_pre registered", flush=True) |
| 140 | + return 0 |
| 141 | + |
| 142 | + if args.atexit_instead: |
| 143 | + atexit.register(on_atexit) |
| 144 | + print("registered atexit, not exit_pre", flush=True) |
| 145 | + return 0 |
| 146 | + |
| 147 | + if args.silent_handler: |
| 148 | + bpy.app.handlers.exit_pre.append(on_exit_silent) |
| 149 | + print("registered silent exit_pre", flush=True) |
| 150 | + return 0 |
| 151 | + |
| 152 | + if args.wrong_text: |
| 153 | + bpy.app.handlers.exit_pre.append(on_exit_wrong) |
| 154 | + print("registered exit_pre writing nope", flush=True) |
| 155 | + return 0 |
| 156 | + |
| 157 | + bpy.app.handlers.exit_pre.append(on_exit_write) |
| 158 | + print("registered exit_pre", flush=True) |
| 159 | + return 0 |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + try: |
| 164 | + sys.exit(main()) |
| 165 | + except Exception as exc: |
| 166 | + print(f"ERROR: {type(exc).__name__}: {exc}", file=sys.stderr) |
| 167 | + sys.exit(1) |
0 commit comments