preferences.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. def callback_enable_selection_state(self, context):
  60. from . import timers
  61. timers.ENABLED = self.EnableSelectionSync
  62. if timers.ENABLED == True:
  63. bpy.app.timers.register(timers.timer_update_selection_sync)
  64. else:
  65. try:
  66. bpy.app.timers.unregister(timers.timer_update_selection_sync)
  67. except ValueError:
  68. pass # It's not registered anymore. that's OK
  69. def callback_sync_selection_poll_rate(self, context):
  70. from . import timers
  71. timers.SELECTION_POLL_RATE = self.SelectionSyncPollRate
  72. # JSONprefix: bpy.props.StringProperty(
  73. # name = "Prefix code file",
  74. # subtype = 'FILE_PATH',
  75. # default = dir_path + '/preferences/prefix.json',)
  76. # JSONchiral: bpy.props.StringProperty(
  77. # name = "Chiral Identifier file",
  78. # subtype = 'FILE_PATH',
  79. # default = dir_path + '/preferences/chiral_identifier.json',)
  80. # JSONseperator:bpy.props.StringProperty(
  81. # name = "Seperator file",
  82. # subtype = 'FILE_PATH',
  83. # default = dir_path + '/preferences/seperator.json',)
  84. widget_library_path : bpy.props.StringProperty()
  85. WidgetsLibraryFolder:bpy.props.StringProperty(
  86. name = "Widget Library Folder",
  87. get=widget_library_get,
  88. set=widget_library_idiot_test,
  89. subtype = 'FILE_PATH',
  90. default = os.path.join(dir_path, 'widgets'),)
  91. WidgetDefaultCollection:bpy.props.StringProperty(
  92. name = "Import Widgets into Collection",
  93. default = "Widgets",)
  94. CurveDefaultCollection:bpy.props.StringProperty(
  95. name = "Import Curves into Collection",
  96. default = "MetaCurves",)
  97. MetaArmatureDefaultCollection:bpy.props.StringProperty(
  98. name = "Import Meta-Armatures into Collection",
  99. default = "MetaRigs",)
  100. ComponentsLibraryFolder:bpy.props.StringProperty(
  101. name = "Component Library Folder",
  102. description = "Location of .rig files to place in the Add Armature menu.",
  103. subtype = 'FILE_PATH',
  104. default = os.path.join(dir_path, 'component_packs'),)
  105. ComponentsAutoLoadFolder:bpy.props.StringProperty(
  106. name = "Component Autoload Folder",
  107. description = "Location of .rig files to load automatically.",
  108. subtype = 'FILE_PATH',
  109. default = os.path.join(dir_path, 'auto_load_components'),)
  110. EnableSelectionSync:bpy.props.BoolProperty(
  111. name = "Enable Selection Sync",
  112. description = "Sync selection of nodes and scene objects.",
  113. default = True,
  114. update = callback_enable_selection_state)
  115. SelectionSyncPollRate:bpy.props.FloatProperty(
  116. name = "Selection Sync Poll Rate",
  117. description = "How often to check for changes in selection.",
  118. default = 0.01,
  119. update = callback_sync_selection_poll_rate)
  120. def draw(self, context):
  121. layout = self.layout
  122. layout.label(text="Mantis Preferences")
  123. layout.prop(self, "EnableSelectionSync")
  124. layout.prop(self, "SelectionSyncPollRate")
  125. layout.prop(self, "WidgetDefaultCollection")
  126. layout.prop(self, "CurveDefaultCollection")
  127. layout.prop(self, "MetaArmatureDefaultCollection")
  128. layout.label(text="Paths:")
  129. layout.prop(self, "WidgetsLibraryFolder", icon='FILE_FOLDER')
  130. layout.prop(self, "ComponentsLibraryFolder", icon='FILE_FOLDER')
  131. layout.prop(self, "ComponentsAutoLoadFolder", icon='FILE_FOLDER')