schema_solve.py 51 KB

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