How to know the order in which scripts are loaded?

Whenever Blender is open, Load scripts and run each modules register function. But how to know which is the order in which these scripts (Addons) are registered?

It’s like if this function runs at the time that Blender is executed.
bpy.utils.load_scripts(reload_scripts=False, refresh_scripts=False)

I would expect it to load addons in the order it finds them on the harddrive. Operating systems usually list folder contents sorted alphanumerically.

Analyzing the module “addon_utils.py” I discovered that this code runs in Blender startup.


import bpy as _bpy
_user_preferences = _bpy.context.user_preferences

...


def _initialize():
    path_list = paths()
    for path in path_list:
        _bpy.utils._sys_path_ensure(path)
    for addon in _user_preferences.addons:
        enable(addon.module)

So the order would be the same as this. Am I right?


for addon in bpy.context.user_preferences.addons:
    print(addon)

However what I want to do is change this order. Or perform a function as soon as the last module is registered. But I found no handle that does this. :frowning:

Would it be possible to run an extra function without having to change the module “addon_utils.py”?
[SUB](Maybe putting a return in register function of a addon, I do not know)[/SUB]

1 Like

I would say that CoDEmanX is right.
when running blender, there’s a little more control. The order in which you register will be the order the menu item appears. (saved by user prefs). You can also control somewhat individual menu items, if you use multiple menu or menu func, the order in which you register in the addon, will be the order the menu is drawn.
maybe not helpful, just some register things I noticed recently. :slight_smile:

I know, this is not what you are asking but,
maybe bpy.app.handlers.load_post | bpy.app.handlers.load_pre could be a work around.
Also found this, Execute operators from Python at start up.

I have not tested this handle, but I imagine that it executes a function every time you load a Blender file, not just when you open the blender for the first time. I hope to be wrong.

However I found out how to change the order in which addons are registereds.
To change the order of execution of an addon, you have to disable this addon and enable again. So it will be at the end of the sequence.
So, just run these lines inside the register function of your addon:


def register():        
    if bpy.context.user_preferences.addons[-1].module != __name__:
        module_name = __name__
        addons = bpy.context.user_preferences.addons


        while module_name in addons:
            addon = addons.get(module_name)
            if addon:
                addons.remove(addon)


        addon = addons.new()
        addon.module = module_name

However, each time you enable an addon, it will be in front. Then the addon will only be the last until the second time you open Blender without enabling any addon. A small disadvantage.

1 Like