preferences.py 4.4 KB

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