preferences.py 4.7 KB

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