PyAPI: postpone loading add-ons until after key-maps have been defined

Loading add-ons after key-maps resolves a problem where add-ons would
setup shortcuts before Blender had created the key-maps.
Making add-ons have to declare the key-maps using region & window types
matching Blender's internal values.

This PR a pitfall pointed out in #110030.

Ref !110092
This commit is contained in:
Campbell Barton
2023-09-05 11:35:14 +10:00
parent bedfc68e3f
commit 6de294a191
4 changed files with 49 additions and 20 deletions

View File

@@ -189,7 +189,7 @@ _global_loaded_modules = [] # store loaded module names for reloading.
import bpy_types as _bpy_types # keep for comparisons, never ever reload this.
def load_scripts(*, reload_scripts=False, refresh_scripts=False):
def load_scripts(*, reload_scripts=False, refresh_scripts=False, extensions=True):
"""
Load scripts and run each modules register function.
@@ -199,6 +199,8 @@ def load_scripts(*, reload_scripts=False, refresh_scripts=False):
:arg refresh_scripts: only load scripts which are not already loaded
as modules.
:type refresh_scripts: bool
:arg: extensions: Loads additional scripts (add-ons & app-templates).
:type: extensions: bool
"""
use_time = use_class_register_check = _bpy.app.debug_python
use_user = not _is_factory_startup
@@ -306,21 +308,8 @@ def load_scripts(*, reload_scripts=False, refresh_scripts=False):
for mod in modules_from_path(path, loaded_modules):
test_register(mod)
# load template (if set)
if any(_bpy.utils.app_template_paths()):
import bl_app_template_utils
bl_app_template_utils.reset(reload_scripts=reload_scripts)
del bl_app_template_utils
# Deal with add-ons separately.
_initialize_once = getattr(_addon_utils, "_initialize_once", None)
if _initialize_once is not None:
# First time, use fast-path.
_initialize_once()
del _addon_utils._initialize_once
else:
_addon_utils.reset_all(reload_scripts=reload_scripts)
del _initialize_once
if extensions:
load_scripts_extensions(reload_scripts=reload_scripts)
if reload_scripts:
_bpy.context.window_manager.tag_script_reload()
@@ -342,6 +331,31 @@ def load_scripts(*, reload_scripts=False, refresh_scripts=False):
)
def load_scripts_extensions(*, reload_scripts=False):
"""
Load extensions scripts (add-ons and app-templates)
:arg reload_scripts: Causes all scripts to have their unregister method
called before loading.
:type reload_scripts: bool
"""
# load template (if set)
if any(_bpy.utils.app_template_paths()):
import bl_app_template_utils
bl_app_template_utils.reset(reload_scripts=reload_scripts)
del bl_app_template_utils
# deal with addons separately
_initialize = getattr(_addon_utils, "_initialize", None)
if _initialize is not None:
# first time, use fast-path
_initialize()
del _addon_utils._initialize
else:
_addon_utils.reset_all(reload_scripts=reload_scripts)
del _initialize
def script_path_user():
"""returns the env var and falls back to home dir or None"""
path = _user_resource('SCRIPTS')