internal_nodes.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from .utilities.node_common import *
  2. from bpy.types import Node
  3. from .base_definitions import MantisNode, MantisSocketTemplate
  4. from uuid import uuid4
  5. # this function is kind of confusing and is very important,
  6. # so it bears a full explanation: its purpose is to connect
  7. # the links going into a group to the nodes in that group.
  8. # FIRST we connect all the incoming links into the Group Node to
  9. # a Group Interface node that does nothing but mark the entrance.
  10. # Then, we connect all the outgoing links back to the nodes
  11. # that had incoming links, so the nodes OUTSIDE the Node Group
  12. # are connected directly to BOTH the GroupInterface and the
  13. # nodes INSIDE the node group.
  14. # we give the GroupInterface nodes an obscenely high
  15. # multi_input_sort_id so that they are always last.
  16. # but since these links are going IN, they shouldn't cause any problems.
  17. # the sub_sort_id is set here in case there are UI links which represent
  18. # multiple Mantis links - the mantis links are sorted within the UI links
  19. # and the UI links are sorted as normal, so all the links are in the right
  20. # order.... probably. BUG here?
  21. # I want the Group Interface nodes to be part of the hierarchy...
  22. # but I want to cut the links. hmmm what to do? Fix it if it causes problems.
  23. # solution to hypothetical BUG could be to do traversal on the links
  24. # instead of the sockets.
  25. # NOTE: using NodeSocket.links is slow, it should be cached
  26. def grp_node_reroute_common(in_node, out_node, interface):
  27. from .base_definitions import links_sort_key
  28. for in_node_input in in_node.inputs:
  29. i = 0
  30. if len(in_node_input.links)>1: # sort here to ensure correct sub_sort_id
  31. in_node_input.links.sort(key=links_sort_key)
  32. while (in_node_input.links):
  33. in_link = in_node_input.links.pop()
  34. from_node = in_link.from_node; from_socket = in_link.from_socket
  35. link = from_node.outputs[from_socket].connect(
  36. interface,in_node_input.name, sort_id = 2**16, sub_sort_id=i)
  37. i += 1; in_link.die()
  38. for out_node_output in out_node.outputs:
  39. while (out_node_output.links):
  40. out_link = out_node_output.links.pop()
  41. to_node = out_link.to_node; to_socket = out_link.to_socket
  42. for j, l in enumerate(interface.inputs[out_node_output.name].links):
  43. # we are connecting the link from the ORIGINAL output to the FINAL input.
  44. link = l.from_node.outputs[l.from_socket].connect(
  45. to_node, to_socket, sort_id = out_link.multi_input_sort_id)
  46. link.sub_sort_id = j
  47. out_link.die()
  48. def reroute_links_grp(group, all_nodes):
  49. interface = all_nodes.get( ( *group.signature, "InputInterface") )
  50. if group.inputs:
  51. if group_input := all_nodes.get(( *group.signature, "NodeGroupInput")):
  52. grp_node_reroute_common(group, group_input, interface)
  53. else:
  54. raise RuntimeError("internal error: failed to enter a node group ")
  55. def reroute_links_grpout(group_output, all_nodes):
  56. if (group_output_node := all_nodes.get( ( *group_output.signature[:-1],) )):
  57. interface = all_nodes.get( ( *group_output_node.signature, "OutputInterface") )
  58. grp_node_reroute_common(group_output, group_output_node, interface)
  59. else:
  60. prOrange(f"WARN: unconnected outputs from a node group "
  61. "(maybe you are running the tree from inside a node group?)")
  62. # TODO: make node_type a class property like it is for everything else
  63. # I can't do this without making some changes to schema and anything that
  64. # checks for DUMMY SCHEMA
  65. class DummyNode(MantisNode):
  66. __slots__ = ("node_type", "ui_node", "solver", "uuid", "reroute_links_type")
  67. def __init__(self, signature, base_tree, ui_node = None, ui_signature=None):
  68. super().__init__(signature, base_tree)
  69. self.ui_node = ui_node
  70. self.prepared = True
  71. self.uuid = uuid4()
  72. self.solver = None
  73. self.node_type = 'DUMMY'
  74. self.reroute_links_type=""
  75. if ui_node:
  76. if ui_node.bl_idname in ["MantisSchemaGroup"]:
  77. self.node_type = 'DUMMY_SCHEMA'
  78. self.prepared = False
  79. for sock in ui_node.inputs:
  80. if sock.identifier == "__extend__" or sock.name == "__extend__":
  81. continue
  82. self.inputs[sock.identifier] = NodeSocket(is_input = True, name = sock.identifier, node = self)
  83. for sock in ui_node.outputs:
  84. if sock.identifier == "__extend__" or sock.name == "__extend__":
  85. continue
  86. self.outputs[sock.identifier] = NodeSocket(is_input = False, name = sock.identifier, node = self)
  87. self.parameters[sock.identifier]=None
  88. # keep track of the "natural signature" of Schema nodes - so that they are unambiguous
  89. self.ui_signature=self.signature
  90. if ui_signature:
  91. self.ui_signature=ui_signature
  92. # This is necessary for Schema to work if there are multiple Schema nodes using the same Schema tree.
  93. # this is ugly and I hate it.
  94. self.execution_prepared=True # in case it gets left behind in the tree as a dependency
  95. def reroute_links(self, all_nodes):
  96. match self.reroute_links_type:
  97. case "group":
  98. reroute_links_grp(self, all_nodes)
  99. case "out":
  100. reroute_links_grpout(self, all_nodes)
  101. class NoOpNode(MantisNode):
  102. __slots__ = ()
  103. node_type = 'UTILITY'
  104. def __init__(self, signature, base_tree):
  105. super().__init__(signature, base_tree)
  106. self.inputs['Input'] = NodeSocket(is_input=True, node=self, name='Input')
  107. self.outputs['Output'] = NodeSocket(is_input=False, node=self, name='Output')
  108. self.init_parameters()
  109. self.set_traverse([("Input", "Output")])
  110. self.prepared, self.executed = True, True
  111. self.execution_prepared=True
  112. # this node is useful for me to insert in the tree and use for debugging especially connections.
  113. class AutoGenNode(MantisNode):
  114. __slots__ = ()
  115. node_type = 'UTILITY'
  116. def __init__(self, signature, base_tree):
  117. super().__init__(signature, base_tree)
  118. self.prepared, self.executed = True, True
  119. self.execution_prepared=True
  120. def reset_execution(self):
  121. super().reset_execution()
  122. self.prepared, self.executed = True, True
  123. # The Group Interface node is responsible for gathering node connections
  124. # going in or out of the group and connecting back out the other side
  125. # this is also where caching and overlays live
  126. class GroupInterface(MantisNode):
  127. __slots__ = ("in_out")
  128. node_type = 'INTERFACE'
  129. def __init__(self, signature, base_tree, in_out, ui_node = None, items_tree = None):
  130. # DO: refactor me to use the tree interface
  131. super().__init__(signature, base_tree)
  132. self.prepared, self.executed = True, True
  133. self.in_out = in_out
  134. # init the sockets based on in/out, then set up traversal
  135. # the index and length are like constant inputs.
  136. if self.in_out == 'SCHEMA_INPUT':
  137. # this one is here because the tree expects it when linking things together
  138. self.outputs["Schema Length"] = NodeSocket(is_input=False, node=self, name="Schema Length")
  139. # HACK this one here is just there for overlays, really, doesn't hurt
  140. self.outputs["Length"] = self.outputs["Schema Length"]
  141. if 'SCHEMA' in self.in_out:
  142. self.outputs["Index"] = NodeSocket(is_input=False, node=self, name="Index")
  143. for i, item in enumerate(items_tree):
  144. if item.item_type == 'PANEL': continue
  145. is_connection = False; is_array = False
  146. if hasattr(item, 'is_connection'): # in older files and for some types this happens
  147. is_connection = item.is_connection # BUG really every socket should be a Mantis interface type
  148. is_array = item.is_array
  149. if (self.in_out in {'INPUT', 'OUTPUT'} and item.in_out == self.in_out):
  150. source = self.inputs[item.identifier] = NodeSocket(is_input=True, node=self, name=item.identifier)
  151. target = self.outputs[item.identifier] = NodeSocket(is_input=False, node=self, name=item.identifier)
  152. self.set_traverse( [(source.name, target.name)] )
  153. elif self.in_out == 'SCHEMA_INPUT':
  154. if is_array: continue # I'll skip these for now
  155. source = self.inputs[item.identifier] = NodeSocket(is_input=True, node=self, name=item.identifier)
  156. target = self.outputs[item.identifier] = NodeSocket(is_input=False, node=self, name=item.identifier)
  157. self.set_traverse( [(source.name, target.name)] )
  158. elif self.in_out == 'SCHEMA_BETWEEN':
  159. if is_connection: # we need these at every in-between because they are modified between
  160. source = self.inputs[item.identifier] = NodeSocket(is_input=True, node=self, name=item.identifier)
  161. target = self.outputs[item.identifier] = NodeSocket(is_input=False, node=self, name=item.identifier)
  162. self.set_traverse( [(source.name, target.name)] )
  163. self.execution_prepared=True