schema_solve.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. from ..utilities.utilities import (prRed, prGreen, prPurple, prWhite,
  2. prOrange,
  3. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  4. wrapOrange,)
  5. from ..utilities.utilities import init_connections, init_dependencies, get_link_in_out
  6. from ..base_definitions import (SchemaUINode, custom_props_types, \
  7. MantisNodeGroup, SchemaGroup, replace_types, GraphError, links_sort_key)
  8. from ..utilities.node_common import setup_custom_props_from_np
  9. # a class that solves Schema nodes
  10. from bpy.types import NodeGroupInput, NodeGroupOutput, NodeSocket
  11. from .readtree import execution_error_cleanup
  12. class SchemaSolver:
  13. __slots__ = (
  14. "all_nodes", "node", "solved", "tree",
  15. "uuid", "error_popups", "signature",
  16. "schema_nodes", "solved_nodes",
  17. "incoming_connections", "outgoing_connections",
  18. "constant_in", "constant_out",
  19. "array_input_connections", "array_output_connections",
  20. "nested_schemas", "autogenerated_nodes",
  21. "held_links", "tree_path_names",
  22. "autogen_path_names", "is_node_group",
  23. "index_link", "solve_length", "index",
  24. "interfaces", "index_str", "prev_index_str",
  25. "current_interface", "next_interface"
  26. )
  27. def __init__(self, schema_dummy, nodes, ui_node, signature=None, error_popups=False):
  28. self.all_nodes = nodes # this is the parsed tree from Mantis
  29. self.node = schema_dummy
  30. self.node.solver = self
  31. self.solved = False
  32. self.tree = ui_node.node_tree
  33. self.uuid = self.node.uuid
  34. self.error_popups = error_popups
  35. if signature:
  36. self.signature = signature
  37. else:
  38. self.signature = self.node.signature
  39. self.schema_nodes={}; self.solved_nodes = {}
  40. self.incoming_connections = {}; self.outgoing_connections = {}
  41. self.constant_in = {}; self.constant_out = {}
  42. self.array_input_connections = {}; self.array_output_connections = {}
  43. self.nested_schemas = {}; self.autogenerated_nodes = {}
  44. self.held_links = []
  45. self.tree_path_names = [*self.node.signature] # same tree as the schema node
  46. self.autogen_path_names = ['SCHEMA_AUTOGENERATED', *self.node.signature[1:]]
  47. self.is_node_group = False
  48. if self.node.ui_node.bl_idname == "MantisNodeGroup":
  49. self.is_node_group = True
  50. if self.node.inputs['Schema Length'].links:
  51. self.index_link = self.node.inputs['Schema Length'].links[0]
  52. else:
  53. self.index_link = None
  54. # this should never happen, but it's OK to check.
  55. if self.node.inputs["Schema Length"].is_linked:
  56. if (other := self.node.inputs['Schema Length'].links[0].from_node).prepared == False:
  57. raise RuntimeError(f"Schema Length cannot be determined for {self.node} because {other} is not prepared.")
  58. self.solve_length = self.node.evaluate_input("Schema Length")
  59. # The user may be using an overlay variable to set the schema length
  60. from .overlays import node_scan_overlays
  61. if isinstance(self.solve_length, str):
  62. self.solve_length = node_scan_overlays(self.node, 'Schema Length')
  63. if self.solve_length == 0: return
  64. elif self.solve_length < 0:
  65. raise GraphError(f"Schema Length of less than 0 is not allowed in {self.node}")
  66. # I'm making this a property of the solver because the solver's data is modified as it solves each iteration
  67. self.index = 0
  68. self.interfaces = []
  69. prWhite(f"\nExpanding schema {self.tree.name} in node {self.node.signature}"
  70. f" with length {self.solve_length}.")
  71. self.init_schema_links()
  72. self.generate_interface_nodes()
  73. self.set_schema_per_interation_constants()
  74. # Sort the multi-input nodes in reverse order of ID, this ensures that they are
  75. # read in the order they were created
  76. for inp in self.node.inputs.values():
  77. inp.links.sort(key=links_sort_key)
  78. for ui_node in self.tree.nodes:
  79. # Make the mantis nodes for the special interface types
  80. # we use the bl_idname because all schema nodes should be single-instance
  81. signature = (*self.tree_path_names, ui_node.bl_idname)
  82. if isinstance(ui_node, (SchemaUINode, NodeGroupInput, NodeGroupOutput)):
  83. # We use the schema node's "natural signature" here because it represents
  84. # the "original" signature of the schema UI group node since this schema
  85. # solver may be in a nested schema, and its node's signature may have
  86. # uuid/index attached.
  87. get_sig = (*self.node.ui_signature, ui_node.bl_idname)
  88. if not (mantis_node := self.all_nodes.get(get_sig)):
  89. raise RuntimeError(wrapRed(f"Not found: {get_sig}"))
  90. self.schema_nodes[signature] = mantis_node
  91. # HACK to make Group Nodes work
  92. if ui_node.bl_idname == "NodeGroupInput":
  93. from ..schema_nodes import SchemaConstInput
  94. mantis_node = SchemaConstInput(signature=signature, base_tree=self.node.base_tree, parent_schema_node=self.node)
  95. self.schema_nodes[signature] = mantis_node
  96. if ui_node.bl_idname == "NodeGroupOutput":
  97. from ..schema_nodes import SchemaConstOutput
  98. mantis_node = SchemaConstOutput(signature=signature, base_tree=self.node.base_tree, parent_schema_node=self.node)
  99. self.schema_nodes[signature] = mantis_node
  100. def init_schema_links(self,):
  101. """ Sort and store the links to/from the Schema group node."""
  102. for item in self.tree.interface.items_tree:
  103. if item.item_type == 'PANEL': continue
  104. from ..utilities.utilities import read_schema_type
  105. parent_name = read_schema_type(item)
  106. # just gonna try and make the
  107. match parent_name:
  108. case 'Connection':
  109. if item.in_out == 'INPUT':
  110. if incoming_links := self.node.inputs[item.identifier].links:
  111. self.incoming_connections[item.name] = incoming_links[0]
  112. else:
  113. self.incoming_connections[item.name] = None
  114. else: # OUTPUT
  115. if outgoing_links := self.node.outputs[item.identifier].links:
  116. self.outgoing_connections[item.name] = outgoing_links.copy()
  117. else:
  118. self.outgoing_connections[item.name] = []
  119. case 'Constant':
  120. if item.in_out == 'INPUT':
  121. if constant_in_links := self.node.inputs[item.identifier].links:
  122. self.constant_in[item.name] = constant_in_links[0]
  123. else:
  124. self.constant_in[item.name] = None
  125. else: # OUTPUT
  126. if constant_out_links := self.node.outputs[item.identifier].links:
  127. self.constant_out[item.name] = constant_out_links.copy()
  128. else:
  129. self.constant_out[item.name] = []
  130. case 'Array':
  131. if item.in_out == 'INPUT':
  132. if item.identifier not in self.array_input_connections.keys():
  133. self.array_input_connections[item.identifier]=[]
  134. if in_links := self.node.inputs[item.identifier].links:
  135. self.array_input_connections[item.identifier]=in_links.copy()
  136. # I am tempted to put a check here, but it is sufficient to
  137. # rely on hierarchy links ensuring the arrays are prepared.
  138. # and testing here for un-prepared arrays will mess up schema
  139. # relationships in non-hierarchy situations.
  140. # Still, I'd like to have an easier time catching these problems.
  141. else: # OUTPUT
  142. if item.identifier not in self.array_output_connections.keys():
  143. self.array_output_connections[item.identifier]=[]
  144. if out_links := self.node.outputs[item.identifier].links:
  145. self.array_output_connections[item.identifier] = out_links.copy()
  146. # Using lambdas here causes a lot of function calls, should be optimized out
  147. # since after all this only needs to change when the index updates
  148. def set_schema_per_interation_constants(self):
  149. # NOTE: I removed the UUID that has always been here
  150. # I can't figure out why I needed it and as far as I can tell there
  151. # really is no way for this to have name clashes. Tests work OK, too.
  152. self.index_str = '.'+str(self.index).zfill(4)
  153. self.prev_index_str = '.'+str(self.index-1).zfill(4)
  154. self.current_interface = self.interfaces[self.index]
  155. self.next_interface = self.interfaces[self.index + 1] # this is guaranteed
  156. if self.is_node_group:
  157. self.index_str = ''; self.prev_index_str = ''
  158. def generate_interface_nodes(self):
  159. from ..internal_nodes import GroupInterface
  160. # One input interface per iteration, and one output interface
  161. items_tree = self.tree.interface.items_tree
  162. mContext=self.node.mContext
  163. for i in range(self.solve_length): # one input per
  164. index_str = '.'+str(i).zfill(4)
  165. name = 'InputInterface'+index_str
  166. if i == 0: in_out = 'SCHEMA_INPUT'
  167. else: in_out = 'SCHEMA_BETWEEN'
  168. signature = (None, *self.autogen_path_names[1:], name)
  169. if not (interface := self.all_nodes.get( signature)):
  170. interface = GroupInterface(signature=signature, base_tree=self.tree, in_out=in_out,
  171. mContext=mContext, ui_node=self.node.ui_node, items_tree=items_tree)
  172. interface.generated_at_schema_index = self.node.generated_at_schema_index + [i]
  173. # add the index and length socket constants. Only the first one needs Length
  174. # (it's best to avoid having multiple places to read single-source values.)
  175. interface.parameters['Index'] = i
  176. if i == 0: interface.parameters['Length'] = self.solve_length
  177. # the GroupInterface will initialize with the right sockets, but we still have to link them
  178. if in_out == 'SCHEMA_INPUT': # Take the links into the schema and plug them here
  179. for item in items_tree:
  180. if item.item_type == 'PANEL': continue
  181. if (mantis_link := self.constant_in.get(item.name)):
  182. mantis_link.from_node.outputs[mantis_link.from_socket].connect(interface, item.identifier)
  183. # for mantis_link in self.array_input_connections.get(item.name, []):
  184. # mantis_link.from_node.outputs[mantis_link.from_socket].connect(interface, item.identifier)
  185. if (mantis_link := self.incoming_connections.get(item.name)):
  186. mantis_link.from_node.outputs[mantis_link.from_socket].connect(interface, item.identifier)
  187. self.interfaces.append(interface)
  188. # solved nodes will get added to the outer all_nodes later
  189. self.solved_nodes[signature]=interface
  190. # now do the output
  191. signature = (None, *self.autogen_path_names[1:], "OutputInterface" + index_str)
  192. interface = GroupInterface(signature=signature, base_tree=self.tree, in_out='OUTPUT',
  193. mContext=mContext, ui_node=self.node.ui_node, items_tree=items_tree)
  194. self.interfaces.append(interface)
  195. interface.generated_at_schema_index = i
  196. self.solved_nodes[signature]=interface
  197. # strip the AUTOGEN/SCHEMA tag if it is there
  198. def is_node_deeper_nested(self, queried_node, compare_node):
  199. return len(compare_node.signature) < len(queried_node.signature)
  200. def gen_solve_iteration_mantis_nodes(self, frame_mantis_nodes, unprepared):
  201. for ui_node in self.tree.nodes:
  202. mantis_node_name = ui_node.name
  203. index_str = self.index_str
  204. mContext=self.node.mContext
  205. if isinstance(ui_node, SchemaUINode):
  206. continue # IGNORE the schema interface nodes, we already made them in __init__()
  207. # they are reused for each iteration.
  208. elif ui_node.bl_idname in ['NodeFrame', 'NodeReroute']:
  209. continue # IGNORE stuff that is purely UI - frames, reroutes.
  210. elif ui_node.bl_idname in ['NodeGroupInput', 'NodeGroupOutput']:
  211. continue # we converted these to Schema Nodes because they represent a Group input.
  212. signature = (*self.autogen_path_names, mantis_node_name+index_str)
  213. ui_signature=(*self.signature, mantis_node_name)
  214. prototype_mantis_node = self.all_nodes[ui_signature]
  215. # the prototype_mantis_node was generated inside the schema when we parsed the tree.
  216. # it is the ui_node of the mantis node which we make for this iteration
  217. # for Schema sub-nodes ... they need a ui_node to init.
  218. if prototype_mantis_node.node_type in ['DUMMY', 'DUMMY_SCHEMA']:
  219. # We stored the ui_node ui_node when creating the Mantis node.
  220. ui_node = prototype_mantis_node.ui_node
  221. # if prototype_mantis_node is a group or schema: TODO changes are needed elsewhere to make this easier to read. LEGIBILITY
  222. if ui_node.bl_idname in ["MantisNodeGroup", "MantisSchemaGroup"]:
  223. mantis_node = prototype_mantis_node.__class__(
  224. signature, prototype_mantis_node.base_tree, ui_node=ui_node,
  225. ui_signature = prototype_mantis_node.signature)
  226. # now let's copy the links from the ui_node node
  227. if ui_node.bl_idname in ["MantisNodeGroup"]:
  228. mantis_node.prepared = False
  229. mantis_node.node_type = 'DUMMY_SCHEMA' # we promote it to a schema for now
  230. from ..base_definitions import NodeSocket
  231. mantis_node.inputs['Schema Length'] = NodeSocket(is_input=True, node=mantis_node, name='Schema Length')
  232. mantis_node.parameters['Schema Length'] = 1 # set the length to 1 since it is a single group instance
  233. # we'll make the autogenerated nodes for constant inputs. It doesn't matter that there is technically
  234. # a ui_node available for each one -- these are cheap and I want this to be easy.
  235. from .readtree import make_connections_to_ng_dummy
  236. make_connections_to_ng_dummy(self.node.base_tree, self.autogen_path_names, frame_mantis_nodes, self.all_nodes, mantis_node)
  237. else:
  238. mantis_node = prototype_mantis_node.__class__(signature, prototype_mantis_node.base_tree, ui_node=ui_node)
  239. else:
  240. mantis_node = prototype_mantis_node.__class__(signature, prototype_mantis_node.base_tree)
  241. frame_mantis_nodes[mantis_node.signature] = mantis_node
  242. self.all_nodes[mantis_node.signature] = mantis_node
  243. mantis_node.ui_signature=ui_signature # set the natural signature to ensure we can access from the UI
  244. if mantis_node.prepared == False:
  245. unprepared.append(mantis_node)
  246. if mantis_node.__class__.__name__ in custom_props_types:
  247. setup_custom_props_from_np(mantis_node, ui_node)
  248. # be sure to pass on the Mantis Context to them.
  249. mantis_node.mContext=mContext
  250. mantis_node.generated_at_schema_index = self.node.generated_at_schema_index + [self.index]
  251. mantis_node.fill_parameters(ui_node)
  252. assert mantis_node.mContext is not None
  253. def handle_link_from_index_input(self, index, frame_mantis_nodes, ui_link):
  254. from ..base_definitions import can_remove_socket_for_autogen
  255. _from_name, to_name = get_link_in_out(ui_link)
  256. to_node = frame_mantis_nodes[ (*self.autogen_path_names, to_name+self.index_str) ]
  257. if (not can_remove_socket_for_autogen(to_node, ui_link.to_socket.name)) or \
  258. to_node.node_type in ['DUMMY', 'DUMMY_SCHEMA']:
  259. from_node = self.current_interface
  260. connection = from_node.outputs[ui_link.from_socket.name].connect(node=to_node, socket=ui_link.to_socket.identifier)
  261. connection.is_hierarchy = False # For now this is the easiest way to do this
  262. return
  263. # Since the index is already determined, it is safe to remove the socket and just keep the value.
  264. to_node.parameters[ui_link.to_socket.name] = index
  265. del to_node.inputs[ui_link.to_socket.name]
  266. def handle_link_from_schema_length_input(self, frame_mantis_nodes, ui_link):
  267. # see, here I can just use the schema node
  268. _from_name, to_name = get_link_in_out(ui_link)
  269. if to_name in replace_types:
  270. to_node = self.schema_nodes[(*self.autogen_path_names, to_name)]
  271. else:
  272. to_node = frame_mantis_nodes[(*self.autogen_path_names, to_name+self.index_str)]
  273. # this self.index_link is only used here?
  274. if self.index_link is None:
  275. # this should be impossible because the Schema gets an auto-generated Int input.
  276. raise NotImplementedError(" 241 This code should be unreachable. Please report this as a bug!")
  277. if (self.index_link.from_node):
  278. connection = self.index_link.from_node.outputs[self.index_link.from_socket].connect(node=to_node, socket=ui_link.to_socket.name)
  279. # otherwise we can autogen an int input I guess...?
  280. else:
  281. raise RuntimeError("247 I This code should be unreachable. Please report this as a bug!")
  282. def handle_link_from_incoming_connection_input(self, frame_mantis_nodes, ui_link):
  283. incoming = self.incoming_connections[ui_link.from_socket.name]
  284. if incoming is not None:
  285. from_node = incoming.from_node
  286. _from_name, to_name = get_link_in_out(ui_link)
  287. to_node = frame_mantis_nodes[ (*self.autogen_path_names, to_name+self.index_str) ]
  288. socket_name=ui_link.to_socket.name
  289. if to_node.node_type in [ 'DUMMY_SCHEMA' ]:
  290. socket_name=ui_link.to_socket.identifier
  291. connection = from_node.outputs[incoming.from_socket].connect(node=to_node, socket=socket_name)
  292. init_connections(from_node)
  293. def handle_link_to_outgoing_connection_output(self, frame_mantis_nodes, ui_link,):
  294. mantis_incoming_node = self.schema_nodes[*self.tree_path_names, 'SchemaIncomingConnection']
  295. for mantis_link in mantis_incoming_node.outputs[ui_link.to_socket.name].links:
  296. to_mantis_node, to_socket_name = mantis_link.to_node, mantis_link.to_socket
  297. from_name = get_link_in_out(ui_link)[0]
  298. from_mantis_node = self.solved_nodes[ (*self.autogen_path_names, from_name+self.prev_index_str) ]
  299. to_mantis_node_signature = ( *self.autogen_path_names,
  300. to_mantis_node.signature[-1] + self.index_str )
  301. # we need to detect if the next node is in a group.
  302. # REMEMBER: at this point, nested groups haven't been solved yet.
  303. if to_mantis_node_signature not in frame_mantis_nodes.keys() and \
  304. self.is_node_deeper_nested(to_mantis_node, from_mantis_node):
  305. to_mantis_node = frame_mantis_nodes[ (
  306. *self.autogen_path_names, # the SCHEMA_AUTOGENERATED string
  307. to_mantis_node.signature[-2]+ self.index_str ) ]
  308. # this connection was forbidden before, right? so this should be a safe assumption.
  309. assert to_mantis_node.node_type == 'DUMMY_SCHEMA', "I expected this to be a group/schema"
  310. # we need to get the identifier of the named socket now.
  311. to_socket_name = to_mantis_node.ui_node.inputs[to_socket_name].identifier
  312. else:
  313. to_mantis_node = frame_mantis_nodes[ to_mantis_node_signature ] # add the index string
  314. from_socket_name = ui_link.from_socket.name
  315. if from_mantis_node.node_type in ['DUMMY_SCHEMA']:
  316. from_socket_name = ui_link.from_socket.identifier
  317. connection = from_mantis_node.outputs[from_socket_name].connect(node=to_mantis_node, socket=to_socket_name)
  318. # We want to delete the links from the tree into the schema node.
  319. # TODO: this is not robust enough and I do not feel sure this is doing the right thing.
  320. if existing_link := self.incoming_connections[ui_link.to_socket.name]:
  321. if existing_link.to_node == self.node:
  322. print ("INFO: Deleting...", existing_link)
  323. if self.node.signature[-1] in existing_link.to_node.signature:
  324. existing_link.die()
  325. # BUG may exist here.
  326. self.incoming_connections[ui_link.to_socket.name] = connection
  327. def handle_link_from_constant_input(self, frame_mantis_nodes, ui_link, to_ui_node):
  328. incoming = self.constant_in[ui_link.from_socket.name]
  329. if incoming is None:
  330. raise GraphError (f"{self.node} is missing a required input: {ui_link.from_socket.name}")
  331. from_node = incoming.from_node
  332. to_name = get_link_in_out(ui_link)[1]
  333. to_node = frame_mantis_nodes[(*self.autogen_path_names, to_name+self.index_str)]
  334. to_socket=ui_link.to_socket.name
  335. from ..base_definitions import MantisNodeGroup, SchemaGroup
  336. if isinstance(to_ui_node, (SchemaGroup, MantisNodeGroup)):
  337. to_socket=ui_link.to_socket.identifier
  338. connection = from_node.outputs[incoming.from_socket].connect(node=to_node, socket=to_socket)
  339. init_connections(from_node)
  340. def handle_link_from_array_input_get(self, frame_mantis_nodes, ui_link ):
  341. from_ui_node = ui_link.from_socket.node
  342. from_node = self.schema_nodes[(*self.node.ui_signature, from_ui_node.bl_idname)]
  343. from collections import deque
  344. unprepared = deque(from_node.hierarchy_dependencies)
  345. self.prepare_nodes(unprepared)
  346. from ..utilities.utilities import cap, wrap
  347. get_index = from_node.evaluate_input("Index", self.index) # get the most recent link
  348. # getting the link at self.index just saves the trouble of killing the old links
  349. # that are left because this node is reused in each iteration of the schema
  350. assert(get_index is not None), f"Cannot get index in {from_node}"
  351. oob = from_node.evaluate_input("OoB Behaviour")
  352. array_length = len(self.array_input_connections[ui_link.from_socket.identifier])-1
  353. if oob == 'WRAP':
  354. get_index = wrap(0, array_length+1, get_index)
  355. if oob == 'HOLD':
  356. get_index = cap(get_index, array_length)
  357. try:
  358. incoming = self.array_input_connections[ui_link.from_socket.identifier][get_index]
  359. except IndexError:
  360. raise RuntimeError(f"The array index {get_index} is out of bounds. Size: "
  361. f"{len(self.array_input_connections[ui_link.from_socket.identifier])}")
  362. to_name = get_link_in_out(ui_link)[1]
  363. to_node = frame_mantis_nodes[(*self.autogen_path_names, to_name+self.index_str)]
  364. from_socket = incoming.from_node.outputs[incoming.from_socket]
  365. connection = from_socket.connect(to_node, ui_link.to_socket.name)
  366. # TODO: kill links to the array get that are no longer needed
  367. # right now I inefficiently waste cpu-time solving extra nodes
  368. # because I took the lazy way out
  369. def handle_link_to_array_input_get(self, frame_mantis_nodes, ui_link):
  370. from_name, to_name = get_link_in_out(ui_link)
  371. from_mantis_node = frame_mantis_nodes[(*self.autogen_path_names, from_name+self.index_str)]
  372. to_mantis_node = self.schema_nodes[(*self.tree_path_names, to_name)]
  373. # this only needs to be done once:
  374. if self.index == 0: # BUG? HACK? TODO find out what is going on here.
  375. # Kill the link between the schema node group and the node connecting to it
  376. old_mantis_node = self.all_nodes[(*self.tree_path_names, from_name)]
  377. # I am not sure about this!
  378. existing_link = old_mantis_node.outputs[ui_link.from_socket.name].links[0]
  379. existing_link.die()
  380. #
  381. connection = from_mantis_node.outputs[ui_link.from_socket.name].connect(node=to_mantis_node, socket=ui_link.to_socket.name)
  382. connection.is_hierarchy = True # we have to mark this because links to Schema are not usually hierarchy (?)
  383. to_mantis_node.hierarchy_dependencies.append(from_mantis_node); from_mantis_node.hierarchy_connections.append(to_mantis_node)
  384. # TODO: review the wisdom of this default.
  385. def handle_link_from_array_input(self, frame_mantis_nodes, ui_link, index):
  386. get_index = index
  387. try:
  388. incoming = self.array_input_connections[ui_link.from_socket.identifier][get_index]
  389. except IndexError:
  390. if len(self.array_input_connections[ui_link.from_socket.identifier]) > 0:
  391. incoming = self.array_input_connections[ui_link.from_socket.identifier][0]
  392. # prOrange(incoming.from_node.node_type)
  393. if incoming.from_node.node_type not in ['DUMMY_SCHEMA']:
  394. raise NotImplementedError(wrapRed("dev: make it so Mantis checks if there are enough Array inputs."))
  395. else: # do nothing
  396. return
  397. else:
  398. raise RuntimeError(wrapRed("make it so Mantis checks if there are enough Array inputs!"))
  399. to_name = get_link_in_out(ui_link)[1]
  400. to_node = frame_mantis_nodes[(*self.autogen_path_names, to_name+self.index_str)]
  401. connection = incoming.from_node.outputs[incoming.from_socket].connect(node=to_node, socket=ui_link.to_socket.name)
  402. init_connections(incoming.from_node)
  403. def handle_link_from_array_input_all(self, frame_mantis_nodes, ui_link):
  404. all_links = self.array_input_connections[ui_link.from_socket.identifier]
  405. to_name = get_link_in_out(ui_link)[1]
  406. to_node = frame_mantis_nodes[(*self.autogen_path_names, to_name+self.index_str)]
  407. # connection = incoming.from_node.outputs[incoming.from_socket].connect(node=to_node, socket=ui_link.to_socket.name)
  408. for l in all_links:
  409. # we need to copy the link with the new from-node info
  410. from ..base_definitions import NodeLink
  411. to_socket_name=ui_link.to_socket.name
  412. if to_node.node_type in ['DUMMY_SCHEMA']:
  413. to_socket_name=ui_link.to_socket.identifier
  414. connection = NodeLink(l.from_node, l.from_socket, to_node, to_socket_name,
  415. l.multi_input_sort_id, l.sub_sort_id)
  416. to_node.flush_links()
  417. def handle_link_to_constant_output(self, frame_mantis_nodes, index, ui_link, to_ui_node):
  418. to_node = self.schema_nodes[(*self.tree_path_names, to_ui_node.bl_idname)]
  419. expose_when = to_node.evaluate_input('Expose when N==')
  420. # this will be None for nested Groups that have ordinary Group Input and Group Output nodes.
  421. # and in that case we just do it no matter what
  422. if expose_when is not None and expose_when > self.solve_length-1:
  423. raise GraphError(f"The Schema has a constant output at index {expose_when}"
  424. f" but it terminates at index {self.solve_length-1}. "
  425. f"The Schema must have a length of at least {expose_when+1}.")
  426. from_name = get_link_in_out(ui_link)[0]
  427. #use it directly if it is a mantis node; this happens when the previous node was a Schema
  428. if isinstance( ui_link.from_socket, NodeSocket): # normal
  429. from_node = frame_mantis_nodes[(*self.autogen_path_names, from_name+self.index_str) ]
  430. else: # it's a mantis socket, set manually while looping though prepped links
  431. from_node = ui_link.from_socket.node
  432. from_socket_name = ui_link.from_socket.name
  433. if from_node.node_type == 'DUMMY_SCHEMA':
  434. if isinstance( ui_link.from_socket, NodeSocket): # normal
  435. from_socket_name = ui_link.from_socket.identifier
  436. else:
  437. from_socket_name = ui_link.from_socket.name
  438. # HACK here to force it to work with ordinary node groups, which don't seem to set this value correctly.
  439. if to_ui_node.bl_idname == "NodeGroupOutput":
  440. expose_when = index # the expose-when value is None because the GroupOutput doesn't have the socket.
  441. # end HACK
  442. if index == expose_when:
  443. for outgoing in self.constant_out[ui_link.to_socket.name]:
  444. to_node = outgoing.to_node
  445. connection = from_node.outputs[from_socket_name].connect(node=to_node, socket=outgoing.to_socket)
  446. def handle_link_from_subschema_to_output(self, frame_mantis_nodes, ui_link, to_ui_node):
  447. # wire the schema node itself to the output so it will push the connections when solving
  448. to_node = self.schema_nodes[(*self.tree_path_names, to_ui_node.bl_idname)]
  449. from_name = get_link_in_out(ui_link)[0]
  450. from_node = frame_mantis_nodes[ (*self.autogen_path_names, from_name+self.index_str) ]
  451. connection = from_node.outputs[ui_link.from_socket.identifier].connect(node=to_node, socket=ui_link.to_socket.name)
  452. def handle_link_from_array_type_to_array_out(self, original_ui_link, dummy_link):
  453. # this is so annoyingly specific lol
  454. from_node = dummy_link.from_mantis_node; from_socket_name=dummy_link.from_socket.name
  455. for outgoing in self.array_output_connections[original_ui_link.to_socket.identifier]:
  456. to_node = outgoing.to_node
  457. connection = from_node.outputs[from_socket_name].connect(node=to_node, socket=outgoing.to_socket)
  458. # WTF is even happening here?? TODO BUG HACK
  459. def handle_link_to_array_output(self, frame_mantis_nodes, index, ui_link, to_ui_node, from_ui_node):# if this duplicated code works, dedupe!
  460. to_node = self.schema_nodes[(*self.tree_path_names, to_ui_node.bl_idname)] # get it by [], we want a KeyError if this fails
  461. from_name = get_link_in_out(ui_link)[0]
  462. #use it directly if it is a mantis node; this happens when the previous node was a Schema
  463. if isinstance( ui_link.from_socket, NodeSocket): # normal
  464. from_node = frame_mantis_nodes[(*self.autogen_path_names, from_name+self.index_str) ]
  465. else: # it's a mantis socket, set manually while looping though prepped links
  466. from_node = ui_link.from_socket.node
  467. from_socket_name = ui_link.from_socket.name
  468. for outgoing in self.array_output_connections[ui_link.to_socket.identifier]:
  469. if not from_node:
  470. from_node = self.schema_nodes[(*self.tree_path_names, from_ui_node.bl_idname)]
  471. to_node = outgoing.to_node
  472. from ..schema_nodes import SchemaIndex
  473. if isinstance(from_node, SchemaIndex):
  474. signature = ("MANTIS_AUTOGENERATED", *self.tree_path_names[1:-1], self.index_str,
  475. ui_link.from_socket.name, ui_link.from_socket.identifier)
  476. from_node = self.all_nodes.get(signature)
  477. if not from_node:
  478. from .readtree import autogen_node
  479. from_node = autogen_node(self.node.base_tree, ui_link.from_socket,
  480. signature, self.node.mContext)
  481. if ui_link.from_socket.name in ['Index']:
  482. from_node.parameters = {ui_link.from_socket.name:index}
  483. else:
  484. from_node.parameters = {ui_link.from_socket.name:self.solve_length}
  485. frame_mantis_nodes[signature]=from_node; self.solved_nodes[signature]=from_node
  486. self.all_nodes[signature]=from_node
  487. elif from_node.node_type == 'DUMMY_SCHEMA':
  488. if isinstance( ui_link.from_socket, NodeSocket): # normal
  489. from_socket_name = ui_link.from_socket.identifier
  490. else:
  491. from_socket_name = ui_link.from_socket.name
  492. if to_node.node_type == 'DUMMY_SCHEMA' and to_node.prepared:
  493. other_stem = ('SCHEMA_AUTOGENERATED', *to_node.signature[1:])
  494. other_schema_np = to_node.ui_node
  495. other_schema_tree = other_schema_np.node_tree
  496. for n in other_schema_tree.nodes:
  497. if n.bl_idname not in ["SchemaArrayInput", "SchemaArrayInputGet", "SchemaArrayInputAll"]:
  498. continue
  499. out = n.outputs[outgoing.to_socket]
  500. for l in out.links:
  501. other_index_str = lambda : '.'+str(to_node.uuid)+'.'+str(index).zfill(4)
  502. other_sig = (*other_stem, l.to_node.name+other_index_str())
  503. out_node = self.all_nodes.get(other_sig)
  504. if not out_node: # if it is a sub-schema, the other node is in the parent
  505. out_node = to_node.solver.all_nodes.get(other_sig)
  506. if not out_node:
  507. raise KeyError(f"Cannot find node: {other_sig}")
  508. connection = from_node.outputs[from_socket_name].connect(node=out_node, socket=l.to_socket.name)
  509. else:
  510. connection = from_node.outputs[from_socket_name].connect(node=to_node, socket=outgoing.to_socket)
  511. def spoof_link_for_subschema_to_output_edge_case(self, ui_link, ):
  512. to_ui_node = ui_link.to_socket.node
  513. # ui_link is the link between the Schema & the output but we have mantis_links
  514. # connected up correctly. between the output and the schema's generated nodes.
  515. # so seek BACK from the output node and grab the from-node that is connected to
  516. # the link. then modify the ui_link to point to that node.
  517. from ..base_definitions import DummyLink
  518. if not isinstance(ui_link, DummyLink): # make it a Dummy so i can modify it
  519. ui_link = DummyLink(ui_link.from_socket, ui_link.to_socket,
  520. multi_input_sort_id=ui_link.multi_input_sort_id)
  521. to_node = self.schema_nodes[(*self.tree_path_names, to_ui_node.bl_idname)]
  522. for inp in to_node.inputs:
  523. if inp.name == ui_link.to_socket.name: # the most recent one is from
  524. l = inp.links[-1]; break # <---- this index of the outer schema
  525. ui_link.from_mantis_node = l.from_node; ui_link.from_socket = l.from_node.outputs[l.from_socket]
  526. return ui_link
  527. def test_is_sub_schema(self, other):
  528. for i in range(len(other.ui_signature)-1): # -1, we don't want to check this node, obviously
  529. if self.node.ui_signature[:i+1]:
  530. return False
  531. return True
  532. def prepare_nodes(self, unprepared):
  533. # At this point, we've already run a pretty exhaustive preperation phase to prep the schema's dependencies
  534. # So we should not need to add any new dependencies unless there is a bug elsewhere.
  535. # and in fact, I could skip this in some cases, and should investigate if profiling reveals a slowdown here.
  536. forbidden=set()
  537. e = None
  538. # forbid some nodes - they aren't necessary to solve the schema & cause problems.
  539. while unprepared:
  540. mantis_node = unprepared.pop()
  541. if mantis_node.node_type == 'DUMMY_SCHEMA' and not self.test_is_sub_schema(mantis_node):
  542. forbidden.add(mantis_node) # do NOT add this as a dependency.
  543. if mantis_node in forbidden: continue # trying to resolve dependencies for.
  544. if sum([dep.prepared for dep in mantis_node.hierarchy_dependencies]) == len(mantis_node.hierarchy_dependencies):
  545. try:
  546. mantis_node.bPrepare()
  547. if mantis_node.node_type == 'DUMMY_SCHEMA':
  548. self.solve_nested_schema(mantis_node)
  549. except Exception as e:
  550. raise execution_error_cleanup(mantis_node, e, show_error = False)
  551. break
  552. if not mantis_node.prepared:
  553. raise RuntimeError( f"{mantis_node} has failed to prepare."
  554. " Please report this as a bug in mantis." )
  555. else: # Keeping this for-loop as a fallback, it should never add dependencies though
  556. can_add_me = True
  557. for dep in mantis_node.hierarchy_dependencies:
  558. if not dep.prepared and dep not in unprepared:
  559. if dep in forbidden:
  560. can_add_me=False
  561. forbidden.add(mantis_node) # forbid the parent, too
  562. continue
  563. unprepared.appendleft(dep)
  564. if can_add_me:
  565. unprepared.appendleft(mantis_node) # just rotate them until they are ready.
  566. def solve_iteration(self):
  567. """ Solve an iteration of the schema.
  568. - 1 Create the Mantis Node instances that represent this iteration of the schema
  569. - 2 Connect the links from the entrypoint or previous iteration.
  570. - 3 Connect the constant and array links, and any link between nodes entirely within the tree
  571. - 4 Prepare the nodes that modify data (in case of e.g. array get index or nested schema length input)
  572. - 5 Connect the final prepared nodes
  573. and return the nodes that were created in this schema iteration (frame).
  574. This function also adds to held_links to pass data between iterations.
  575. """
  576. from ..schema_nodes_ui import (SchemaIndex,
  577. SchemaArrayInput,
  578. SchemaArrayInputGet,
  579. SchemaArrayInputAll,
  580. SchemaArrayOutput,
  581. SchemaConstInput,
  582. SchemaConstOutput,
  583. SchemaOutgoingConnection,
  584. SchemaIncomingConnection,)
  585. from ..utilities.utilities import clear_reroutes, link_mantis_nodes
  586. from ..base_definitions import array_output_types
  587. self.set_schema_per_interation_constants()
  588. frame_mantis_nodes = {}
  589. # Later we have to run bPrepare() on these guys, so we make the deque and fill it now.
  590. from collections import deque
  591. unprepared= deque()
  592. self.gen_solve_iteration_mantis_nodes(frame_mantis_nodes, unprepared)
  593. # This is where we handle node connections BETWEEN frames
  594. while(self.held_links):
  595. ui_link = self.held_links.pop()
  596. to_ui_node = ui_link.to_socket.node; from_ui_node = ui_link.from_socket.node
  597. if isinstance(to_ui_node, SchemaOutgoingConnection):
  598. self.handle_link_to_outgoing_connection_output(frame_mantis_nodes, ui_link)
  599. # Get the rerouted links from the graph. We don't really need to do this every iteration.
  600. # TODO: use profiling to determine if this is slow; if so: copy & reuse the data, refactor the pop()'s out.
  601. ui_links = clear_reroutes(list(self.tree.links))
  602. # Now we handle ui_links in the current frame, including those ui_links between Schema nodes and "real" nodes
  603. links_to_output = []
  604. array_input_get_link = []
  605. for ui_link in ui_links:
  606. to_ui_node = ui_link.to_socket.node; from_ui_node = ui_link.from_socket.node
  607. if isinstance(from_ui_node, SchemaIndex):
  608. if ui_link.from_socket.name == "Index":
  609. self.handle_link_from_index_input(self.index, frame_mantis_nodes, ui_link)
  610. elif ui_link.from_socket.name == "Schema Length":
  611. self.handle_link_from_schema_length_input(frame_mantis_nodes, ui_link)
  612. continue
  613. if isinstance(from_ui_node, SchemaIncomingConnection):
  614. if ui_link.from_socket.name in self.incoming_connections.keys():
  615. self.handle_link_from_incoming_connection_input(frame_mantis_nodes, ui_link)
  616. continue
  617. if isinstance(from_ui_node, (SchemaConstInput, NodeGroupInput)):
  618. if ui_link.from_socket.name in self.constant_in.keys():
  619. self.handle_link_from_constant_input( frame_mantis_nodes, ui_link, to_ui_node)
  620. continue
  621. if isinstance(to_ui_node, SchemaArrayInputGet):
  622. self.handle_link_to_array_input_get( frame_mantis_nodes, ui_link)
  623. continue
  624. if isinstance(from_ui_node, SchemaArrayInput):
  625. self.handle_link_from_array_input(frame_mantis_nodes, ui_link, self.index)
  626. continue
  627. if isinstance(from_ui_node, SchemaArrayInputAll):
  628. self.handle_link_from_array_input_all(frame_mantis_nodes, ui_link)
  629. continue
  630. # HOLD these links to the next iteration:
  631. if isinstance(to_ui_node, SchemaOutgoingConnection):
  632. if isinstance(from_ui_node, (MantisNodeGroup, SchemaGroup)):
  633. self.handle_link_from_subschema_to_output(frame_mantis_nodes, ui_link, to_ui_node)
  634. self.held_links.append(ui_link) # is this wise? Why am I doing this?
  635. continue
  636. # HOLD these links until prep is done a little later
  637. if isinstance(to_ui_node, (SchemaConstOutput, NodeGroupOutput)) or isinstance(to_ui_node, SchemaArrayOutput):
  638. if isinstance(from_ui_node, (MantisNodeGroup, SchemaGroup)):
  639. self.handle_link_from_subschema_to_output(frame_mantis_nodes, ui_link, to_ui_node)
  640. # both links are desirable to create, so don't continue here
  641. if from_ui_node.bl_idname in array_output_types:
  642. self.handle_link_from_subschema_to_output(frame_mantis_nodes, ui_link, to_ui_node)
  643. # this one wires links around - we need to finish connecting it to the output
  644. # before we can prepare it. Otherwise, it will send a link from *itself* instead of
  645. # rewiring one of its *inputs* to the next node.
  646. # We have to prep it, and then deal with the links between it and the array. pain.
  647. links_to_output.append(ui_link)
  648. continue
  649. if isinstance(from_ui_node, SchemaArrayInputGet):
  650. array_input_get_link.append(ui_link)
  651. continue
  652. # for any of the special cases, we hit a 'continue' block. So this connection is not special, and is made here.
  653. connection = link_mantis_nodes(self.autogen_path_names, ui_link,
  654. frame_mantis_nodes, from_suffix=self.index_str,
  655. to_suffix=self.index_str)
  656. for signature, node in frame_mantis_nodes.items():
  657. self.solved_nodes[signature]=node
  658. if node.node_type == "DUMMY_SCHEMA":
  659. # make sure to add the nodes to the group's sockets if the user set them directly
  660. from .readtree import make_connections_to_ng_dummy
  661. make_connections_to_ng_dummy(
  662. self.node.base_tree,
  663. self.autogen_path_names,
  664. {}, # just pass an empty dict, this argument is not needed in this context
  665. self.all_nodes,
  666. node)
  667. from ..utilities.utilities import init_schema_dependencies
  668. init_schema_dependencies(node, self.all_nodes)
  669. else:
  670. init_dependencies(node) # it is hard to overstate how important this single line of code is
  671. # We have to prepare the nodes leading to Schema Length
  672. unprepared=deque()
  673. for node in frame_mantis_nodes.values():
  674. if node.node_type == 'DUMMY_SCHEMA' and (schema_len_in := node.inputs.get("Schema Length")):
  675. for l in schema_len_in.links:
  676. unprepared.append(l.from_node)
  677. self.prepare_nodes(unprepared)
  678. # We have to prepare the nodes leading to Array Input Get
  679. for ui_link in array_input_get_link:
  680. from_name = get_link_in_out(ui_link)[0]
  681. # because this both provides and receives deps, it must be solved first.
  682. from_node = self.schema_nodes.get( (*self.node.ui_signature, ui_link.from_node.bl_idname) )
  683. self.handle_link_from_array_input_get(frame_mantis_nodes, ui_link )
  684. # Finally, we have to prepare nodes leading to outputs.
  685. for i in range(len(links_to_output)):
  686. unprepared=deque()
  687. ui_link = links_to_output[i]
  688. to_ui_node = ui_link.to_socket.node; from_ui_node = ui_link.from_socket.node
  689. # ugly workaround here in a very painful edge case...
  690. if isinstance(from_ui_node, (MantisNodeGroup, SchemaGroup)) or\
  691. from_ui_node.bl_idname in array_output_types:
  692. ui_link=self.spoof_link_for_subschema_to_output_edge_case(ui_link)
  693. links_to_output[i] = ui_link
  694. from_name = get_link_in_out(ui_link)[0]
  695. signature = (*self.autogen_path_names, from_name+self.index_str)
  696. #use it directly if it is a mantis node; this happens when the previous node was a Schema
  697. if hasattr(ui_link, "from_node") and (from_node := self.schema_nodes.get( (*self.node.ui_signature, ui_link.from_node.bl_idname))):
  698. unprepared.append(from_node)
  699. unprepared.extend(from_node.hierarchy_dependencies)
  700. elif from_node := frame_mantis_nodes.get(signature):
  701. unprepared.append(from_node)
  702. unprepared.extend(from_node.hierarchy_dependencies)
  703. else:
  704. raise RuntimeError(" 671 there has been an error parsing the tree. Please report this as a bug.")
  705. self.prepare_nodes(unprepared) # prepare only the dependencies we need for this link
  706. # and handle the output by the specific type
  707. if isinstance(to_ui_node, (SchemaConstOutput, NodeGroupOutput)):
  708. self.handle_link_to_constant_output(frame_mantis_nodes, self.index, ui_link, to_ui_node)
  709. if isinstance(to_ui_node, SchemaArrayOutput):
  710. if from_node.bl_idname in array_output_types:
  711. from ..base_definitions import DummyLink
  712. for l in from_node.rerouted:
  713. new_link = DummyLink(
  714. from_socket=l.from_node.outputs[l.from_socket],
  715. to_socket=l.to_node.inputs[l.to_socket],
  716. from_mantis_node=l.from_node, to_mantis_node=l.to_node,
  717. multi_input_sort_id=l.multi_input_sort_id
  718. )
  719. self.handle_link_from_array_type_to_array_out(ui_link, new_link)
  720. else:
  721. self.handle_link_to_array_output(frame_mantis_nodes, self.index, ui_link, to_ui_node, from_ui_node)
  722. return frame_mantis_nodes
  723. def solve_nested_schema(self, schema_mantis_node):
  724. """ Solves all schema node groups found in this Schema. This is a recursive function, which will
  725. solve all levels of nested schema - since this function is called by solver.solve().
  726. """
  727. solver=None
  728. if schema_mantis_node.prepared == False:
  729. all_nodes = self.all_nodes.copy()
  730. ui_node = schema_mantis_node.ui_node
  731. length = schema_mantis_node.evaluate_input("Schema Length")
  732. tree = ui_node.node_tree
  733. if schema_mantis_node.ui_node.bl_idname == "MantisNodeGroup":
  734. prOrange(f"Expanding Node Group {tree.name} in node {schema_mantis_node}.")
  735. else:
  736. prOrange(f"Expanding schema {tree.name} in node {schema_mantis_node} with length {length}.")
  737. solver = SchemaSolver(schema_mantis_node, all_nodes, ui_node, schema_mantis_node.ui_signature, error_popups=self.error_popups)
  738. solved_nodes = solver.solve()
  739. schema_mantis_node.prepared = True
  740. for k,v in solved_nodes.items():
  741. self.solved_nodes[k]=v
  742. return solver
  743. def finalize(self, mantis_nodes_in_frame):
  744. from ..schema_nodes_ui import (SchemaOutgoingConnection,)
  745. for i in range(len(self.held_links)):
  746. link = self.held_links.pop()
  747. to_np = link.to_socket.node; from_np = link.from_socket.node
  748. if isinstance(to_np, SchemaOutgoingConnection):
  749. if link.to_socket.name in self.outgoing_connections.keys():
  750. if (outgoing_links := self.outgoing_connections[link.to_socket.name]) is None: continue
  751. for outgoing in outgoing_links:
  752. if outgoing:
  753. to_node = outgoing.to_node
  754. from_node =mantis_nodes_in_frame[(*self.autogen_path_names, from_np.name+self.index_str) ]
  755. from_socket_name = link.from_socket.name
  756. if from_node.node_type in ['DUMMY_SCHEMA']:
  757. from_socket_name = link.from_socket.identifier
  758. connection = from_node.outputs[from_socket_name].connect(node=to_node, socket=outgoing.to_socket)
  759. # we need to kill the link between the Schema itself and the next node and update the deps. Otherwise:confusing bugs.
  760. outgoing.die(); init_dependencies(to_node)
  761. # else: # the node just isn't connected out this socket.
  762. # # solve all unsolved nested schemas...
  763. for schema_sig, schema_mantis_node in self.nested_schemas.items():
  764. self.solve_nested_schema(schema_mantis_node)
  765. for n in self.autogenerated_nodes.values():
  766. init_connections(n)
  767. for c in n.connections:
  768. init_dependencies(c)
  769. all_outgoing_links = []
  770. for conn in self.outgoing_connections.values():
  771. for outgoing in conn:
  772. all_outgoing_links.append(outgoing)
  773. for conn in self.constant_out.values():
  774. for outgoing in conn:
  775. all_outgoing_links.append(outgoing)
  776. for conn in self.array_output_connections.values():
  777. for outgoing in conn:
  778. all_outgoing_links.append(outgoing)
  779. for outgoing in all_outgoing_links:
  780. to_node = outgoing.to_node
  781. for l in to_node.inputs[outgoing.to_socket].links:
  782. if self.node == l.from_node:
  783. l.die()
  784. for inp in self.node.inputs.values():
  785. for l in inp.links:
  786. init_connections(l.from_node) # to force it to have hierarchy connections with the new nodes.
  787. def solve(self):
  788. if self.solve_length < 1:
  789. from ..base_definitions import GraphError
  790. for o in self.node.outputs:
  791. if o.is_linked:
  792. raise GraphError(f"ERROR: Schema {self} has a length"
  793. " of 0 but other nodes depend on it.")
  794. print (f"WARN: Schema {self} has a length of 0 or less and will not expand.")
  795. return {} # just don't do anything - it's OK to have a noop schema if it doesn't have dependencies.
  796. for index in range(self.solve_length):
  797. self.index = index
  798. frame_mantis_nodes = self.solve_iteration()
  799. for sig, mantis_node in frame_mantis_nodes.items():
  800. if mantis_node.node_type == 'DUMMY_SCHEMA':
  801. self.nested_schemas[sig] = mantis_node
  802. self.finalize(frame_mantis_nodes)
  803. self.solved = True
  804. self.node.prepared = True
  805. prGreen(f"Schema declared {len(self.solved_nodes)} nodes.\n")
  806. return self.solved_nodes