forked from blender/blender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon_utils.py
More file actions
2011 lines (1660 loc) · 75.7 KB
/
addon_utils.py
File metadata and controls
2011 lines (1660 loc) · 75.7 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: 2011-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
__all__ = (
"paths",
"modules",
"check",
"check_extension",
"enable",
"disable",
"disable_all",
"reset_all",
"module_bl_info",
"extensions_refresh",
"stale_pending_remove_paths",
"stale_pending_stage_paths",
)
import bpy as _bpy
_preferences = _bpy.context.preferences
error_encoding = False
# (name, file, path)
error_duplicates = []
addons_fake_modules = {}
# Global cached extensions, set before loading extensions on startup.
# `{addon_module_name: "Reason for incompatibility", ...}`
_extensions_incompatible = {}
# Global extension warnings, lazily calculated when displaying extensions.
# `{addon_module_name: "Warning", ...}`
_extensions_warnings = {}
# Filename used for stale files (which we can't delete).
_stale_filename = ".~stale~"
# Don't display these in the UI, unless extension development is enabled.
#
# NOTE: these add-ons will *not* be included in `bpy.context.preferences.addons`,
# therefore they cannot have saved preferences. Ideally this would be supported,
# which could be part of an improvement to store preferences for disabled add-ons.
# This is the reason we can't include "cycles".
# See #71486 and follow up discussion on #151863.
_addons_hidden_core = {
"bl_pkg",
"io_anim_bvh",
"io_curve_svg",
"io_mesh_uv_layout",
"io_scene_fbx",
}
# Called only once at startup, avoids calling 'reset_all', correct but slower.
def _initialize_once():
for path in paths():
_bpy.utils._sys_path_ensure_append(path)
_stale_pending_check_and_remove_once()
_initialize_extensions_repos_once()
for addon in _preferences.addons:
if (module_name := addon.module) in _addons_hidden_core:
continue
enable(
module_name,
# Ensured by `_initialize_extensions_repos_once`.
refresh_handled=True,
)
for module_name in _addons_hidden_core:
enable(
module_name,
# Ensured by `_initialize_extensions_repos_once`.
refresh_handled=True,
default_set=False,
persistent=True,
)
def paths():
import os
paths = []
for i, p in enumerate(_bpy.utils.script_paths()):
# Bundled add-ons are always first.
# Since this isn't officially part of the API, print an error so this never silently fails.
addon_dir = os.path.join(p, "addons_core" if i == 0 else "addons")
if os.path.isdir(addon_dir):
paths.append(addon_dir)
elif i == 0:
print("Internal error:", addon_dir, "was not found!")
return paths
# A version of `paths` that includes extension repositories returning a list `(path, package)` pairs.
#
# Notes on the ``package`` value.
#
# - For top-level modules (the "addons" directories, the value is an empty string)
# because those add-ons can be imported directly.
# - For extension repositories the value is a module string (which can be imported for example)
# where any modules within the `path` can be imported as a sub-module.
# So for example, given a list value of: `("/tmp/repo", "bl_ext.temp_repo")`.
#
# An add-on located at `/tmp/repo/my_handy_addon.py` will have a unique module path of:
# `bl_ext.temp_repo.my_handy_addon`, which can be imported and will be the value of it's `Addon.module`.
def _paths_with_extension_repos():
import os
addon_paths = [(path, "") for path in paths()]
for repo in _preferences.extensions.repos:
if not repo.enabled:
continue
dirpath = repo.directory
if not os.path.isdir(dirpath):
continue
addon_paths.append((dirpath, "{:s}.{:s}".format(_ext_base_pkg_idname, repo.module)))
return addon_paths
def _fake_module(mod_name, mod_path, speedy=True):
global error_encoding
import os
if _bpy.app.debug_python:
print("fake_module", mod_path, mod_name)
if mod_name.startswith(_ext_base_pkg_idname_with_dot):
return _fake_module_from_extension(mod_name, mod_path)
import ast
ModuleType = type(ast)
try:
file_mod = open(mod_path, "r", encoding='UTF-8')
except OSError as ex:
print("Error opening file:", mod_path, ex)
return None
with file_mod:
if speedy:
lines = []
line_iter = iter(file_mod)
line = ""
while not line.startswith("bl_info"):
try:
line = line_iter.readline()
except UnicodeDecodeError as ex:
if not error_encoding:
error_encoding = True
print("Error reading file as UTF-8:", mod_path, ex)
return None
if len(line) == 0:
break
while line.rstrip():
lines.append(line)
try:
line = line_iter.readline()
except UnicodeDecodeError as ex:
if not error_encoding:
error_encoding = True
print("Error reading file as UTF-8:", mod_path, ex)
return None
data = "".join(lines)
else:
data = file_mod.read()
del file_mod
try:
ast_data = ast.parse(data, filename=mod_path)
except Exception:
print("Syntax error 'ast.parse' cannot read:", repr(mod_path))
import traceback
traceback.print_exc()
ast_data = None
body_info = None
if ast_data:
for body in ast_data.body:
if body.__class__ == ast.Assign:
if len(body.targets) == 1:
if getattr(body.targets[0], "id", "") == "bl_info":
body_info = body
break
if body_info:
try:
mod = ModuleType(mod_name)
mod.bl_info = ast.literal_eval(body.value)
mod.__file__ = mod_path
mod.__time__ = os.path.getmtime(mod_path)
except Exception:
print("AST error parsing bl_info for:", repr(mod_path))
import traceback
traceback.print_exc()
return None
return mod
else:
print("Warning: add-on missing 'bl_info', this can cause poor performance!:", repr(mod_path))
return None
def modules_refresh(*, module_cache=addons_fake_modules):
global error_encoding
import os
error_encoding = False
error_duplicates.clear()
modules_stale = set(module_cache.keys())
for path, pkg_id in _paths_with_extension_repos():
for mod_name, mod_path in _bpy.path.module_names(path, package=pkg_id):
modules_stale.discard(mod_name)
mod = module_cache.get(mod_name)
if mod is not None:
if mod.__file__ != mod_path:
print(
"multiple addons with the same name:\n"
" {!r}\n"
" {!r}".format(mod.__file__, mod_path)
)
error_duplicates.append((mod.bl_info["name"], mod.__file__, mod_path))
elif (
(mod.__time__ != os.path.getmtime(metadata_path := mod_path)) if not pkg_id else
# Check the manifest time as this is the source of the cache.
(mod.__time_manifest__ != os.path.getmtime(metadata_path := mod.__file_manifest__))
):
print("reloading addon meta-data:", mod_name, repr(metadata_path), "(time-stamp change detected)")
del module_cache[mod_name]
mod = None
if mod is None:
mod = _fake_module(
mod_name,
mod_path,
)
if mod:
module_cache[mod_name] = mod
# just in case we get stale modules, not likely
for mod_stale in modules_stale:
del module_cache[mod_stale]
del modules_stale
def modules(*, module_cache=addons_fake_modules, refresh=True):
if refresh or ((module_cache is addons_fake_modules) and modules._is_first):
modules_refresh(module_cache=module_cache)
modules._is_first = False
# Dictionaries are ordered in more recent versions of Python,
# re-create the dictionary from sorted items.
# This avoids having to sort on every call to this function.
module_cache_items = list(module_cache.items())
# Sort by name with the module name as a tie breaker.
module_cache_items.sort(key=lambda item: ((item[1].bl_info.get("name") or item[0]).casefold(), item[0]))
module_cache.clear()
module_cache.update((key, value) for key, value in module_cache_items)
return module_cache.values()
modules._is_first = True
def check(module_name):
"""
Returns the loaded state of the addon.
:param module_name: The name of the addon and module.
:type module_name: str
:return: (loaded_default, loaded_state)
:rtype: tuple[bool, bool]
"""
import sys
loaded_default = module_name in _preferences.addons
mod = sys.modules.get(module_name)
loaded_state = (
(mod is not None) and
getattr(mod, "__addon_enabled__", Ellipsis)
)
if loaded_state is Ellipsis:
print(
"Warning: addon-module", module_name, "found module "
"but without '__addon_enabled__' field, "
"possible name collision from file:",
repr(getattr(mod, "__file__", "<unknown>")),
)
loaded_state = False
if mod and getattr(mod, "__addon_persistent__", False):
loaded_default = True
return loaded_default, loaded_state
def check_extension(module_name):
"""
Return true if the module is an extension.
"""
return module_name.startswith(_ext_base_pkg_idname_with_dot)
# utility functions
def _addon_ensure(module_name):
addons = _preferences.addons
addon = addons.get(module_name)
if not addon:
addon = addons.new()
addon.module = module_name
def _addon_remove(module_name):
addons = _preferences.addons
while module_name in addons:
addon = addons.get(module_name)
if addon:
addons.remove(addon)
def enable(module_name, *, default_set=False, persistent=False, refresh_handled=False, handle_error=None):
"""
Enables an addon by name.
:param module_name: the name of the addon and module.
:type module_name: str
:param default_set: Set the user-preference.
:type default_set: bool
:param persistent: Ensure the addon is enabled for the entire session (after loading new files).
:type persistent: bool
:param refresh_handled: When true, :func:`extensions_refresh` must have been called with ``module_name``
included in ``addon_modules_pending``.
This should be used to avoid many calls to refresh extensions when enabling multiple add-ons at once.
:type refresh_handled: bool
:param handle_error: Called in the case of an error, taking an exception argument.
:type handle_error: Callable[[Exception], None] | None
:return: the loaded module or None on failure.
:rtype: ModuleType
"""
import os
import sys
import importlib
from _bpy_restrict_state import RestrictBlend
if handle_error is None:
def handle_error(ex):
if isinstance(ex, ImportError):
# NOTE: checking "Add-on " prefix is rather weak,
# it's just a way to avoid the noise of a full trace-back when
# an add-on is simply missing on the file-system.
if (type(msg := ex.msg) is str) and msg.startswith("Add-on "):
print(msg)
return
import traceback
traceback.print_exc()
if (is_extension := module_name.startswith(_ext_base_pkg_idname_with_dot)):
if not refresh_handled:
extensions_refresh(
addon_modules_pending=[module_name],
handle_error=handle_error,
)
# Ensure the extensions are compatible.
if _extensions_incompatible:
if (error := _extensions_incompatible.get(
module_name[len(_ext_base_pkg_idname_with_dot):].partition(".")[0::2],
)):
try:
raise RuntimeError("Extension {:s} is incompatible ({:s})".format(module_name, error))
except RuntimeError as ex:
handle_error(ex)
# No need to call `extensions_refresh` because incompatible extensions
# will not have their wheels installed.
return None
# NOTE: from now on, before returning None, `extensions_refresh()` must be called
# to ensure wheels setup in anticipation for this extension being used are removed upon failure.
# reload if the mtime changes
mod = sys.modules.get(module_name)
# chances of the file _not_ existing are low, but it could be removed
# Set to `mod.__file__` or None.
mod_file = None
if (
(mod is not None) and
(mod_file := mod.__file__) is not None and
os.path.exists(mod_file)
):
if getattr(mod, "__addon_enabled__", False):
# This is an unlikely situation,
# re-register if the module is enabled.
# Note: the UI doesn't allow this to happen,
# in most cases the caller should 'check()' first.
try:
mod.unregister()
except Exception as ex:
print("Exception in module unregister():", (mod_file or module_name))
handle_error(ex)
if is_extension and not refresh_handled:
extensions_refresh(handle_error=handle_error)
return None
mod.__addon_enabled__ = False
mtime_orig = getattr(mod, "__time__", 0)
mtime_new = os.path.getmtime(mod_file)
if mtime_orig != mtime_new:
print("module changed on disk:", repr(mod_file), "reloading...")
try:
importlib.reload(mod)
except Exception as ex:
handle_error(ex)
del sys.modules[module_name]
if is_extension and not refresh_handled:
extensions_refresh(handle_error=handle_error)
return None
mod.__addon_enabled__ = False
# add the addon first it may want to initialize its own preferences.
# must remove on fail through.
if default_set:
_addon_ensure(module_name)
# Split registering up into 3 steps so we can undo
# if it fails par way through.
# Disable the context: using the context at all
# while loading an addon is really bad, don't do it!
with RestrictBlend():
# 1) try import
try:
# Use instead of `__import__` so that sub-modules can eventually be supported.
# This is also documented to be the preferred way to import modules.
mod = importlib.import_module(module_name)
if (mod_file := mod.__file__) is None:
# This can happen when:
# - The add-on has been removed but there are residual `.pyc` files left behind.
# - An extension is a directory that doesn't contain an `__init__.py` file.
#
# Include a message otherwise the "cause:" for failing to load the module is left blank.
# Include the `__path__` when available so there is a reference to the location that failed to load.
raise ImportError(
"module loaded with no associated file, __path__={!r}, aborting!".format(
getattr(mod, "__path__", None)
),
name=module_name,
)
mod.__time__ = os.path.getmtime(mod_file)
mod.__addon_enabled__ = False
except Exception as ex:
# If the add-on doesn't exist, don't print full trace-back because the back-trace is in this case
# is verbose without any useful details. A missing path is better communicated in a short message.
# Account for `ImportError` & `ModuleNotFoundError`.
if isinstance(ex, ImportError):
if ex.name == module_name:
ex.msg = "Add-on not loaded: \"{:s}\", cause: {:s}".format(module_name, str(ex))
# Issue with an add-on from an extension repository, report a useful message.
elif is_extension and module_name.startswith(ex.name + "."):
repo_id = module_name[len(_ext_base_pkg_idname_with_dot):].rpartition(".")[0]
repo = next(
(repo for repo in _preferences.extensions.repos if repo.module == repo_id),
None,
)
if repo is None:
ex.msg = (
"Add-on not loaded: \"{:s}\", cause: extension repository \"{:s}\" doesn't exist".format(
module_name, repo_id,
)
)
elif not repo.enabled:
ex.msg = (
"Add-on not loaded: \"{:s}\", cause: extension repository \"{:s}\" is disabled".format(
module_name, repo_id,
)
)
else:
# The repository exists and is enabled, it should have imported.
ex.msg = "Add-on not loaded: \"{:s}\", cause: {:s}".format(module_name, str(ex))
handle_error(ex)
if default_set:
_addon_remove(module_name)
if is_extension and not refresh_handled:
extensions_refresh(handle_error=handle_error)
return None
if is_extension:
# Handle the case the an extension has `bl_info` (which is not used for extensions).
# Note that internally a `bl_info` is added based on the extensions manifest - for compatibility.
# So it's important not to use this one.
bl_info = getattr(mod, "bl_info", None)
if bl_info is not None:
# Use `_init` to detect when `bl_info` was generated from the manifest, see: `_bl_info_from_extension`.
if type(bl_info) is dict and "_init" not in bl_info:
# This print is noisy, hide behind a debug flag.
# Once `bl_info` is fully deprecated this should be changed to always print a warning.
if _bpy.app.debug_python:
print(
"Add-on \"{:s}\" has a \"bl_info\" which will be ignored in favor of \"{:s}\"".format(
module_name, _ext_manifest_filename_toml,
)
)
# Always remove as this is not expected to exist and will be lazily initialized.
del mod.bl_info
# 2) Try register collected modules.
# Removed register_module, addons need to handle their own registration now.
# Core add-ons are unconditionally enabled and don't support being filtered out.
use_owner = is_extension or (module_name not in _addons_hidden_core)
from _bpy import _bl_owner_id_get, _bl_owner_id_set
owner_id_prev = _bl_owner_id_get()
_bl_owner_id_set(module_name if use_owner else "")
# 3) Try run the modules register function.
try:
mod.register()
except Exception as ex:
print("Exception in module register():", (mod_file or module_name))
handle_error(ex)
del sys.modules[module_name]
if default_set:
_addon_remove(module_name)
if is_extension and not refresh_handled:
extensions_refresh(handle_error=handle_error)
return None
finally:
_bl_owner_id_set(owner_id_prev)
# * OK loaded successfully! *
mod.__addon_enabled__ = True
mod.__addon_persistent__ = persistent
if _bpy.app.debug_python:
print("\taddon_utils.enable", mod.__name__)
return mod
def disable(module_name, *, default_set=False, refresh_handled=False, handle_error=None):
"""
Disables an addon by name.
:param module_name: The name of the addon and module.
:type module_name: str
:param default_set: Set the user-preference.
:type default_set: bool
:param handle_error: Called in the case of an error, taking an exception argument.
:type handle_error: Callable[[Exception], None] | None
"""
import sys
if handle_error is None:
def handle_error(_ex):
import traceback
traceback.print_exc()
mod = sys.modules.get(module_name)
# Possible this add-on is from a previous session and didn't load a
# module this time. So even if the module is not found, still disable
# the add-on in the user preferences.
if mod and getattr(mod, "__addon_enabled__", False) is not False:
mod.__addon_enabled__ = False
mod.__addon_persistent__ = False
try:
mod.unregister()
except Exception as ex:
mod_path = getattr(mod, "__file__", module_name)
print("Exception in module unregister():", repr(mod_path))
del mod_path
handle_error(ex)
else:
print(
"addon_utils.disable: {:s} not {:s}".format(
module_name,
"loaded" if mod is None else "enabled",
)
)
# could be in more than once, unlikely but better do this just in case.
if default_set:
_addon_remove(module_name)
if not refresh_handled:
extensions_refresh(handle_error=handle_error)
if _bpy.app.debug_python:
print("\taddon_utils.disable", module_name)
def reset_all(*, reload_scripts=False):
"""
Sets the addon state based on the user preferences.
"""
import sys
# Ensures stale `addons_fake_modules` isn't used.
modules._is_first = True
addons_fake_modules.clear()
# Update extensions compatibility (after reloading preferences).
# Potentially refreshing wheels too.
extensions_refresh()
for path, pkg_id in _paths_with_extension_repos():
if not pkg_id:
_bpy.utils._sys_path_ensure_append(path)
for mod_name, _mod_path in _bpy.path.module_names(path, package=pkg_id):
is_enabled, is_loaded = check(mod_name)
# first check if reload is needed before changing state.
if reload_scripts:
import importlib
mod = sys.modules.get(mod_name)
if mod:
importlib.reload(mod)
if is_enabled == is_loaded:
pass
elif is_enabled:
enable(mod_name, refresh_handled=True)
elif is_loaded:
print("\taddon_utils.reset_all unloading", mod_name)
disable(mod_name)
def disable_all():
import sys
# Collect modules to disable first because dict can be modified as we disable.
# NOTE: don't use `getattr(item[1], "__addon_enabled__", False)` because this runs on all modules,
# including 3rd party modules unrelated to Blender.
#
# Some modules may have their own `__getattr__` and either:
# - Not raise an `AttributeError` (is they should),
# causing `hasattr` & `getattr` to raise an exception instead of treating the attribute as missing.
# - Generate modules dynamically, modifying `sys.modules` which is being iterated over,
# causing a RuntimeError: "dictionary changed size during iteration".
#
# Either way, running 3rd party logic here can cause undefined behavior.
# Use direct `__dict__` access to bypass `__getattr__`, see: #111649.
modules = sys.modules.copy()
addon_modules = [
item for item in modules.items()
if type(mod_dict := getattr(item[1], "__dict__", None)) is dict
if mod_dict.get("__addon_enabled__")
]
# Check the enabled state again since it's possible the disable call
# of one add-on disables others.
for mod_name, mod in addon_modules:
if getattr(mod, "__addon_enabled__", False):
disable(mod_name, refresh_handled=True)
def _blender_manual_url_prefix():
return "https://docs.blender.org/manual/{:s}/{:d}.{:d}".format(
_bpy.utils.manual_language_code(),
*_bpy.app.version[:2],
)
def _bl_info_basis():
return {
"name": "",
"author": "",
"version": (),
"blender": (),
"location": "",
"description": "",
"doc_url": "",
"support": 'COMMUNITY',
"category": "",
"warning": "",
"show_expanded": False,
}
def module_bl_info(mod, *, info_basis=None):
if info_basis is None:
info_basis = _bl_info_basis()
addon_info = getattr(mod, "bl_info", {})
# avoid re-initializing
if "_init" in addon_info:
return addon_info
if not addon_info:
if mod.__name__.startswith(_ext_base_pkg_idname_with_dot):
addon_info, filepath_toml = _bl_info_from_extension(mod.__name__, mod.__file__)
if addon_info is None:
# Unexpected, this is a malformed extension if meta-data can't be loaded.
print("module_bl_info: failed to extract meta-data from", filepath_toml)
# Continue to initialize dummy data.
addon_info = {}
mod.bl_info = addon_info
for key, value in info_basis.items():
addon_info.setdefault(key, value)
if not addon_info["name"]:
addon_info["name"] = mod.__name__
doc_url = addon_info["doc_url"]
if doc_url:
doc_url_prefix = "{BLENDER_MANUAL_URL}"
if doc_url_prefix in doc_url:
addon_info["doc_url"] = doc_url.replace(
doc_url_prefix,
_blender_manual_url_prefix(),
)
# Remove the maintainers email while it's not private, showing prominently
# could cause maintainers to get direct emails instead of issue tracking systems.
import re
if "author" in addon_info:
addon_info["author"] = re.sub(r"\s*<.*?>", "", addon_info["author"])
addon_info["_init"] = None
return addon_info
# -----------------------------------------------------------------------------
# Stale File Handling
#
# Notes:
# - On startup, a file exists that indicates cleanup is needed.
# In the common case the file doesn't exist.
# Otherwise module paths are scanned for files to remove.
# - Since errors resolving paths to remove could result in user data loss,
# ensure the paths are always within the (extension/add-on/app-template) directory.
# - File locking isn't used, if multiple Blender instances start at the
# same time and try to remove the same files, this won't cause errors.
# Even so, remove the checking file immediately avoid unnecessary
# file-system access overhead for other Blender instances.
#
# For more implementation details see `_bpy_internal.extensions.stale_file_manager`.
# This mainly impacts WIN32 which can't remove open file handles, see: #77837 & #125049.
#
# Use for all systems as the problem can impact any system if file removal fails
# for any reason (typically permissions or file-system error).
def _stale_pending_filepath():
# When this file exists, stale file removal is pending.
# Try to remove stale files next launch.
import os
return os.path.join(_bpy.utils.user_resource('CONFIG'), "stale-pending")
def _stale_pending_stage(debug):
import os
stale_pending_filepath = _stale_pending_filepath()
dirpath = os.path.dirname(stale_pending_filepath)
if os.path.exists(stale_pending_filepath):
return
try:
os.makedirs(dirpath, exist_ok=True)
with open(stale_pending_filepath, "wb") as _:
pass
except Exception as ex:
print("Unable to set stale files pending:", str(ex))
def _stale_file_directory_iter():
import os
for repo in _preferences.extensions.repos:
if not repo.enabled:
continue
if repo.source == 'SYSTEM':
continue
yield repo.directory
# Skip `addons_core` because add-ons because these will never be uninstalled by the user.
yield from paths()[1:]
# The `local_dir`, for wheels.
yield os.path.join(_bpy.utils.user_resource('EXTENSIONS'), ".local")
# The `path_app_templates`, for user app-templates.
yield _bpy.utils.user_resource(
'SCRIPTS',
path=os.path.join("startup", "bl_app_templates_user"),
create=False,
)
def _stale_pending_check_and_remove_once():
# This runs on every startup, early exit if no stale data removal is staged.
import os
stale_pending_filepath = _stale_pending_filepath()
if not os.path.exists(stale_pending_filepath):
return
# Some stale data needs to be removed, this is an exceptional case.
# Allow for slower logic than is typically accepted on startup.
from _bpy_internal.extensions.stale_file_manager import StaleFiles
debug = _bpy.app.debug_python
# Remove the pending file if all are removed.
is_empty = True
for dirpath in _stale_file_directory_iter():
if not os.path.exists(os.path.join(dirpath, _stale_filename)):
continue
try:
stale_handle = StaleFiles(
base_directory=dirpath,
stale_filename=_stale_filename,
debug=debug,
)
stale_handle.state_load(check_exists=True)
if not stale_handle.is_empty():
stale_handle.state_remove_all()
if not stale_handle.is_empty():
is_empty = False
if stale_handle.is_modified():
stale_handle.state_store(check_exists=False)
except Exception as ex:
print("Unexpected error clearing stale data, this is a bug!", str(ex))
if is_empty:
try:
os.remove(stale_pending_filepath)
except Exception as ex:
if debug:
print("Failed to remove stale-pending file:", str(ex))
def stale_pending_stage_paths(path_base, paths):
# - `path_base` must a directory iterated over by `_stale_file_directory_iter`.
# Otherwise the stale files will never be removed.
# - `paths` must be absolute paths which could not be removed.
# They must be located within `base_path` otherwise they cannot be removed.
from _bpy_internal.extensions.stale_file_manager import StaleFiles
debug = _bpy.app.debug_python
stale_handle = StaleFiles(
base_directory=path_base,
stale_filename=_stale_filename,
debug=debug,
)
# Already checked.
if stale_handle.state_load_add_and_store(paths=paths):
# Force clearing stale files on next restart.
_stale_pending_stage(debug)
def stale_pending_remove_paths(path_base, paths):
# The reverse of: `stale_pending_stage_paths`.
from _bpy_internal.extensions.stale_file_manager import StaleFiles
debug = _bpy.app.debug_python
stale_handle = StaleFiles(
base_directory=path_base,
stale_filename=_stale_filename,
debug=debug,
)
# Already checked.
if stale_handle.state_load_remove_and_store(paths=paths):
# Don't attempt to reverse the `_stale_pending_stage` call.
# This is not trivial since other repositories may need to be cleared.
# There will be a minor performance hit on restart but this is enough
# of a corner case that it's not worth attempting to calculate if
# removal of pending files is needed or not.
pass
# -----------------------------------------------------------------------------
# Extension Pre-Flight Compatibility Check
#
# Check extension compatibility on startup so any extensions which are incompatible with Blender are marked as
# incompatible and wont be loaded. This cache avoids having to scan all extensions on startup on *every* startup.
#
# Implementation:
#
# The emphasis for this cache is to have minimum overhead for the common case where:
# - The simple case where there are no extensions enabled (running tests, background tasks etc).
# - The more involved case where extensions are enabled and have not changed since last time Blender started.
# In this case do as little as possible since it runs on every startup, the following steps are unavoidable.
# - When reading compatibility cache, then run the following tests, regenerating when changes are detected.
# - Compare with previous blender version/platform.
# - Stat the manifests of all enabled extensions, testing that their modification-time and size are unchanged.
# - When any changes are detected,
# regenerate compatibility information which does more expensive operations
# (loading manifests, check version ranges etc).
#
# Other notes:
#
# - This internal format may change at any point, regenerating the cache should be reasonably fast
# but may introduce a small but noticeable pause on startup for user configurations that contain many extensions.
# - Failure to load will simply ignore the file and regenerate the file as needed.
#
# Format:
#
# - The cache is ZLIB compressed pickled Python dictionary.
# - The dictionary keys are as follows:
# `"blender": (bpy.app.version, platform.system(), platform.machine(), python_version, magic_number)`
# `"filesystem": [(repo_module, pkg_id, manifest_time, manifest_size), ...]`
# `"incompatible": {(repo_module, pkg_id): "Reason for being incompatible", ...}`
#
def _pickle_zlib_file_read(filepath):
import pickle
import gzip
with gzip.GzipFile(filepath, "rb") as fh:
data = pickle.load(fh)
return data
def _pickle_zlib_file_write(filepath, data) -> None:
import pickle
import gzip
with gzip.GzipFile(filepath, "wb", compresslevel=9) as fh:
pickle.dump(data, fh)
def _extension_repos_module_to_directory_map():
return {repo.module: repo.directory for repo in _preferences.extensions.repos if repo.enabled}
def _extension_compat_cache_update_needed(
cache_data, # `dict[str, Any]`
blender_id, # `tuple[Any, ...]`
extensions_enabled, # `set[tuple[str, str]]`
print_debug, # `Callable[[Any], None] | None`
): # `-> bool`
# Detect when Blender itself changes.
if cache_data.get("blender") != blender_id:
if print_debug is not None:
print_debug("blender changed")
return True
# Detect when any of the extensions paths change.
cache_filesystem = cache_data.get("filesystem", [])
# Avoid touching the file-system if at all possible.
# When the length is the same and all cached ID's are in this set, we can be sure they are a 1:1 patch.
if len(cache_filesystem) != len(extensions_enabled):
if print_debug is not None:
print_debug("length changes ({:d} -> {:d}).".format(len(cache_filesystem), len(extensions_enabled)))
return True
from os import stat
from os.path import join
repos_module_to_directory_map = _extension_repos_module_to_directory_map()
for repo_module, pkg_id, cache_stat_time, cache_stat_size in cache_filesystem:
if (repo_module, pkg_id) not in extensions_enabled:
if print_debug is not None:
print_debug("\"{:s}.{:s}\" no longer enabled.".format(repo_module, pkg_id))
return True
if repo_directory := repos_module_to_directory_map.get(repo_module, ""):
pkg_manifest_filepath = join(repo_directory, pkg_id, _ext_manifest_filename_toml)
else:
pkg_manifest_filepath = ""
# It's possible an extension has been set as an add-on but cannot find the repository it came from.
# In this case behave as if the file can't be found (because it can't) instead of ignoring it.