1
# ##### BEGIN GPL LICENSE BLOCK #####
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License
5
# as published by the Free Software Foundation; either version 2
6
# of the License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software Foundation,
15
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
# ##### END GPL LICENSE BLOCK #####
34
error_duplicates = False
35
error_encoding = False
36
addons_fake_modules = {}
40
# RELEASE SCRIPTS: official scripts distributed in Blender releases
41
addon_paths = _bpy.utils.script_paths("addons")
43
# CONTRIB SCRIPTS: good for testing but not official scripts yet
44
# if folder addons_contrib/ exists, scripts in there will be loaded too
45
addon_paths += _bpy.utils.script_paths("addons_contrib")
47
# EXTERN SCRIPTS: external projects scripts
48
# if folder addons_extern/ exists, scripts in there will be loaded too
49
addon_paths += _bpy.utils.script_paths("addons_extern")
54
def modules(module_cache):
55
global error_duplicates
59
error_duplicates = False
60
error_encoding = False
64
# fake module importing
65
def fake_module(mod_name, mod_path, speedy=True, force_support=None):
68
if _bpy.app.debug_python:
69
print("fake_module", mod_path, mod_name)
71
ModuleType = type(ast)
72
file_mod = open(mod_path, "r", encoding='UTF-8')
75
line_iter = iter(file_mod)
77
while not l.startswith("bl_info"):
79
l = line_iter.readline()
80
except UnicodeDecodeError as e:
81
if not error_encoding:
83
print("Error reading file as UTF-8:", mod_path, e)
92
l = line_iter.readline()
93
except UnicodeDecodeError as e:
94
if not error_encoding:
96
print("Error reading file as UTF-8:", mod_path, e)
100
data = "".join(lines)
103
data = file_mod.read()
108
ast_data = ast.parse(data, filename=mod_path)
110
print("Syntax error 'ast.parse' can't read %r" % mod_path)
112
traceback.print_exc()
118
for body in ast_data.body:
119
if body.__class__ == ast.Assign:
120
if len(body.targets) == 1:
121
if getattr(body.targets[0], "id", "") == "bl_info":
127
mod = ModuleType(mod_name)
128
mod.bl_info = ast.literal_eval(body.value)
129
mod.__file__ = mod_path
130
mod.__time__ = os.path.getmtime(mod_path)
132
print("AST error in module %s" % mod_name)
134
traceback.print_exc()
137
if force_support is not None:
138
mod.bl_info["support"] = force_support
144
modules_stale = set(module_cache.keys())
146
for path in path_list:
148
# force all contrib addons to be 'TESTING'
149
if path.endswith("addons_contrib") or path.endswith("addons_extern"):
150
force_support = 'TESTING'
154
for mod_name, mod_path in _bpy.path.module_names(path):
155
modules_stale -= {mod_name}
156
mod = module_cache.get(mod_name)
158
if mod.__file__ != mod_path:
159
print("multiple addons with the same name:\n %r\n %r" %
160
(mod.__file__, mod_path))
161
error_duplicates = True
163
elif mod.__time__ != os.path.getmtime(mod_path):
164
print("reloading addon:",
167
os.path.getmtime(mod_path),
170
del module_cache[mod_name]
174
mod = fake_module(mod_name,
176
force_support=force_support)
178
module_cache[mod_name] = mod
180
# just in case we get stale modules, not likely
181
for mod_stale in modules_stale:
182
del module_cache[mod_stale]
185
mod_list = list(module_cache.values())
186
mod_list.sort(key=lambda mod: (mod.bl_info['category'],
192
def check(module_name):
194
Returns the loaded state of the addon.
196
:arg module_name: The name of the addon and module.
197
:type module_name: string
198
:return: (loaded_default, loaded_state)
199
:rtype: tuple of booleans
202
loaded_default = module_name in _bpy.context.user_preferences.addons
204
mod = sys.modules.get(module_name)
205
loaded_state = mod and getattr(mod, "__addon_enabled__", Ellipsis)
207
if loaded_state is Ellipsis:
208
print("Warning: addon-module %r found module "
209
"but without __addon_enabled__ field, "
210
"possible name collision from file: %r" %
211
(module_name, getattr(mod, "__file__", "<unknown>")))
215
return loaded_default, loaded_state
218
def enable(module_name, default_set=True):
220
Enables an addon by name.
222
:arg module_name: The name of the addon and module.
223
:type module_name: string
224
:return: the loaded module or None on failure.
234
traceback.print_exc()
236
# reload if the mtime changes
237
mod = sys.modules.get(module_name)
238
# chances of the file _not_ existing are low, but it could be removed
239
if mod and os.path.exists(mod.__file__):
240
mod.__addon_enabled__ = False
241
mtime_orig = getattr(mod, "__time__", 0)
242
mtime_new = os.path.getmtime(mod.__file__)
243
if mtime_orig != mtime_new:
244
print("module changed on disk:", mod.__file__, "reloading...")
250
del sys.modules[module_name]
252
mod.__addon_enabled__ = False
254
# Split registering up into 3 steps so we can undo
255
# if it fails par way through.
259
mod = __import__(module_name)
260
mod.__time__ = os.path.getmtime(mod.__file__)
261
mod.__addon_enabled__ = False
266
# 2) try register collected modules
267
# removed, addons need to handle own registration now.
269
# 3) try run the modules register function
274
del sys.modules[module_name]
277
# * OK loaded successfully! *
279
# just in case its enabled already
280
ext = _bpy.context.user_preferences.addons.get(module_name)
282
ext = _bpy.context.user_preferences.addons.new()
283
ext.module = module_name
285
mod.__addon_enabled__ = True
287
if _bpy.app.debug_python:
288
print("\taddon_utils.enable", mod.__name__)
293
def disable(module_name, default_set=True):
295
Disables an addon by name.
297
:arg module_name: The name of the addon and module.
298
:type module_name: string
301
mod = sys.modules.get(module_name)
303
# possible this addon is from a previous session and didn't load a
304
# module this time. So even if the module is not found, still disable
305
# the addon in the user prefs.
307
mod.__addon_enabled__ = False
313
traceback.print_exc()
315
print("addon_utils.disable", module_name, "not loaded")
317
# could be in more then once, unlikely but better do this just in case.
318
addons = _bpy.context.user_preferences.addons
321
while module_name in addons:
322
addon = addons.get(module_name)
326
if _bpy.app.debug_python:
327
print("\taddon_utils.disable", module_name)
330
def reset_all(reload_scripts=False):
332
Sets the addon state based on the user preferences.
337
# RELEASE SCRIPTS: official scripts distributed in Blender releases
340
for path in paths_list:
341
_bpy.utils._sys_path_ensure(path)
342
for mod_name, mod_path in _bpy.path.module_names(path):
343
is_enabled, is_loaded = check(mod_name)
345
# first check if reload is needed before changing state.
347
mod = sys.modules.get(mod_name)
351
if is_enabled == is_loaded:
356
print("\taddon_utils.reset_all unloading", mod_name)
360
def module_bl_info(mod, info_basis={"name": "",
368
"support": 'COMMUNITY',
371
"show_expanded": False,
375
addon_info = getattr(mod, "bl_info", {})
377
# avoid re-initializing
378
if "_init" in addon_info:
382
mod.bl_info = addon_info
384
for key, value in info_basis.items():
385
addon_info.setdefault(key, value)
387
if not addon_info["name"]:
388
addon_info["name"] = mod.__name__
390
addon_info["_init"] = None