menu_classes.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from bpy.types import Panel, Menu
  2. def TellClasses():
  3. return [
  4. MantisActiveTreePanel,
  5. MantisNodeGroupsMenu,
  6. ]
  7. # Function to append submenu to context menu
  8. def node_context_menu_draw(self, context):
  9. layout = self.layout
  10. layout.separator() # Optional: Adds a separator before your submenu
  11. layout.operator_context = "INVOKE_DEFAULT"
  12. layout.operator("mantis.nodes_cleanup", text='Sort Selected Nodes')
  13. layout.operator("mantis.connect_nodes_to_input")
  14. layout.operator("mantis.select_nodes_of_type")
  15. layout.operator("mantis.import_from_component_library")
  16. # layout.menu('NODE_MT_context_menu_mantis')
  17. # Function to append submenu to node add menu
  18. def node_add_menu_draw(self, context):
  19. # NODE_MT_add
  20. layout = self.layout
  21. layout.separator() # Optional: Adds a separator before your submenu
  22. layout.menu("NODE_MT_add_mantis_groups")
  23. # layout.menu('NODE_MT_context_menu_mantis')
  24. class MantisNodeGroupsMenu(Menu):
  25. """Menu to show available node groups"""
  26. bl_idname= "NODE_MT_add_mantis_groups"
  27. bl_label = "Components"
  28. def draw(self, context):
  29. node_tree = None
  30. # just gonna do the same thing we do in poll operators
  31. if not context.space_data:
  32. return
  33. if not hasattr(context.space_data, "path"):
  34. return
  35. try:
  36. node_tree = context.space_data.path[0].node_tree
  37. except IndexError: # not in the UI, for example, in a script instead.
  38. return
  39. if node_tree is None: # because the space is right but there is no selected tree.
  40. return
  41. # now we're clear to do the menu function
  42. layout = self.layout
  43. from bpy import data
  44. for ng in data.node_groups:
  45. if ng.bl_idname in ['MantisTree', 'SchemaTree']:
  46. if ng.name == node_tree.name and ng.bl_idname == node_tree.bl_idname:
  47. continue # don't add the node group into itself.
  48. if ng.contains_tree(node_tree):
  49. continue # don't create an infinite loop of node trees
  50. operator_settings = layout.operator(
  51. "mantis.add_component", text=ng.name,
  52. icon='NODE', emboss=False,)
  53. operator_settings.node_group_tree_name=ng.name
  54. operator_settings.tree_invoked = node_tree.name
  55. class MantisActiveTreePanel(Panel):
  56. """N-Panel menu for Mantis"""
  57. bl_label = "Active Tree"
  58. bl_idname = "MANTIS_PT_active_tree"
  59. bl_space_type = 'NODE_EDITOR'
  60. bl_region_type = 'UI'
  61. bl_category = "Mantis"
  62. bl_context = "scene"
  63. @classmethod
  64. def poll(cls, context):
  65. area = context.area
  66. if not area: return False
  67. if not area.spaces: return False
  68. spc = area.spaces[0]
  69. if spc.type != "NODE_EDITOR":
  70. return False
  71. if not spc.node_tree:
  72. return False
  73. if spc.node_tree.bl_idname != "MantisTree":
  74. return False
  75. return True
  76. def draw(self, context):
  77. area = context.area
  78. spc = area.spaces[0]
  79. nt = spc.node_tree
  80. layout = self.layout
  81. layout.label(text=f"Tree Hash: {nt.hash}")
  82. layout.prop(nt, "do_live_update", text='Live Updates',)
  83. layout.operator("mantis.invalidate_node_tree")
  84. layout.operator("mantis.execute_node_tree")
  85. layout.operator("mantis.force_display_update", text='Force Display Update')
  86. layout.operator("mantis.import_from_component_library")