menu_classes.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from bpy.types import Panel, Menu
  2. def TellClasses():
  3. return [
  4. MantisActiveTreePanel,
  5. MantisNodeGroupsMenu,
  6. MantisAddArmature,
  7. ]
  8. # Function to append submenu to context menu
  9. def node_context_menu_draw(self, context):
  10. layout = self.layout
  11. layout.separator() # Optional: Adds a separator before your submenu
  12. layout.operator_context = "INVOKE_DEFAULT"
  13. layout.operator("mantis.nodes_cleanup", text='Sort Selected Nodes')
  14. layout.operator("mantis.connect_nodes_to_input")
  15. layout.operator("mantis.select_nodes_of_type")
  16. layout.operator("mantis.import_from_component_library")
  17. # layout.menu('NODE_MT_context_menu_mantis')
  18. # Function to append submenu to node add menu
  19. def node_add_menu_draw(self, context):
  20. layout = self.layout
  21. layout.separator() # Optional: Adds a separator before your submenu
  22. layout.menu("NODE_MT_add_mantis_groups")
  23. # Function to append Mantis Component Packs to armature add menu
  24. def armature_add_menu_draw(self, context):
  25. layout = self.layout
  26. layout.separator()
  27. layout.menu("VIEW3D_MT_armature_add_mantis_packs")
  28. # Function to append Mantis to Import Menu
  29. def import_menu_draw(self, context):
  30. self.layout.operator("mantis.import_tree")
  31. class MantisNodeGroupsMenu(Menu):
  32. """Menu to show available node groups"""
  33. bl_idname= "NODE_MT_add_mantis_groups"
  34. bl_label = "Components"
  35. def draw(self, context):
  36. node_tree = None
  37. # just gonna do the same thing we do in poll operators
  38. if not context.space_data:
  39. return
  40. if not hasattr(context.space_data, "path"):
  41. return
  42. try:
  43. node_tree = context.space_data.path[0].node_tree
  44. except IndexError: # not in the UI, for example, in a script instead.
  45. return
  46. if node_tree is None: # because the space is right but there is no selected tree.
  47. return
  48. # now we're clear to do the menu function
  49. layout = self.layout
  50. from bpy import data
  51. for ng in data.node_groups:
  52. if ng.bl_idname in ['MantisTree', 'SchemaTree']:
  53. if ng.name == node_tree.name and ng.bl_idname == node_tree.bl_idname:
  54. continue # don't add the node group into itself.
  55. if ng.contains_tree(node_tree):
  56. continue # don't create an infinite loop of node trees
  57. operator_settings = layout.operator(
  58. "mantis.add_component", text=ng.name,
  59. icon='NODE', emboss=False,)
  60. operator_settings.node_group_tree_name=ng.name
  61. operator_settings.tree_invoked = node_tree.name
  62. class MantisAddArmature(Menu):
  63. """Menu to show the user's component pack library in the Add Armature menu"""
  64. bl_idname= "VIEW3D_MT_armature_add_mantis_packs"
  65. bl_label = "Mantis Rigs"
  66. def draw(self, context):
  67. # start showing the mantis packs
  68. from .preferences import get_bl_addon_object
  69. bl_mantis_addon = get_bl_addon_object()
  70. if bl_mantis_addon:
  71. components_path = bl_mantis_addon.preferences.ComponentsLibraryFolder
  72. # now we're clear to do the menu function
  73. layout = self.layout
  74. from .utilities import get_component_library_items
  75. component_items = get_component_library_items()
  76. for component_item in component_items:
  77. # how to "EXEC_DEFAULT" here?
  78. operator_settings = layout.operator( operator="mantis.import_tree_no_menu", text=component_item[1])
  79. from os import path as os_path
  80. operator_settings.filepath = os_path.join(components_path, component_item[0])
  81. class MantisActiveTreePanel(Panel):
  82. """N-Panel menu for Mantis"""
  83. bl_label = "Active Tree"
  84. bl_idname = "MANTIS_PT_active_tree"
  85. bl_space_type = 'NODE_EDITOR'
  86. bl_region_type = 'UI'
  87. bl_category = "Mantis"
  88. bl_context = "scene"
  89. @classmethod
  90. def poll(cls, context):
  91. area = context.area
  92. if not area: return False
  93. if not area.spaces: return False
  94. spc = area.spaces[0]
  95. if spc.type != "NODE_EDITOR":
  96. return False
  97. if not spc.node_tree:
  98. return False
  99. if spc.node_tree.bl_idname != "MantisTree":
  100. return False
  101. return True
  102. def draw(self, context):
  103. area = context.area
  104. spc = area.spaces[0]
  105. nt = spc.node_tree
  106. layout = self.layout
  107. layout.label(text=f"Tree Hash: {nt.hash}")
  108. layout.prop(nt, "do_live_update", text='Live Updates',)
  109. layout.operator("mantis.invalidate_node_tree")
  110. layout.operator("mantis.execute_node_tree")
  111. layout.operator("mantis.force_display_update", text='Force Display Update')
  112. layout.operator("mantis.import_from_component_library")