File: bl_load_addons.py

package info (click to toggle)
blender 4.3.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 309,564 kB
  • sloc: cpp: 2,385,210; python: 330,236; ansic: 280,972; xml: 2,446; sh: 972; javascript: 317; makefile: 170
file content (130 lines) | stat: -rw-r--r-- 3,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# SPDX-FileCopyrightText: 2011-2022 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later

# Simple script to enable all add-ons, and disable.

"""
./blender.bin --background --factory-startup --python tests/python/bl_load_addons.py
"""

import bpy
import addon_utils

import sys
import importlib

EXCLUDE_DIRECTORIES = (
    bpy.utils.user_resource('SCRIPTS'),
    *addon_utils.paths()[1:],
)
EXCLUDE_ADDONS = set()


def _init_addon_exclusions():

    # In case we built without cycles.
    if not bpy.app.build_options.cycles:
        EXCLUDE_ADDONS.add("cycles")

    # In case we built without freestyle.
    if not bpy.app.build_options.freestyle:
        EXCLUDE_ADDONS.add("render_freestyle_svg")

    if not bpy.app.build_options.xr_openxr:
        EXCLUDE_ADDONS.add("viewport_vr_preview")


def addon_modules_sorted():
    # Pass in an empty module cache to prevent `addon_utils` local module cache being manipulated.
    modules = [
        mod for mod in addon_utils.modules(module_cache={})
        if not (mod.__file__.startswith(EXCLUDE_DIRECTORIES))
        if not (mod.__name__ in EXCLUDE_ADDONS)
    ]
    modules.sort(key=lambda mod: mod.__name__)
    return modules


def disable_addons():
    # First disable all.
    addons = bpy.context.preferences.addons
    for mod_name in list(addons.keys()):
        addon_utils.disable(mod_name, default_set=True)
    assert bool(addons) is False


def test_load_addons(prefs):
    modules = addon_modules_sorted()

    disable_addons()

    addons = prefs.addons

    addons_fail = []

    for mod in modules:
        mod_name = mod.__name__
        print("\tenabling:", mod_name)
        addon_utils.enable(mod_name, default_set=True)
        if (mod_name not in addons) and (mod_name not in EXCLUDE_ADDONS):
            addons_fail.append(mod_name)

    if addons_fail:
        print("addons failed to load (%d):" % len(addons_fail))
        for mod_name in addons_fail:
            print("    %s" % mod_name)
    else:
        print("addons all loaded without errors!")
    print("")


def reload_addons(prefs, do_reload=True, do_reverse=True):
    modules = addon_modules_sorted()
    addons = prefs.addons

    disable_addons()

    # Run twice each time.
    for _ in (0, 1):
        for mod in modules:
            mod_name = mod.__name__
            print("\tenabling:", mod_name)
            addon_utils.enable(mod_name, default_set=True)
            assert mod_name in addons

        for mod in modules:
            mod_name = mod.__name__
            print("\tdisabling:", mod_name)
            addon_utils.disable(mod_name, default_set=True)
            assert not (mod_name in addons)

            # Now test reloading.
            if do_reload:
                sys.modules[mod_name] = importlib.reload(sys.modules[mod_name])

        if do_reverse:
            # In case order matters when it shouldn't.
            modules.reverse()


def main():
    from bpy import context
    # Remove repositories since these point to the users home directory by default.
    prefs = context.preferences
    extension_repos = prefs.extensions.repos
    while extension_repos:
        extension_repos.remove(extension_repos[0])

    _init_addon_exclusions()

    # First load add-ons, print a list of all add-ons that fail.
    test_load_addons(prefs)

    reload_addons(prefs, do_reload=False, do_reverse=False)
    reload_addons(prefs, do_reload=False, do_reverse=True)
    reload_addons(prefs, do_reload=True, do_reverse=True)


if __name__ == "__main__":
    main()