readtree.py 36 KB

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