preferences.py 4.9 KB

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