readtree.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. from .utilities import prRed, prGreen, prPurple, prWhite, prOrange, \
  2. wrapRed, wrapGreen, wrapPurple, wrapWhite, wrapOrange
  3. def grp_node_reroute_common(nc, nc_to, all_nc):
  4. # we need to do this: go to the to-node
  5. # then reroute the link in the to_node all the way to the beginning
  6. # so that the number of links in "real" nodes is unchanged
  7. # then the links in the dummy nodes need to be deleted
  8. for inp_name, inp in nc.inputs.items():
  9. # assume each input socket only has one input for now
  10. if inp.is_connected:
  11. while (inp.links):
  12. in_link = inp.links.pop()
  13. from_nc = in_link.from_node
  14. from_socket = in_link.from_socket
  15. links = []
  16. from_links = from_nc.outputs[from_socket].links.copy()
  17. while(from_links):
  18. from_link = from_links.pop()
  19. if from_link == in_link:
  20. from_link.die()
  21. continue # DELETE the dummy node link
  22. links.append(from_link)
  23. from_nc.outputs[from_socket].links = links
  24. down = nc_to.outputs[inp_name]
  25. for downlink in down.links:
  26. downlink.from_node = from_nc
  27. downlink.from_socket = from_socket
  28. from_nc.outputs[from_socket].links.append(downlink)
  29. if hasattr(downlink.to_node, "reroute_links"):
  30. downlink.to_node.reroute_links(downlink.to_node, all_nc)
  31. in_link.die()
  32. def reroute_links_grp(nc, all_nc):
  33. if nc.inputs:
  34. if (nc_to := all_nc.get( ( *nc.signature, "NodeGroupInput") )):
  35. grp_node_reroute_common(nc, nc_to, all_nc)
  36. else:
  37. raise RuntimeError("internal error: failed to enter a node group ")
  38. def reroute_links_grpout(nc, all_nc):
  39. if (nc_to := all_nc.get( ( *nc.signature[:-1],) )):
  40. grp_node_reroute_common(nc, nc_to, all_nc)
  41. else:
  42. raise RuntimeError("error leaving a node group (maybe you are running the tree from inside a node group?)")
  43. # FIXME I don't think these signatures are unique.
  44. def insert_lazy_parents(nc):
  45. from .link_nodes import LinkInherit
  46. from .base_definitions import NodeLink
  47. inherit_nc = None
  48. if nc.inputs["Relationship"].is_connected:
  49. link = nc.inputs["Relationship"].links[0]
  50. # print(nc)
  51. from_nc = link.from_node
  52. if from_nc.node_type in ["XFORM"] and link.from_socket in ["xForm Out"]:
  53. inherit_nc = LinkInherit(("MANTIS_AUTOGENERATED", *nc.signature[1:], "LAZY_INHERIT"), nc.base_tree)
  54. for from_link in from_nc.outputs["xForm Out"].links:
  55. if from_link.to_node == nc and from_link.to_socket == "Relationship":
  56. break # this is it
  57. from_link.to_node = inherit_nc; from_link.to_socket="Parent"
  58. from_link.to_node.inputs[from_link.to_socket].is_linked=True
  59. links=[]
  60. while (nc.inputs["Relationship"].links):
  61. to_link = nc.inputs["Relationship"].links.pop()
  62. if to_link.from_node == from_nc and to_link.from_socket == "xForm Out":
  63. continue # don't keep this one
  64. links.append(to_link)
  65. to_link.from_node.outputs[from_link.from_socket].is_linked=True
  66. nc.inputs["Relationship"].links=links
  67. link=NodeLink(from_node=inherit_nc, from_socket="Inheritance", to_node=nc, to_socket="Relationship")
  68. inherit_nc.inputs["Parent"].links.append(from_link)
  69. inherit_nc.parameters = {
  70. "Parent":None,
  71. "Inherit Rotation":True,
  72. "Inherit Scale":'FULL',
  73. "Connected":False,
  74. }
  75. # because the from node may have already been done.
  76. init_connections(from_nc)
  77. init_dependencies(from_nc)
  78. init_connections(inherit_nc)
  79. init_dependencies(inherit_nc)
  80. return inherit_nc
  81. # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** #
  82. # DATA FROM NODES #
  83. # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** #
  84. from .base_definitions import replace_types, NodeSocket
  85. def autogen_node(base_tree, ui_socket, signature, mContext):
  86. mantis_node=None
  87. from .internal_containers import AutoGenNode
  88. mantis_node = AutoGenNode(signature, base_tree)
  89. mantis_node.mContext = mContext
  90. mantis_node.outputs.init_sockets([ui_socket.name])
  91. mantis_node.ui_signature = None # does not exist in the UI
  92. return mantis_node
  93. # TODO: investigate whether I can set the properties in the downstream nodes directly.
  94. # I am doing this in Schema Solver and it seems to work quite efficiently.
  95. def make_connections_to_ng_dummy(base_tree, tree_path_names, local_nc, all_nc, nc_to):
  96. from .socket_definitions import no_default_value
  97. for inp in nc_to.prototype.inputs:
  98. if inp.bl_idname in no_default_value:
  99. continue
  100. nc_from = None
  101. to_s = inp.identifier
  102. if not inp.is_linked: # make an autogenerated NC for the inputs of the group node
  103. # This can be run inside schema. Make it unique with uuid() to be safe.
  104. from uuid import uuid4
  105. signature = ("MANTIS_AUTOGENERATED", *tree_path_names, nc_to.ui_signature[-1], inp.name, inp.identifier, str(uuid4()))
  106. nc_from = all_nc.get(signature) # creating this without checking and
  107. # using UUID signature leads to TERRIBLE CONFUSING BUGS.
  108. if nc_from is None:
  109. nc_from = autogen_node(base_tree, inp, signature, nc_to.mContext)
  110. from .node_container_common import get_socket_value
  111. if nc_from: # autogen can fail and we should catch it.
  112. nc_from.parameters = {inp.name:get_socket_value(inp)}
  113. local_nc[signature] = nc_from; all_nc[signature] = nc_from
  114. nc_from.outputs[inp.name].connect(node=nc_to, socket=to_s, sort_id=0)
  115. else:
  116. prRed("No available auto-generated class for input %s in %s" % (inp.name, np.name))
  117. def gen_node_containers(base_tree, current_tree, tree_path_names, all_nc, local_nc, dummy_nodes, group_nodes, schema_nodes ):
  118. from .internal_containers import DummyNode
  119. for ui_node in current_tree.nodes:
  120. # HACK I found that this isn't being set sometimes. I wonder why? It makes the most sense to do this here.
  121. if hasattr(ui_node, 'initialized'): ui_node.initialized=True
  122. # end HACK. TODO: find out why this is not set sometimes. This is only needed for UI socket change updates.
  123. if ui_node.bl_idname in ["NodeFrame", "NodeReroute"]:
  124. continue # not a Mantis Node
  125. if ui_node.bl_idname in ["NodeGroupInput", "NodeGroupOutput"]:
  126. # we only want ONE dummy in/out per tree_path, so use the bl_idname to make a Dummy node
  127. sig = (None, *tree_path_names, ui_node.bl_idname)
  128. ui_sig = (None, *tree_path_names, ui_node.name)
  129. if not local_nc.get(sig):
  130. nc = DummyNode( signature=sig , base_tree=base_tree, prototype=ui_node, ui_signature=ui_sig )
  131. local_nc[sig] = nc; all_nc[sig] = nc; dummy_nodes[sig] = nc
  132. if ui_node.bl_idname in ["NodeGroupOutput"]:
  133. nc.reroute_links = reroute_links_grpout
  134. elif ui_node.bl_idname in ["MantisNodeGroup", "MantisSchemaGroup"]:
  135. nc = DummyNode( signature= (sig := (None, *tree_path_names, ui_node.name) ), base_tree=base_tree, prototype=ui_node )
  136. local_nc[sig] = nc; all_nc[sig] = nc; dummy_nodes[sig] = nc
  137. make_connections_to_ng_dummy(base_tree, tree_path_names, local_nc, all_nc, nc)
  138. if ui_node.bl_idname == "MantisNodeGroup":
  139. group_nodes.append(nc)
  140. nc.reroute_links = reroute_links_grp
  141. else:
  142. group_nodes.append(nc)
  143. schema_nodes[sig] = nc
  144. # if it wasn't the types we ignore or the types we make a Dummy for, use this to catch all non-special cases.
  145. elif (nc_cls := ui_node.mantis_class):
  146. sig = (None, *tree_path_names, ui_node.name)
  147. if ui_node.bl_idname in replace_types:
  148. sig = (None, *tree_path_names, ui_node.bl_idname)
  149. if local_nc.get(sig):
  150. continue # already made
  151. nc = nc_cls( sig , base_tree)
  152. local_nc[sig] = nc; all_nc[sig] = nc
  153. nc.ui_signature = (*nc.ui_signature[:-1], ui_node.name) # just to ensure it points to a real node.
  154. else:
  155. nc = None
  156. prRed(f"Can't make nc for.. {ui_node.bl_idname}")
  157. # this should be done at init
  158. if nc.signature[0] not in ['MANTIS_AUTOGENERATED'] and nc.node_type not in ['SCHEMA', 'DUMMY', 'DUMMY_SCHEMA']:
  159. nc.fill_parameters()
  160. def data_from_tree(base_tree, tree_path, dummy_nodes, all_nc, all_schema):#
  161. # TODO: it should be relatively easy to make this use a while loop instead of recursion.
  162. local_nc, group_nodes = {}, []
  163. tree_path_names = [tree.name for tree in tree_path if hasattr(tree, "name")]
  164. if tree_path[-1]:
  165. current_tree = tree_path[-1].node_tree # this may be None.
  166. else:
  167. current_tree = base_tree
  168. #
  169. if current_tree: # the node-group may not have a tree set - if so, ignore it.
  170. from .utilities import clear_reroutes
  171. links = clear_reroutes(list(current_tree.links))
  172. gen_node_containers(base_tree, current_tree, tree_path_names, all_nc, local_nc, dummy_nodes, group_nodes, all_schema)
  173. from .utilities import link_node_containers
  174. for link in links:
  175. link_node_containers((None, *tree_path_names), link, local_nc)
  176. if current_tree == base_tree:
  177. # in the base tree, we need to auto-gen the default values in a slightly different way to node groups.
  178. insert_default_values_base_tree(base_tree, all_nc)
  179. # Now, descend into the Node Groups and recurse
  180. for nc in group_nodes:
  181. data_from_tree(base_tree, tree_path+[nc.prototype], dummy_nodes, all_nc, all_schema)
  182. return dummy_nodes, all_nc, all_schema
  183. from .utilities import check_and_add_root, init_connections, init_dependencies, init_schema_dependencies
  184. def is_signature_in_other_signature(parent_signature, child_signature):
  185. # If the other signature is shorter, it isn't a child node
  186. if len(parent_signature) > len(child_signature):
  187. return False
  188. return parent_signature[0:] == child_signature[:len(parent_signature)]
  189. def solve_schema_to_tree(nc, all_nc, roots=[], error_popups=False):
  190. from .utilities import get_node_prototype
  191. np = get_node_prototype(nc.signature, nc.base_tree)
  192. from .schema_solve import SchemaSolver
  193. solver = SchemaSolver(nc, all_nc.copy(), np, error_popups=error_popups)
  194. try:
  195. solved_nodes = solver.solve()
  196. except Exception as e:
  197. # # the schema will run the error cleanup code, we just need to raise or not
  198. solved_nodes = {}
  199. nc.base_tree.hash=''
  200. raise execution_error_cleanup(nc, e, show_error=error_popups)
  201. # maybe this should be done in schema solver. TODO invesitigate a more efficient way
  202. del_me = []
  203. for k, v in all_nc.items():
  204. # delete all the schema's prototype and interface nodes. The links have already been deleted by the solver.
  205. if v.signature[0] not in ['MANTIS_AUTOGENERATED'] and is_signature_in_other_signature(nc.signature, k):
  206. del_me.append(k)
  207. for k in del_me:
  208. del all_nc[k]
  209. for k,v in solved_nodes.items():
  210. all_nc[k]=v
  211. init_connections(v)
  212. check_and_add_root(v, roots, include_non_hierarchy=True)
  213. return solved_nodes
  214. # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** #
  215. # PARSE NODE TREE #
  216. # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** # *** #
  217. schema_bl_idnames = [ "SchemaIndex",
  218. "SchemaArrayInput",
  219. "SchemaArrayInputGet",
  220. "SchemaArrayInputAll",
  221. "SchemaArrayOutput",
  222. "SchemaConstInput",
  223. "SchemaConstOutput",
  224. "SchemaOutgoingConnection",
  225. "SchemaIncomingConnection",
  226. ]
  227. from .utilities import get_all_dependencies
  228. def get_schema_length_dependencies(node, all_nodes={}):
  229. """ Get a list of all dependencies for the given node's length or array properties.
  230. This function will also recursively search for dependencies in its sub-trees.
  231. """
  232. deps = []
  233. prepare_links_to = ['Schema Length', 'Array', 'Index']
  234. def extend_dependencies_from_inputs(node):
  235. for inp in node.inputs.values():
  236. for l in inp.links:
  237. if not l.from_node in node.hierarchy_dependencies:
  238. continue
  239. if "MANTIS_AUTOGENERATED" in l.from_node.signature:
  240. deps.extend([l.from_node]) # why we need this lol
  241. if inp.name in prepare_links_to:
  242. deps.append(l.from_node)
  243. deps.extend(get_all_dependencies(l.from_node))
  244. def deps_filter(dep): # remove any nodes inside the schema
  245. if len(dep.signature) > len(node.signature):
  246. for i in range(len(node.signature)):
  247. dep_sig_elem, node_sig_elem = dep.signature[i], node.signature[i]
  248. if dep_sig_elem != node_sig_elem: break # they don't match, it isn't an inner-node
  249. else: # remove this, it didn't break, meaning it shares signature with outer node
  250. return False # this is an inner-node
  251. return True
  252. # this way we can handle Schema and Array Get nodes with one function
  253. extend_dependencies_from_inputs(node)
  254. if node.node_type == 'DUMMY_SCHEMA':
  255. trees = [(node.prototype.node_tree, node.signature)] # this is UI data
  256. while trees:
  257. tree, tree_signature = trees.pop()
  258. for sub_ui_node in tree.nodes:
  259. if sub_ui_node.bl_idname in ['NodeReroute', 'NodeFrame']:
  260. continue
  261. if sub_ui_node.bl_idname in schema_bl_idnames:
  262. sub_node = all_nodes[(*tree_signature, sub_ui_node.bl_idname)]
  263. else:
  264. sub_node = all_nodes[(*tree_signature, sub_ui_node.name)]
  265. if sub_node.node_type == 'DUMMY_SCHEMA':
  266. extend_dependencies_from_inputs(sub_node)
  267. trees.append((sub_node.prototype.node_tree, sub_node.signature))
  268. return list(filter(deps_filter, deps))
  269. def insert_default_values_base_tree(base_tree, all_mantis_nodes):
  270. # we can get this by name because group inputs are gathered to the bl_idname
  271. InputNode = all_mantis_nodes.get((None, 'NodeGroupInput'))
  272. for output in InputNode.outputs:
  273. for interface_item in base_tree.interface.items_tree:
  274. if interface_item.identifier == output.name: break
  275. else:
  276. raise RuntimeError(f"Default value {output.name} does not exist in {base_tree.name} ")
  277. if interface_item.item_type == "PANEL":
  278. raise RuntimeError(f"Cannot get default value for {output.name} in {base_tree.name} ")
  279. default_value = None
  280. match interface_item.bl_socket_idname:
  281. case "BooleanSocket":
  282. default_value = interface_item.default_bool
  283. case "IntSocket":
  284. default_value = interface_item.default_int
  285. case "FloatSocket":
  286. default_value = interface_item.default_float
  287. case "BooleanThreeTupleSocket":
  288. default_value = interface_item.default_bool_vector
  289. case "VectorSocket":
  290. default_value = interface_item.default_float
  291. case "StringSocket":
  292. default_value = interface_item.default_string
  293. case "xFormSocket":
  294. if interface_item.default_xForm == 'ARMATURE':
  295. default_value = 'MANTIS_DEFAULT_ARMATURE'
  296. else:
  297. raise RuntimeError(f"No xForm connected for {output.name} in {base_tree.name}.")
  298. case _:
  299. raise RuntimeError(f"Cannot get default value for {output.name} in {base_tree.name} ")
  300. output_name = output.name
  301. if interface_item.bl_socket_idname not in ['xFormSocket']:
  302. signature = ("MANTIS_AUTOGENERATED", f"Default Value {output.name}",)
  303. autogen_mantis_node = all_mantis_nodes.get(signature)
  304. if autogen_mantis_node is None:
  305. autogen_mantis_node = autogen_node(base_tree, output, signature, InputNode.mContext)
  306. autogen_mantis_node.parameters[output_name]=default_value
  307. elif interface_item.bl_socket_idname == 'xFormSocket' \
  308. and default_value == 'MANTIS_DEFAULT_ARMATURE':
  309. signature = ("MANTIS_AUTOGENERATED", "MANTIS_DEFAULT_ARMATURE",)
  310. autogen_mantis_node = all_mantis_nodes.get(signature)
  311. if autogen_mantis_node is None:
  312. from .xForm_nodes import xFormArmature
  313. autogen_mantis_node = xFormArmature(signature, base_tree)
  314. autogen_mantis_node.parameters['Name']=base_tree.name+'_MANTIS_AUTOGEN'
  315. autogen_mantis_node.mContext = InputNode.mContext
  316. from mathutils import Matrix
  317. autogen_mantis_node.parameters['Matrix'] = Matrix.Identity(4)
  318. output_name = 'xForm Out'
  319. while output.links:
  320. l = output.links.pop()
  321. to_node = l.to_node; to_socket = l.to_socket
  322. l.die()
  323. autogen_mantis_node.outputs[output_name].connect(to_node, to_socket)
  324. init_connections(l.from_node); init_dependencies(l.from_node)
  325. all_mantis_nodes[autogen_mantis_node.signature]=autogen_mantis_node
  326. def parse_tree(base_tree, error_popups=False):
  327. from uuid import uuid4
  328. base_tree.execution_id = uuid4().__str__() # set the unique id of this execution
  329. from .base_definitions import MantisExecutionContext
  330. mContext = MantisExecutionContext(base_tree=base_tree)
  331. import time
  332. data_start_time = time.time()
  333. # annoyingly I have to pass in values for all of the dicts because if I initialize them in the function call
  334. # then they stick around because the function definition inits them once and keeps a reference
  335. # so instead I have to supply them to avoid ugly code or bugs elsewhere
  336. # it's REALLY confusing when you run into this sort of problem. So it warrants four entire lines of comments!
  337. dummy_nodes, all_mantis_nodes, all_schema = data_from_tree(base_tree, tree_path = [None], dummy_nodes = {}, all_nc = {}, all_schema={})
  338. for dummy in dummy_nodes.values(): # reroute the links in the group nodes
  339. if (hasattr(dummy, "reroute_links")):
  340. dummy.reroute_links(dummy, all_mantis_nodes)
  341. prGreen(f"Pulling data from tree took {time.time() - data_start_time} seconds")
  342. start_time = time.time()
  343. solve_only_these = []; solve_only_these.extend(list(all_schema.values()))
  344. roots, array_nodes = [], []
  345. from collections import deque
  346. unsolved_schema = deque()
  347. from .base_definitions import array_output_types, GraphError
  348. for mantis_node in all_mantis_nodes.values():
  349. # add the Mantis Context here, so that it available during parsing.
  350. mantis_node.mContext = mContext
  351. if mantis_node.node_type in ["DUMMY"]: # clean up the groups
  352. if mantis_node.prototype.bl_idname in ("MantisNodeGroup", "NodeGroupOutput"):
  353. continue
  354. # Initialize the dependencies and connections (from/to links) for each node.
  355. # we record & store it because using a getter is much slower (according to profiling)
  356. init_dependencies(mantis_node); init_connections(mantis_node)
  357. check_and_add_root(mantis_node, roots, include_non_hierarchy=True)
  358. # Array nodes need a little special treatment, they're quasi-schemas
  359. if mantis_node.__class__.__name__ in array_output_types:
  360. solve_only_these.append(mantis_node)
  361. array_nodes.append(mantis_node)
  362. from itertools import chain
  363. for schema in chain(all_schema.values(), array_nodes):
  364. # We must remove the schema/array nodes that are inside a schema tree.
  365. for i in range(len(schema.signature)-1): # -1, we don't want to check this node, obviously
  366. if parent := all_schema.get(schema.signature[:i+1]):
  367. # This will be solved along with its parent schema.
  368. solve_only_these.remove(schema)
  369. break
  370. for schema in all_schema.values():
  371. if schema not in solve_only_these: continue
  372. init_schema_dependencies(schema, all_mantis_nodes)
  373. solve_only_these.extend(get_schema_length_dependencies(schema, all_mantis_nodes))
  374. unsolved_schema.append(schema)
  375. for array in array_nodes:
  376. if array not in solve_only_these: continue
  377. solve_only_these.extend(get_schema_length_dependencies(array))
  378. solve_only_these.extend(array_nodes)
  379. schema_solve_done = set()
  380. solve_only_these = set(solve_only_these)
  381. solve_layer = unsolved_schema.copy(); solve_layer.extend(roots)
  382. while(solve_layer):
  383. n = solve_layer.pop()
  384. if n not in solve_only_these:
  385. continue
  386. if n.signature in all_schema.keys():
  387. for dep in n.hierarchy_dependencies:
  388. if dep not in schema_solve_done and (dep in solve_only_these):
  389. if dep.prepared:
  390. continue
  391. solve_layer.appendleft(n)
  392. break
  393. else:
  394. try:
  395. solved_nodes = solve_schema_to_tree(n, all_mantis_nodes, roots, error_popups=error_popups)
  396. except Exception as e:
  397. e = execution_error_cleanup(n, e, show_error=error_popups)
  398. solved_nodes = {}
  399. if error_popups == False:
  400. raise e
  401. return # break out of this function regardless.
  402. unsolved_schema.remove(n)
  403. schema_solve_done.add(n)
  404. for node in solved_nodes.values():
  405. init_dependencies(node); init_connections(node)
  406. solve_layer.appendleft(node)
  407. schema_solve_done.add(node) # CRITICAL to prevent freezes.
  408. for conn in n.hierarchy_connections:
  409. if conn not in schema_solve_done and conn not in solve_layer:
  410. solve_layer.appendleft(conn)
  411. continue
  412. else:
  413. for dep in n.hierarchy_dependencies:
  414. if dep not in schema_solve_done:
  415. break
  416. else:
  417. try:
  418. n.bPrepare()
  419. except Exception as e:
  420. e = execution_error_cleanup(n, e, show_error=error_popups)
  421. if error_popups == False:
  422. raise e
  423. schema_solve_done.add(n)
  424. for conn in n.hierarchy_connections:
  425. if conn not in schema_solve_done and conn not in solve_layer:
  426. solve_layer.appendleft(conn)
  427. continue
  428. if unsolved_schema:
  429. raise RuntimeError("Failed to resolve all schema declarations")
  430. # I had a problem with this looping forever. I think it is resolved... but I don't know lol
  431. all_mantis_nodes = list(all_mantis_nodes.values())
  432. kept_nc = {}
  433. while (all_mantis_nodes):
  434. nc = all_mantis_nodes.pop()
  435. if nc in array_nodes:
  436. continue
  437. if nc.node_type in ["DUMMY", 'SCHEMA', 'DUMMY_SCHEMA']:
  438. continue # screen out the prototype schema nodes, group in/out, and group placeholders
  439. # cleanup autogen nodes
  440. if nc.signature[0] == "MANTIS_AUTOGENERATED" and len(nc.inputs) == 0 and len(nc.outputs) == 1:
  441. from .base_definitions import can_remove_socket_for_autogen
  442. output=list(nc.outputs.values())[0]
  443. value=list(nc.parameters.values())[0] # IDEA modify the dependecy get function to exclude these nodes completely
  444. keep_me = False
  445. for l in output.links:
  446. to_node = l.to_node; to_socket = l.to_socket
  447. # do not remove the socket if it is a custom property.
  448. if not can_remove_socket_for_autogen(to_node, to_socket):
  449. keep_me = True; continue
  450. l.die()
  451. to_node.parameters[to_socket] = value
  452. del to_node.inputs[to_socket]
  453. init_dependencies(to_node) # to remove the autogen node we no longer need.
  454. if not keep_me:
  455. continue
  456. init_connections(nc) # because we have removed many connections.
  457. if (nc.node_type in ['XFORM']) and ("Relationship" in nc.inputs.keys()):
  458. if (new_nc := insert_lazy_parents(nc)):
  459. kept_nc[new_nc.signature]=new_nc
  460. # be sure to add the Mantis context.
  461. new_nc.mContext =mContext
  462. kept_nc[nc.signature]=nc
  463. prWhite(f"Parsing tree took {time.time()-start_time} seconds.")
  464. prWhite("Number of Nodes: %s" % (len(kept_nc)))
  465. return kept_nc
  466. from .utilities import switch_mode
  467. def execution_error_cleanup(node, exception, switch_objects = [], show_error=False ):
  468. from bpy import context
  469. ui_sig = None
  470. if show_error: # show a popup and select the relevant nodes
  471. if node:
  472. if node.mContext:
  473. if node.mContext.execution_failed==True:
  474. # already have an error, pass it to avoid printing
  475. return # a second error (it's confusing to users.)
  476. node.mContext.execution_failed=True
  477. ui_sig = node.ui_signature
  478. # TODO: see about zooming-to-node.
  479. base_tree = node.base_tree
  480. tree = base_tree
  481. try:
  482. pass
  483. space = context.space_data
  484. for name in ui_sig[1:]:
  485. for n in tree.nodes: n.select = False
  486. n = tree.nodes[name]
  487. n.select = True
  488. tree.nodes.active = n
  489. if hasattr(n, "node_tree"):
  490. tree = n.node_tree
  491. except AttributeError: # not being run in node graph
  492. pass
  493. finally:
  494. def error_popup_draw(self, context):
  495. self.layout.label(text=f"Error: {exception}")
  496. self.layout.label(text=f"see node: {ui_sig[1:]}.")
  497. context.window_manager.popup_menu(error_popup_draw, title="Error", icon='ERROR')
  498. switch_mode(mode='OBJECT', objects=switch_objects)
  499. for ob in switch_objects:
  500. ob.data.pose_position = 'POSE'
  501. prRed(f"Error: {exception} in node {ui_sig}")
  502. return exception
  503. def sort_execution(nodes, xForm_pass):
  504. execution_failed=False
  505. sorted_nodes = []
  506. from .node_container_common import GraphError
  507. # check for cycles here by keeping track of the number of times a node has been visited.
  508. visited={}
  509. check_max_len=len(nodes)**2 # seems too high but safe. In a well-ordered graph, I guess this number should be less than the number of nodes.
  510. max_iterations = len(nodes)**2
  511. i = 0
  512. while(xForm_pass):
  513. if execution_failed: break
  514. if i >= max_iterations:
  515. execution_failed = True
  516. raise GraphError("There is probably a cycle somewhere in the graph. "
  517. "Or a connection missing in a Group/Schema Input")
  518. i+=1
  519. n = xForm_pass.pop()
  520. if visited.get(n.signature) is not None:
  521. visited[n.signature]+=1
  522. else:
  523. visited[n.signature]=0
  524. if visited[n.signature] > check_max_len:
  525. execution_failed = True
  526. raise GraphError("There is a probably a cycle in the graph somewhere. "
  527. "Or a connection missing in a Group/Schema Input")
  528. # we're trying to solve the halting problem at this point.. don't do that.
  529. # TODO find a better way! there are algo's for this but they will require using a different solving algo, too
  530. if n.execution_prepared:
  531. continue
  532. if n.node_type not in ['XFORM', 'UTILITY']:
  533. for dep in n.hierarchy_dependencies:
  534. if not dep.execution_prepared:
  535. xForm_pass.appendleft(n) # hold it
  536. break
  537. else:
  538. n.execution_prepared=True
  539. sorted_nodes.append(n)
  540. for conn in n.hierarchy_connections:
  541. if not conn.execution_prepared:
  542. xForm_pass.appendleft(conn)
  543. else:
  544. for dep in n.hierarchy_dependencies:
  545. if not dep.execution_prepared:
  546. break
  547. else:
  548. n.execution_prepared=True
  549. sorted_nodes.append(n)
  550. for conn in n.hierarchy_connections:
  551. if not conn.execution_prepared:
  552. xForm_pass.appendleft(conn)
  553. return sorted_nodes, execution_failed
  554. def execute_tree(nodes, base_tree, context, error_popups = False):
  555. assert nodes is not None, "Failed to parse tree."
  556. assert len(nodes) > 0, "No parsed nodes for execution."\
  557. " Mantis probably failed to parse the tree."
  558. import bpy
  559. from time import time
  560. from .node_container_common import GraphError
  561. original_active = context.view_layer.objects.active
  562. start_execution_time = time()
  563. mContext = None
  564. from collections import deque
  565. xForm_pass = deque()
  566. for nc in nodes.values():
  567. if not mContext: # just grab one of these. this is a silly way to do this.
  568. mContext = nc.mContext
  569. mContext.b_objects = {} # clear the objects and recreate them
  570. nc.reset_execution()
  571. check_and_add_root(nc, xForm_pass)
  572. mContext.execution_failed = False
  573. switch_me = [] # switch the mode on these objects
  574. active = None # only need it for switching modes
  575. select_me = []
  576. try:
  577. sorted_nodes, execution_failed = sort_execution(nodes, xForm_pass)
  578. for n in sorted_nodes:
  579. try:
  580. if not n.prepared:
  581. n.bPrepare(context)
  582. if not n.executed:
  583. n.bTransformPass(context)
  584. if (n.__class__.__name__ == "xFormArmature" ):
  585. ob = n.bGetObject()
  586. switch_me.append(ob)
  587. active = ob
  588. if not (n.__class__.__name__ == "xFormBone" ) and hasattr(n, "bGetObject"):
  589. ob = n.bGetObject()
  590. if isinstance(ob, bpy.types.Object):
  591. select_me.append(ob)
  592. except Exception as e:
  593. e = execution_error_cleanup(n, e, show_error=error_popups)
  594. if error_popups == False:
  595. raise e
  596. execution_failed = True; break
  597. switch_mode(mode='POSE', objects=switch_me)
  598. for n in sorted_nodes:
  599. try:
  600. if not n.prepared:
  601. n.bPrepare(context)
  602. if not n.executed:
  603. n.bRelationshipPass(context)
  604. except Exception as e:
  605. e = execution_error_cleanup(n, e, show_error=error_popups)
  606. if error_popups == False:
  607. raise e
  608. execution_failed = True; break
  609. switch_mode(mode='OBJECT', objects=switch_me)
  610. # switch to pose mode here so that the nodes can use the final pose data
  611. # this will require them to update the depsgraph.
  612. for ob in switch_me:
  613. ob.data.pose_position = 'POSE'
  614. for n in sorted_nodes:
  615. try:
  616. n.bFinalize(context)
  617. except Exception as e:
  618. e = execution_error_cleanup(n, e, show_error=error_popups)
  619. if error_popups == False:
  620. raise e
  621. execution_failed = True; break
  622. # REST pose for deformer bind, so everything is in the rest position
  623. for ob in switch_me:
  624. ob.data.pose_position = 'REST'
  625. # finally, apply modifiers and bind stuff
  626. for n in sorted_nodes:
  627. try:
  628. n.bModifierApply(context)
  629. except Exception as e:
  630. e = execution_error_cleanup(n, e, show_error=error_popups)
  631. if error_popups == False:
  632. raise e
  633. execution_failed = True; break
  634. for ob in switch_me:
  635. ob.data.pose_position = 'POSE'
  636. tot_time = (time() - start_execution_time)
  637. if not execution_failed:
  638. prGreen(f"Executed tree of {len(sorted_nodes)} nodes in {tot_time} seconds")
  639. if (original_active):
  640. context.view_layer.objects.active = original_active
  641. original_active.select_set(True)
  642. except Exception as e:
  643. e = execution_error_cleanup(None, e, switch_me, show_error=error_popups)
  644. if error_popups == False:
  645. raise e
  646. prRed(f"Failed to execute tree.")
  647. finally:
  648. context.view_layer.objects.active = active
  649. # clear the selection first.
  650. from itertools import chain
  651. for ob in context.selected_objects:
  652. try:
  653. ob.select_set(False)
  654. except RuntimeError: # it isn't in the view layer
  655. pass
  656. for ob in chain(select_me, mContext.b_objects.values()):
  657. try:
  658. ob.select_set(True)
  659. except RuntimeError: # it isn't in the view layer
  660. pass