internal_nodes.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = GroupInterface(
  50. ( *group.signature, "InputInterface"),
  51. group.base_tree, group.ui_node, 'INPUT',)
  52. all_nodes[interface.signature] = interface
  53. if group.inputs:
  54. if group_input := all_nodes.get(( *group.signature, "NodeGroupInput")):
  55. grp_node_reroute_common(group, group_input, interface)
  56. else:
  57. raise RuntimeError("internal error: failed to enter a node group ")
  58. def reroute_links_grpout(group_output, all_nodes):
  59. if (group := all_nodes.get( ( *group_output.signature[:-1],) )):
  60. interface = GroupInterface(
  61. ( *group.signature, "OutputInterface"),
  62. group.base_tree, group.ui_node, 'OUTPUT',)
  63. all_nodes[interface.signature] = interface
  64. grp_node_reroute_common(group_output, group, interface)
  65. else:
  66. prOrange(f"WARN: unconnected outputs from a node group "
  67. "(maybe you are running the tree from inside a node group?)")
  68. # TODO: make node_type a class property like it is for everything else
  69. # I can't do this without making some changes to schema and anything that
  70. # checks for DUMMY SCHEMA
  71. class DummyNode(MantisNode):
  72. __slots__ = ("node_type", "ui_node", "solver", "uuid", "reroute_links_type")
  73. def __init__(self, signature, base_tree, ui_node = None, ui_signature=None):
  74. super().__init__(signature, base_tree)
  75. self.ui_node = ui_node
  76. self.prepared = True
  77. self.uuid = uuid4()
  78. self.solver = None
  79. self.node_type = 'DUMMY'
  80. self.reroute_links_type='group' # group, out, schema
  81. if ui_node:
  82. if ui_node.bl_idname in ["MantisSchemaGroup"]:
  83. self.node_type = 'DUMMY_SCHEMA'
  84. self.prepared = False
  85. for sock in ui_node.inputs:
  86. if sock.identifier == "__extend__" or sock.name == "__extend__":
  87. continue
  88. self.inputs[sock.identifier] = NodeSocket(is_input = True, name = sock.identifier, node = self)
  89. for sock in ui_node.outputs:
  90. if sock.identifier == "__extend__" or sock.name == "__extend__":
  91. continue
  92. self.outputs[sock.identifier] = NodeSocket(is_input = False, name = sock.identifier, node = self)
  93. self.parameters[sock.identifier]=None
  94. # keep track of the "natural signature" of Schema nodes - so that they are unambiguous
  95. self.ui_signature=self.signature
  96. if ui_signature:
  97. self.ui_signature=ui_signature
  98. # This is necessary for Schema to work if there are multiple Schema nodes using the same Schema tree.
  99. # this is ugly and I hate it.
  100. self.execution_prepared=True # in case it gets left behind in the tree as a dependency
  101. def reroute_links(self, all_nodes):
  102. match self.reroute_links_type:
  103. case "group":
  104. reroute_links_grp(self, all_nodes)
  105. case "schema":
  106. pass # gonna implement this later
  107. case "out":
  108. reroute_links_grpout(self, all_nodes)
  109. class NoOpNode(MantisNode):
  110. __slots__ = ()
  111. node_type = 'UTILITY'
  112. def __init__(self, signature, base_tree):
  113. super().__init__(signature, base_tree)
  114. self.inputs['Input'] = NodeSocket(is_input=True, node=self, name='Input')
  115. self.outputs['Output'] = NodeSocket(is_input=False, node=self, name='Output')
  116. self.init_parameters()
  117. self.set_traverse([("Input", "Output")])
  118. self.prepared, self.executed = True, True
  119. self.execution_prepared=True
  120. # this node is useful for me to insert in the tree and use for debugging especially connections.
  121. class AutoGenNode(MantisNode):
  122. __slots__ = ()
  123. node_type = 'UTILITY'
  124. def __init__(self, signature, base_tree):
  125. super().__init__(signature, base_tree)
  126. self.prepared, self.executed = True, True
  127. self.execution_prepared=True
  128. def reset_execution(self):
  129. super().reset_execution()
  130. self.prepared, self.executed = True, True
  131. # The Group Interface node is responsible for gathering node connections
  132. # going in or out of the group and connecting back out the other side
  133. # this is also where caching and overlays live
  134. class GroupInterface(MantisNode):
  135. __slots__ = ("in_out")
  136. node_type = 'INTERFACE'
  137. def __init__(self, signature, base_tree, ui_node, in_out):
  138. super().__init__(signature, base_tree)
  139. self.prepared, self.executed = True, True
  140. self.in_out = in_out
  141. # init the sockets based on in/out, then set up traversal
  142. collection = ui_node.inputs if in_out == 'INPUT' else ui_node.outputs
  143. for socket in collection:
  144. self.inputs[socket.identifier] = NodeSocket(is_input=True, node=self, name=socket.identifier)
  145. self.outputs[socket.identifier] = NodeSocket(is_input=False, node=self, name=socket.identifier)
  146. for socket in self.inputs.keys(): self.set_traverse( [(socket, socket)] )
  147. self.execution_prepared=True