Make the add-on to be registered in last anywhere

In some cases when Blender starts it is necessary to make the add-on to be registered in last, after all other add-ons have been registered. This may be needed if the add-on conflicts with other add-ons or uses functionality from other add-ons, which should be available already during its registration.

When Blender starts, add-ons are loaded in order, according to the “bpy.context.preferences.addons” list, the order of add-ons in which is determined by they were activated by the user.

We can write the required order of activation of the add-on in its installation instructions or, using the fact that the list of add-ons is already available to us when the add-on is registering, add the functionality we need in the “register” function of the add-on.

Let’s write classes for two simple add-ons: The “First” and the “Second”.

The “First” add-on class:

The “Second” add-on class:

Pay attention to the call of the “print” command in the “register” function of add-ons. With its help, we will track the actual loading order of our add-ons.

Save the classes in separate .py files and install add-ons from these files to Blender. First, install the “Second” add-on and after it – the “First” add-on. Don’t forget to save the user settings.

Walking through the “bpy.context.preferences.addons” list:

We can see that our add-ons will be loaded in order: the “Second” – the “First”.

We can verify this by running Blender from the system console to see the output from the “register” add-on functions.

Let’s make some changes to the “Second” add-on so that it will always be loaded last.

We can perform manipulations with loading the add-on inside its “register” function.

Let’s add a check to the “register” function code of the “Second” add-on to see if the add-on is in the last place in the “bpy.context.preferences.addons list”.

If success – everything works as we need, the add-on is loaded in last, and we can register its classes as usual.

And we will get into the “else” condition if Blender is already loading the add-on, but there are more add-ons in the “bpy.context.preferences.addons list” after it.

In this case, we get a pointer to the add-on in the “bpy.context.preferences.addons” list and use it to remove the add-on from its current position in the list.

Then we create a pointer to the new add-on in the list, it will be added to the end, and we assign it the name of our add-on.

When Blender reaches the end of the list, and the last element of the list is again the pointer to our “Second” add-on, it will try to register our add-on again, and since this time the add-on is registered from the last place in the list, its registration will succeed.

In order for the add-on not to throw an error when calling the “unregister” function, we add the additional “second_registered” variable, in which we will mark whether the add-on is really registered or not.

Let’s reinstall our add-ons, again in order, first “Second”, then – “First”.

Now if we launch Blender from the console, we will see the following output:

Blender will first try to register the “Second” add-on from its current location, registration is skipped, and the add-on is moved to the last place in the list. Then the “First” add-on is registered. After that, the “Second” add-on is really registered, the last one in the order.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments