Suppose someone like me compiles Blender as Python module. In that case, you cannot pass any arguments when importing
bpy in your Python environment. All you can do is doing
import bpy and that’s it! For instance, how can one stop the
bpy package from loading the addons, assuming that you have added the
--no-addon argument? All you can do is to do
import bpy and then use the functions/classes within
bpy to do everything else, which is pretty late for many things.
However, there could use a simple trick to take care of this issue and that trick is to set a bunch of environmental variables before doing
import bpy and read them during
bpy startup. This is similar to what many other Python packages do (e.g. this example for Numpy).
Another use case: recently I had this issue and there was no way to resolve this except recompiling everything to get a new
bpy module with setting
-DWITH_CYCLES_DEVICE_CUDA=OFF (of course a more fundamental solution is to do a lazy CUDA initialization as in PyTorch). This issue might have been resolved by setting an environmental variable like
export CYCLES_DEVICE_CUDA=OFF. So, after doing
import bpy, the
bpy module would look for many environmental variables (
CYCLES_DEVICE_CUDA in this case) and determines whether Cycles should use/initialize CUDA or not. In another scenario I wanted to define the path for a blender preference file but it looks like I couldn’t do it through the Python API or after the bpy module has been loaded. Maybe having an environmental variable for things like this could be useful as well. This saves a lot of time and energy and makes things much simpler. This same logic can be applied for things like
etc. Of course, in most cases people will not set these environmental variables and thebpy` module get loaded with the default settings.
Edit: I just noticed that there are a couple of environmental variables that Blender reads them while loading but I think this list should be expanded:
$BLENDER_USER_CONFIG Directory for user configuration files.
$BLENDER_USER_SCRIPTS Directory for user scripts.
$BLENDER_SYSTEM_SCRIPTS Directory for system wide scripts.
$BLENDER_USER_DATAFILES Directory for user data files (icons, translations, ..).
$BLENDER_SYSTEM_DATAFILES Directory for system wide data files.
$BLENDER_SYSTEM_PYTHON Directory for system Python libraries.
$TMP or $TMPDIR Store temporary files here.
$SDL_AUDIODRIVER LibSDL audio driver - alsa, esd, dma.
Is this more clear now?