bl_load_addons.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  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.
  7. #
  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.
  12. #
  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.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8 compliant>
  19. # simple script to enable all addons, and disable
  20. """
  21. ./blender.bin --background -noaudio --factory-startup --python tests/python/bl_load_addons.py
  22. """
  23. import bpy
  24. import addon_utils
  25. import os
  26. import sys
  27. import importlib
  28. BLACKLIST_DIRS = (
  29. os.path.join(bpy.utils.resource_path('USER'), "scripts"),
  30. ) + tuple(addon_utils.paths()[1:])
  31. BLACKLIST_ADDONS = set()
  32. def _init_addon_blacklist():
  33. # in case we built without cycles
  34. if not bpy.app.build_options.cycles:
  35. BLACKLIST_ADDONS.add("cycles")
  36. # in case we built without freestyle
  37. if not bpy.app.build_options.freestyle:
  38. BLACKLIST_ADDONS.add("render_freestyle_svg")
  39. # netrender has known problems re-registering
  40. BLACKLIST_ADDONS.add("netrender")
  41. for mod in addon_utils.modules():
  42. if addon_utils.module_bl_info(mod)['blender'] < (2, 80, 0):
  43. BLACKLIST_ADDONS.add(mod.__name__)
  44. def addon_modules_sorted():
  45. modules = addon_utils.modules({})
  46. modules[:] = [
  47. mod for mod in modules
  48. if not (mod.__file__.startswith(BLACKLIST_DIRS))
  49. if not (mod.__name__ in BLACKLIST_ADDONS)
  50. ]
  51. modules.sort(key=lambda mod: mod.__name__)
  52. return modules
  53. def disable_addons():
  54. # first disable all
  55. addons = bpy.context.preferences.addons
  56. for mod_name in list(addons.keys()):
  57. addon_utils.disable(mod_name, default_set=True)
  58. assert(bool(addons) is False)
  59. def test_load_addons():
  60. modules = addon_modules_sorted()
  61. disable_addons()
  62. addons = bpy.context.preferences.addons
  63. addons_fail = []
  64. for mod in modules:
  65. mod_name = mod.__name__
  66. print("\tenabling:", mod_name)
  67. addon_utils.enable(mod_name, default_set=True)
  68. if (mod_name not in addons) and (mod_name not in BLACKLIST_ADDONS):
  69. addons_fail.append(mod_name)
  70. if addons_fail:
  71. print("addons failed to load (%d):" % len(addons_fail))
  72. for mod_name in addons_fail:
  73. print(" %s" % mod_name)
  74. else:
  75. print("addons all loaded without errors!")
  76. print("")
  77. def reload_addons(do_reload=True, do_reverse=True):
  78. modules = addon_modules_sorted()
  79. addons = bpy.context.preferences.addons
  80. disable_addons()
  81. # Run twice each time.
  82. for _ in (0, 1):
  83. for mod in modules:
  84. mod_name = mod.__name__
  85. print("\tenabling:", mod_name)
  86. addon_utils.enable(mod_name, default_set=True)
  87. assert(mod_name in addons)
  88. for mod in modules:
  89. mod_name = mod.__name__
  90. print("\tdisabling:", mod_name)
  91. addon_utils.disable(mod_name, default_set=True)
  92. assert(not (mod_name in addons))
  93. # now test reloading
  94. if do_reload:
  95. sys.modules[mod_name] = importlib.reload(sys.modules[mod_name])
  96. if do_reverse:
  97. # in case order matters when it shouldn't
  98. modules.reverse()
  99. def main():
  100. _init_addon_blacklist()
  101. # first load addons, print a list of all addons that fail
  102. test_load_addons()
  103. reload_addons(do_reload=False, do_reverse=False)
  104. reload_addons(do_reload=False, do_reverse=True)
  105. reload_addons(do_reload=True, do_reverse=True)
  106. if __name__ == "__main__":
  107. # So a python error exits(1)
  108. try:
  109. main()
  110. except:
  111. import traceback
  112. traceback.print_exc()
  113. sys.exit(1)