menu_classes.py 5.1 KB

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