Page MenuHome

Support environment variables to override USER & SYSTEM resource paths
ClosedPublic

Authored by Campbell Barton (campbellbarton) on Sep 30 2022, 4:59 AM.

Details

Summary

Even though individual USER/SYSTEM paths could be set using environment variables,
it wasn't possible to override the USER or SYSTEM paths.

This meant the result of bpy.utils.resource_path('USER') & bpy.utils.resource_path('SYSTEM') could still be used by scripts, making the Blender session potentially the default USER directory (even when BLENDER_USER_CONFIG, BLENDER_USER_SCRIPTS & BLENDER_USER_DATAFILES all point elsewhere).

Resolve by adding environment variables:

  • BLENDER_USER_RESOURCES
  • BLENDER_SYSTEM_RESOURCES

These will be used for bpy.utils.resource_path('USER') & bpy.utils.resource_path('SYSTEM'), as well as a basis for user & system directories, unless those environment variables are set (BLENDER_USER_* or BLENDER_SYSTEM_*).

Resolves issue raised by T101389.

Example usage & output:

1#!/bin/sh
2
3# Path to a portable build (containing ./bin/blender).
4BUILD_DIR=/path/to/build
5VERSION="3.4"
6
7# System Path:
8
9cd $BUILD_DIR/bin/$VERSION
10rm -rf relocated_system
11mkdir relocated_system
12mv datafiles relocated_system/
13mv python relocated_system/
14mv scripts relocated_system/
15cd ..
16export BLENDER_SYSTEM_RESOURCES=$BUILD_DIR/bin/$VERSION/relocated_system
17
18# User Path:
19mkdir $BUILD_DIR/bin/$VERSION/relocated_user
20mkdir $BUILD_DIR/bin/$VERSION/relocated_user/scripts
21export BLENDER_USER_RESOURCES=$BUILD_DIR/bin/$VERSION/relocated_user
22
23$BUILD_DIR/bin/blender -b \
24--python-expr "import bpy; print('TEMP', bpy.app.tempdir)" \
25--python-expr "import bpy; print('BLENDER_USER_SCRIPTS', bpy.utils.user_resource('SCRIPTS'))" \
26--python-expr "import bpy; print('BLENDER_USER_CONFIG', bpy.utils.user_resource('CONFIG'))" \
27--python-expr "import bpy; print('BLENDER_USER_DATAFILES', bpy.utils.user_resource('DATAFILES'))" \
28--python-expr "import bpy; print('BLENDER_SYSTEM_SCRIPTS', bpy.utils.system_resource('SCRIPTS'))" \
29--python-expr "import bpy; print('BLENDER_SYSTEM_DATAFILES', bpy.utils.system_resource('DATAFILES'))" \
30--python-expr "import bpy; print('BLENDER_SYSTEM_PYTHON', bpy.utils.system_resource('PYTHON'))" \
31--python-expr "import bpy; print('BLENDER_USER_RESOURCES:', bpy.utils.resource_path('USER'))" \
32--python-expr "import bpy; print('BLENDER_SYSTEM_RESOURCES:', bpy.utils.resource_path('SYSTEM'))" \
33--python-expr "import bpy; print('bpy.utils.script_paths()', bpy.utils.script_paths())" \
34--python-expr "import bpy; bpy.ops.wm.save_homefile()"
35
36# Expected output:
37#
38# ~ Blender 3.4.0 Alpha
39# ~ TEMP /tmp/blender_mCr4dz/
40# ~ BLENDER_USER_SCRIPTS /path/to/build/bin/3.4/relocated_user/scripts
41# ~ BLENDER_USER_CONFIG /path/to/build/bin/3.4/relocated_user/config
42# ~ BLENDER_USER_DATAFILES /path/to/build/bin/3.4/relocated_user/datafiles
43# ~ BLENDER_SYSTEM_SCRIPTS /path/to/build/bin/3.4/relocated_system/scripts
44# ~ BLENDER_SYSTEM_DATAFILES /path/to/build/bin/3.4/relocated_system/datafiles
45# ~ BLENDER_SYSTEM_PYTHON /path/to/build/bin/3.4/relocated_system/python
46# ~ BLENDER_USER_RESOURCES: /path/to/build/bin/3.4/relocated_user
47# ~ BLENDER_SYSTEM_RESOURCES: /path/to/build/bin/3.4/relocated_system
48# ~ bpy.utils.script_paths() ['/path/to/build/bin/3.4/relocated_system/scripts', '/path/to/build/bin/3.4/relocated_user/scripts']
49# ~ Writing homefile: '/path/to/build/bin/3.4/relocated_user/config/startup.blend' ok
50# ~ Info: Startup file saved


Note that this seems like a fairly straightforward change and reasonable not to have to define every sub-directory explicitly when overriding all, submitting for review in case there are details regarding it's implementation that could be changed.

I considered auto-detecting the base so if all BLENDER_USER_* shared a common root, that could automatically be used for BLENDER_USER_RESOURCES (same for SYSTEM...). But this seems too error prone, as it's possible only some environment variables are overridden making it unclear which root should be used - or, if we require all paths to be overridden - adding a new environment variable in the future would change the automatically selected resource-paths in a confusing & non-obvious way, so I think being explicit is the most straightforward solution.

Diff Detail

Repository
rB Blender
Branch
TEMP-RESOURCE-PATH (branched from master)
Build Status
Buildable 24074
Build 24074: arc lint + arc unit

Event Timeline

Campbell Barton (campbellbarton) requested review of this revision.Sep 30 2022, 4:59 AM
Campbell Barton (campbellbarton) created this revision.
Campbell Barton (campbellbarton) retitled this revision from Support environment variables for USER & SYSTEM resource paths to Support environment variables to override USER & SYSTEM resource paths.Sep 30 2022, 5:44 AM
Campbell Barton (campbellbarton) edited the summary of this revision. (Show Details)
Brecht Van Lommel (brecht) requested changes to this revision.Sep 30 2022, 6:41 PM

Would it make sense to name these BLENDER_USER_RESOURCES and BLENDER_SYSTEM_RESOURCES instead?

On the one hand BLENDER_RESOURCES_PATH_* is consistent with the API functions, but it's inconsistent with the other environment variables. Personally would pick consistency with the other environment variables.

This revision now requires changes to proceed.Sep 30 2022, 6:41 PM
  • Update based on feedback
This revision is now accepted and ready to land.Oct 3 2022, 2:03 PM