How can I force blender to load or unload Addons when launched from command line

Hey Guys,

I’m currently adding Blender to our Pipeline and want to be able to create variations of Blender that start or stop different Addons when launched.

Likewise, I need some addons to be launched at first run. I plan to use the BQt addon, as our pipeline is built around Pyside. Ideally, the user would NOT have to turn it on at first initial run. The plugin would already turn on, preventing each single user to have to do so.

Every script I’ve tried so far, seems to gets run “too soon” :man_facepalming: When Launched, blender runs the scripts in the “script/startup” before the addons are even seen by blender. Likewise, if I launch Blender with the --python command, It again Won’t enable the addons, as it’s running before their script as run.

In Maya this is quite simple, as I can create startup script and tell it what plugins to load or unload. I’m surprised that this seems complicated in Blender.

As I’m new to scripting in Blender, I might just be missing something quite Obvious. :sweat_smile: Any help would be greatly appreciated.

Hi and welcome to BA,
an easy solution could be Application Templates:
https://docs.blender.org/manual/en/latest/advanced/app_templates.html
I use them a lot.
Even though something has changed a little since then, this video is gold and worth watching:

Hi BG_Division,

This looks pretty excellent!

I’ll have to see if these can also find templates saved elsewhere than AppData, as I don’t want to have to push these templates to everyone.

I’ll start reading up on it, thanks a lot!!

hmm… I’m not a guru in this field but maybe this could be an helpful start:

source:
https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#command-line-args-environment-variables

Include the same folder in AppData’s blender configuration and Program Files’ blender configuration.

If you install it at the location of the blender in Program Files, you can set it individually without affecting AppData.

ups, sorry @Francky, I misunderstood that part :grimacing:
I don’t think environment variables have anything to do with it

I had intended to do it like oo_1942Kim mentioned.

But your option might be simpler, I could have standard Blender Installs on the machines, but then set extra paths where the Custom Templates are.

This could be project/task specific too, instead of adding all possible templates to each blender.

Thanks!

here’s one as well for you regarding templates:
custom application templates - welcome to blender brew

some things don’t work properly with templates, like activating paths on load.
It’s better to use a separate startup script for that if you want to use various script/extensions setups. It has some… limitations still.

Well this is beyond frustrating!
For some reason I can’t seem to set environment variables through any means?!

This has to be a user error on my part?!


I’ve remove this post, as it was in fact User error :man_facepalming:

Yes, this can be a exercise in frustration to get things working. As you probably already found out there’s several threads on this forum dealing with similiar topics.
You tried using a init file on startup looking at the environment settings?

And yes, any other 3d app deals with this much simpler or sophisticated :man_shrugging:

1 Like

Finally got it working properly :sweat_smile:
Haven’t made Application Templates yet, but that part should be straightforward.

My confusion came in part that each specific environment variable were meant for. As user_scripts and system_scripts are not interchangeable, as they are each meant for specific addons.

The other issue was simply that the path MUST contain the addons folder inside it, you can’t link directly to the addons themselves. This was not specified in the docs, as I imagine they assume we’d know this :sweat_smile:

I’ll share, just in case it can help someone else:

In the end it’s rather simple, my .bat file is:

set BLENDER_USER_SCRIPTS=\fs01\env\packages\blender\user_scripts\
set BLENDER_SYSTEM_SCRIPTS=\fs01\env\packages\blender\system_scripts\

“C:\Program Files\Blender Foundation\Blender 4.2\blender.exe” --python launch_config.py --addons bqt

The pyside addon, BQt is placed in an addons folder in the SYSTEM_SCRIPTS and launched automatically with the --addons bqt command.

The python script “launch_config.py” actually runs before the addons are loaded, so I use it to set up the PATHs to our repo and packages. This way, I can forgo having to install Pyside and all other python packages directly to each Blender install.

Thanks again guys!!

1 Like

Good to see you got it working.
yes, the docs are sometimes cryptic, and in this case very much so.

I advise you to also log in onto the developer forum, as there’s a discussion going on atm about (VFX platform) pipelines and Python support.

It would add to the discussion about studios implementing Blender.
Also, explain your issues as well, maybe the docs will be updated to be more clear on certain topics. :wink:

I think I’ll wait to be more familiar with all the intricacies of how blender do things before commenting there :sweat_smile:

But I’ll bookmark that Link.

Thanks Rob

I thought I’d share the final solution I ended up with.

Thanks to RobWuRob’s help, I have something much more flexible than using Environment Variables.

Everything is controlled thru a Python script instead, dynamically adding or removing script directories based on the script that was used. (Thanks RobWu)

I initially though that starting Addons in a Python Startup script didn’t work, but in fact it does. But there is a Bug in Blender, where preferences won’t show the proper state of the addons when doing so.

I’ve also noticed that Blender 4.2+ doesn’t use PYTHONPATH, so I first add a PATH to where my tools are. While also adding BQt to my pipeline and running it directly, instead of adding it to a Blender addon path.

The addon path are wiped clean initially, as the artist might have started another config previously. This way I can create launch configs for Modeling, Lighting or Animation, which just a few tweak of this .py script.

Finally, I can force enable addons that I want on with this config.

Blender is then launched using the config with:
blender --python lighting_config.py

I won’t be using Application Templates, as it wouldn’t be as flexible.

As artist will start any task using the pipeline, it can easily take care of starting up with a proper empty scene, with premade collections and whatnot depending on the task.

Here’s the code:

# Local imports
import bpy
import addon_utils

# Built-in imports
import sys
from pathlib import Path

# Setup Environment paths
launcher_path = Path.home() / ".env/repos/launcher/"
sys.path.append(str(launcher_path))

import launcher  # Sets up enviroment

# Let's launch Pyside Integration
import blender_tools.bqt as bqt
print("Starting up BQt")
bqt.register()

# Let's add our custom addon paths
prefs = bpy.context.preferences
# Let's start by removing any sfs_ names ones, as these where created thru pipeline
for i, directory in enumerate(prefs.filepaths.script_directories):
    if "sfs_" in directory.name:
        bpy.ops.preferences.script_directory_remove(i)

# Let's add back the one specific to this template
dir_dict = {'sfs_general': "//fs01/env/packages/blender/base_tools/",
            'sfs_lighting': "//fs01/env/packages/blender/light_tools/"}

for name, path in dir_dict.items():
    bpy.ops.preferences.script_directory_add(directory=path, filter_folder=True)
    # let's rename it
    new_dir = bpy.context.preferences.filepaths.script_directories[-1]
    new_dir.name = name
    print(f"Added script directory: {path}")


# Insert any Addons that should be Loaded at launch
enabled_addons = ['node_wrangler']
for addon in enabled_addons:
    addon_utils.enable(addon)
1 Like