preferences.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import bpy
  2. import os
  3. dir_path = os.path.dirname(os.path.realpath(__file__))
  4. #
  5. def get_bl_addon_object(raise_error = False):
  6. from bpy import context
  7. try_these_first = ['bl_ext.nodes_tools.mantis',
  8. 'bl_ext.repos.mantis', 'bl_ext.blender_modules_enabled.mantis',]
  9. for mantis_key in try_these_first:
  10. bl_mantis_addon = context.preferences.addons.get(mantis_key)
  11. if bl_mantis_addon is not None: # chekc the addon AND the prefs
  12. if bl_mantis_addon.preferences is not None:
  13. break
  14. # the prefs will be None if the addon is disabled.
  15. else:
  16. for k in context.preferences.addons.keys():
  17. if k.endswith("mantis"):
  18. bl_mantis_addon = context.preferences.addons[k]
  19. if bl_mantis_addon is not None:
  20. if bl_mantis_addon.preferences is not None:
  21. print(f"Unexpected addon preferences key: {k}")
  22. break
  23. else:
  24. print("FAILED TO FIND MANTIS.")
  25. if bl_mantis_addon is None or bl_mantis_addon.preferences is None:
  26. if raise_error==True:
  27. raise RuntimeError("Mantis Preferences not found. This is a bug."
  28. " Please report it on gitlab.")
  29. if raise_error==False:
  30. print( "Mantis Preferences not found. This is a bug."
  31. " Please report it on gitlab.")
  32. return bl_mantis_addon
  33. # Just look and see if it is a ridiculous choice and show an error popup if the user needs
  34. # to select a different directory
  35. def filepath_idiot_test(path):
  36. def do_error_popup():
  37. def error_popup_draw(self, context):
  38. self.layout.label(text="A maximum of 1000 widget files is allowed in the Widget Library.")
  39. self.layout.label(text="Make sure the WIdget Library does not scan a huge number of files/folders.")
  40. from bpy import context
  41. context.window_manager.popup_menu(error_popup_draw, title="Error", icon='ERROR')
  42. try:
  43. if tot_files := sum([len(files) for r, d, files in os.walk(path)]) > 1000:
  44. do_error_popup()
  45. return ''
  46. else:
  47. return path
  48. except FileNotFoundError:
  49. return ''
  50. except RecursionError:
  51. do_error_popup()
  52. return ''
  53. def widget_library_get(self):
  54. return self.widget_library_path
  55. def widget_library_idiot_test(self, value):
  56. self.widget_library_path = filepath_idiot_test(value)
  57. class MantisPreferences(bpy.types.AddonPreferences):
  58. bl_idname = __package__
  59. # JSONprefix: bpy.props.StringProperty(
  60. # name = "Prefix code file",
  61. # subtype = 'FILE_PATH',
  62. # default = dir_path + '/preferences/prefix.json',)
  63. # JSONchiral: bpy.props.StringProperty(
  64. # name = "Chiral Identifier file",
  65. # subtype = 'FILE_PATH',
  66. # default = dir_path + '/preferences/chiral_identifier.json',)
  67. # JSONseperator:bpy.props.StringProperty(
  68. # name = "Seperator file",
  69. # subtype = 'FILE_PATH',
  70. # default = dir_path + '/preferences/seperator.json',)
  71. widget_library_path : bpy.props.StringProperty()
  72. WidgetsLibraryFolder:bpy.props.StringProperty(
  73. name = "Widget Library Folder",
  74. get=widget_library_get,
  75. set=widget_library_idiot_test,
  76. subtype = 'FILE_PATH',
  77. default = os.path.join(dir_path, 'widgets'),)
  78. WidgetDefaultCollection:bpy.props.StringProperty(
  79. name = "Import Widgets into Collection",
  80. default = "Widgets",)
  81. CurveDefaultCollection:bpy.props.StringProperty(
  82. name = "Import Curves into Collection",
  83. default = "MetaCurves",)
  84. MetaArmatureDefaultCollection:bpy.props.StringProperty(
  85. name = "Import Meta-Armatures into Collection",
  86. default = "MetaRigs",)
  87. ComponentsLibraryFolder:bpy.props.StringProperty(
  88. name = "Component Library Folder",
  89. description = "Location of .rig files to place in the Add Armature menu.",
  90. subtype = 'FILE_PATH',
  91. default = os.path.join(dir_path, 'component_packs'),)
  92. ComponentsAutoLoadFolder:bpy.props.StringProperty(
  93. name = "Component Autoload Folder",
  94. description = "Location of .rig files to load automatically.",
  95. subtype = 'FILE_PATH',
  96. default = os.path.join(dir_path, 'auto_load_components'),)
  97. def draw(self, context):
  98. layout = self.layout
  99. layout.label(text="Mantis Preferences")
  100. layout.prop(self, "WidgetsLibraryFolder", icon='FILE_FOLDER')
  101. layout.prop(self, "WidgetDefaultCollection")
  102. layout.prop(self, "ComponentsLibraryFolder", icon='FILE_FOLDER')
  103. layout.prop(self, "CurveDefaultCollection")
  104. layout.prop(self, "MetaArmatureDefaultCollection")
  105. layout.prop(self, "ComponentsAutoLoadFolder", icon='FILE_FOLDER')