From 64ffc14e0abe72c039ba99e57738a1bc06a4e0f1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 1 Dec 2026 02:17:45 +0100 Subject: [PATCH] run_intents: refuse Blender user directories that are a person's own, or that Blender is not using The guard only checked that the three variables were non-empty. tools/private_dirs.py now refuses a variable set inside %APPDATA%/Blender Foundation, ~/.config/blender or the macOS folder, and a Blender whose own answer for CONFIG, EXTENSIONS or SCRIPTS is not under the variable. (Soul finding run-intents-guard-checks-non-empty, PR 22.) Co-Authored-By: Claude Fable 5.2 --- surfaces/blender/tools/private_dirs.py | 44 ++++++++++++++++++++++++++ surfaces/blender/tools/run_intents.py | 12 ++++--- 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 surfaces/blender/tools/private_dirs.py diff --git a/surfaces/blender/tools/private_dirs.py b/surfaces/blender/tools/private_dirs.py new file mode 100644 index 0000000..014ef78 --- /dev/null +++ b/surfaces/blender/tools/private_dirs.py @@ -0,0 +1,44 @@ +"""Whether a Blender about to be driven by a script has user directories of its own. Stdlib only. + +A script that enables add-ons or saves preferences in a person's own Blender folders changes her Blender. The +three variables below point Blender at other folders; BLENDER_USER_RESOURCE_PATH does not (Blender 5.3 ignores it). +""" +from __future__ import annotations + +import os +from pathlib import Path +from typing import Callable, Mapping + +VARIABLES = {"BLENDER_USER_CONFIG": "CONFIG", "BLENDER_USER_EXTENSIONS": "EXTENSIONS", "BLENDER_USER_SCRIPTS": "SCRIPTS"} + + +def own_blender_roots(environ: Mapping[str, str], home: str) -> list[Path]: + """Where a person's own Blender keeps its folders on Windows, Linux and macOS, for this environment.""" + roots = [Path(home) / ".config" / "blender", Path(home) / "Library" / "Application Support" / "Blender"] + for variable in ("APPDATA", "XDG_CONFIG_HOME"): + if environ.get(variable): + roots.append(Path(environ[variable]) / ("Blender Foundation" if variable == "APPDATA" else "blender")) + return roots + + +def _inside(path: Path, root: Path) -> bool: + a, b = os.path.normcase(os.path.abspath(path)), os.path.normcase(os.path.abspath(root)) + return a == b or a.startswith(b + os.sep) + + +def refusal(environ: Mapping[str, str], home: str, resource: Callable[[str], str] | None = None) -> str | None: + """None when the three variables are set to folders outside every own-Blender root, and, given Blender's + own answer (resource('CONFIG') and so on, bpy.utils.user_resource), Blender is really using them; otherwise + the sentence to refuse with.""" + for variable, kind in VARIABLES.items(): + value = environ.get(variable) + if not value: + return f"{variable} is not set: run in private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS directories" + for root in own_blender_roots(environ, home): + if _inside(Path(value), root): + return f"{variable} is {value}, inside a person's own Blender folder ({root}): point it at a scratch directory" + if resource is not None: + used = resource(kind) + if not used or not _inside(Path(used), Path(value)): + return f"Blender uses {used or 'no folder'} for {kind}, not {variable} ({value}): refusing to touch it" + return None diff --git a/surfaces/blender/tools/run_intents.py b/surfaces/blender/tools/run_intents.py index 9c06c01..eb09a64 100644 --- a/surfaces/blender/tools/run_intents.py +++ b/surfaces/blender/tools/run_intents.py @@ -8,8 +8,8 @@ rows are checked as in a live session, and each gets a receipt. No CultMesh node written to (default: the recipe's name with .receipts.json) and nowhere else, so nothing here shows in a person's Agents panel. This script is the human of its own Blender: it enables the host add-ons the recipe names, reloads host actions once, and opens and saves the .blend. It refuses to start unless -BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS are set, so it never reads or writes a -person's own Blender folders (docs/install.md). +BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS are set to folders outside a person's own +Blender folders and Blender is really using them (tools/private_dirs.py), so it never reads or writes hers. The recipe: @@ -41,9 +41,11 @@ def fail(message: str) -> None: def main() -> int: - for name in ("BLENDER_USER_CONFIG", "BLENDER_USER_EXTENSIONS", "BLENDER_USER_SCRIPTS"): - if not os.environ.get(name): - fail(f"{name} is not set: run in private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS directories") + sys.path.insert(0, str(Path(__file__).resolve().parent)) + import private_dirs + refused = private_dirs.refusal(os.environ, os.path.expanduser("~"), lambda kind: bpy.utils.user_resource(kind)) + if refused: + fail(refused) arguments = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] if not arguments: fail("usage: blender --background --factory-startup --python run_intents.py -- []")