preferences.py 4.0 KB

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