ops_ui.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # gonna start putting the UI operators in here so
  2. # that the nodegroup operators file is more focused
  3. import bpy
  4. from bpy.types import Operator
  5. from .utilities import (prRed, prGreen, prPurple, prWhite,
  6. prOrange,
  7. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  8. wrapOrange,)
  9. def TellClasses():
  10. return [
  11. MantisAddComponent,
  12. ColorPalleteAddSocket,
  13. ColorPalleteRemoveSocket,
  14. ]
  15. def poll_in_mantis_tree(context):
  16. if not context.space_data:
  17. return False
  18. if not hasattr(context.space_data, "path"):
  19. return False
  20. try:
  21. node_tree = context.space_data.path[0].node_tree
  22. except IndexError: # not in the UI, for example, in a script instead.
  23. return False
  24. if node_tree is None: # because the space is right but there is no selected tree.
  25. return False
  26. return True
  27. class MantisAddComponent(Operator):
  28. """Add a component group into the scene"""
  29. bl_idname = "mantis.add_component"
  30. bl_label = "Add Component Group"
  31. bl_options = {'REGISTER', 'UNDO'}
  32. node_group_tree_name : bpy.props.StringProperty()
  33. tree_invoked : bpy.props.StringProperty(options ={'HIDDEN'})
  34. @classmethod
  35. def poll(cls, context):
  36. return poll_in_mantis_tree(context)
  37. def invoke(self, context, event):
  38. return self.execute(context)
  39. def execute (self, context):
  40. from bpy import data
  41. tree = bpy.data.node_groups[self.tree_invoked]
  42. for node in tree.nodes:
  43. node.select = False
  44. tree = bpy.data.node_groups[self.tree_invoked]
  45. node_group = data.node_groups.get(self.node_group_tree_name)
  46. node_group_type = "MantisNodeGroup"
  47. if node_group.bl_idname == 'SchemaTree':
  48. node_group_type = "MantisSchemaGroup"
  49. node = tree.nodes.new(node_group_type)
  50. node.select = True
  51. tree.nodes.active = node
  52. node.node_tree = node_group
  53. node.location = context.space_data.cursor_location
  54. return bpy.ops.node.translate_attach_remove_on_cancel('INVOKE_DEFAULT',)
  55. # TRANSFORM_OT_translate={ "value":
  56. # (context.space_data.cursor_location[0]*1.25,
  57. # context.space_data.cursor_location[1]*1.25, 0)})
  58. # TODO: figure out how to make this feel nice in the UI
  59. # bpy.ops.node.translate_attach_remove_on_cancel(TRANSFORM_OT_translate={"value":(-59.247, -5.7344, 0), "orient_type":'GLOBAL', "orient_matrix":((1, 0, 0), (0, 1, 0), (0, 0, 1)), "orient_matrix_type":'GLOBAL', "constraint_axis":(False, False, False), "mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_elements":{'GRID'}, "use_snap_project":False, "snap_target":'CLOSEST', "use_snap_self":True, "use_snap_edit":True, "use_snap_nonedit":True, "use_snap_selectable":False, "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":True, "use_duplicated_keyframes":False, "view2d_edge_pan":True, "release_confirm":False, "use_accurate":False, "use_automerge_and_split":False, "translate_origin":False}, NODE_OT_attach={})
  60. class ColorPalleteAddSocket(Operator):
  61. """Add a Color Set socket to the node"""
  62. bl_idname = "mantis.color_pallete_socket_add"
  63. bl_label = "Add Color Set"
  64. bl_options = {'REGISTER', 'UNDO'}
  65. tree_invoked : bpy.props.StringProperty(options ={'HIDDEN'})
  66. node_invoked : bpy.props.StringProperty(options ={'HIDDEN'})
  67. @classmethod
  68. def poll(cls, context):
  69. return True
  70. def execute(self, context):
  71. n = bpy.data.node_groups[self.tree_invoked].nodes[self.node_invoked]
  72. #name them uniquely
  73. number=0
  74. for o in n.outputs:
  75. if int(o.name[-3:]) == number:
  76. number = int(o.name[-3:]) + 1
  77. else: # my intention is to use the first number that isn't used
  78. break # so break here because we have reached the end or a 'gap'
  79. n.outputs.new("ColorSetSocket", "Color Set."+str(number).zfill(3))
  80. return {'FINISHED'}
  81. class ColorPalleteRemoveSocket(Operator):
  82. """Remove a Color Set socket from the node"""
  83. bl_idname = "mantis.color_pallete_socket_remove"
  84. bl_label = "X"
  85. bl_options = {'REGISTER', 'UNDO'}
  86. tree_invoked : bpy.props.StringProperty(options = {'HIDDEN'})
  87. node_invoked : bpy.props.StringProperty(options = {'HIDDEN'})
  88. socket_invoked : bpy.props.StringProperty(options = {'HIDDEN'})
  89. @classmethod
  90. def poll(cls, context):
  91. return True
  92. def execute(self, context):
  93. n = bpy.data.node_groups[self.tree_invoked].nodes[self.node_invoked]
  94. # the name doesn't matter, will probably be Color Set.001 or something instead
  95. for s in n.outputs:
  96. if s.identifier == self.socket_invoked:
  97. break
  98. n.outputs.remove(s)
  99. return {'FINISHED'}