node_common.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. from .utilities import (prRed, prGreen, prPurple, prWhite,
  2. prOrange,
  3. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  4. wrapOrange,)
  5. from .base_definitions import GraphError, NodeSocket, MantisNode
  6. from collections.abc import Callable
  7. # BE VERY CAREFUL
  8. # the x_containers files import * from this file
  9. # so all the top-level imports are carried over
  10. #TODO: refactor the socket definitions so this becomes unnecessary.
  11. def get_socket_value(node_socket):
  12. value = None
  13. if hasattr(node_socket, "default_value"):
  14. value = node_socket.default_value
  15. if node_socket.bl_idname == 'MatrixSocket':
  16. value = node_socket.TellValue()
  17. return value
  18. # TODO: modify this to work with multi-input nodes
  19. def trace_single_line(mantis_node, input_name, link_index=0):
  20. # DO: refactor this for new link class
  21. """Traces a line to its input."""
  22. nodes = [mantis_node]
  23. # Trace a single line
  24. if (socket := mantis_node.inputs.get(input_name) ):
  25. while (socket.is_linked):
  26. link = socket.links[link_index]; link_index = 0
  27. if (socket := link.from_node.outputs.get(link.from_socket)):
  28. nodes.append(socket.node)
  29. if socket.can_traverse:
  30. socket = socket.traverse_target
  31. else: # this is an output.
  32. break
  33. else:
  34. break
  35. return nodes, socket
  36. # this is same as the other, just flip from/to and in/out
  37. def trace_single_line_up(mantis_node, output_name,):
  38. """I use this to get the xForm from a link node."""
  39. nodes = [mantis_node]
  40. if hasattr(mantis_node, "outputs"):
  41. # Trace a single line
  42. if (socket := mantis_node.outputs.get(output_name) ):
  43. while (socket.is_linked):
  44. # This is bad, but it's efficient for nodes that only expect
  45. # one path along the given line
  46. link = socket.links[0] # TODO: find out if this is wise.
  47. other = link.to_node.inputs.get(link.to_socket)
  48. if (other):
  49. socket = other
  50. if socket.can_traverse:
  51. socket = socket.traverse_target
  52. nodes.append(socket.node)
  53. else: # this is an input.
  54. nodes.append(socket.node)
  55. break
  56. else:
  57. break
  58. return nodes, socket
  59. def trace_line_up_branching(mantis_node : MantisNode, output_name : str,
  60. break_condition : Callable = lambda mantis_node : False):
  61. """ Returns all leaf nodes at the ends of branching lines from an output."""
  62. leaf_nodes = []
  63. if hasattr(mantis_node, "outputs"):
  64. # Trace a single line
  65. if (socket := mantis_node.outputs.get(output_name) ):
  66. check_sockets={socket}
  67. while (check_sockets):
  68. # This is bad, but it's efficient for nodes that only expect
  69. # one path along the given line
  70. socket = check_sockets.pop()
  71. for link in socket.links:
  72. other = link.to_node.inputs.get(link.to_socket)
  73. if (other):
  74. socket = other
  75. if break_condition(socket.node):
  76. leaf_nodes.append(socket.node)
  77. elif socket.is_input and socket.can_traverse:
  78. check_sockets.add(socket.traverse_target)
  79. else: # this is an input.
  80. leaf_nodes.append(socket.node)
  81. return leaf_nodes
  82. # this function exists to walk back to the first ancestor node of XFORM or LINK without
  83. # breaking the traverse behaviour which I still need for various reasons.
  84. def get_parent_xForm_info(mantis_node, socket_name = 'Relationship'):
  85. from .mantis_dataclasses import xForm_info
  86. # we re-implement the trace logic here for performance.
  87. # because this kind of connection has traversal (for Links to find their children), we must
  88. # simply seek back to the first available xForm or Link and get its xForm info.\
  89. parent_node = None
  90. if (socket := mantis_node.inputs.get(socket_name) ):
  91. while (socket.is_linked):
  92. link = socket.links[0]
  93. if (socket := link.from_node.outputs.get(link.from_socket)):
  94. if socket.node.node_type in ['LINK', 'XFORM']:
  95. parent_node = socket.node; break
  96. if socket.can_traverse:
  97. socket = socket.traverse_target
  98. else: # this is an output.
  99. break
  100. else:
  101. break
  102. parent_xForm_info = None
  103. if parent_node:
  104. if parent_node.node_type == 'XFORM':
  105. parent_xForm_info = parent_node.parameters['xForm Out']
  106. else:
  107. if 'Output Relationship' in parent_node.parameters.keys():
  108. parent_xForm_info = parent_node.parameters['Output Relationship']
  109. else: # TODO refactor Inherit Node to have the same output as the other link nodes.
  110. parent_xForm_info = parent_node.parameters['Inheritance']
  111. if parent_xForm_info is None: parent_xForm_info = xForm_info() # default to empty
  112. return parent_xForm_info
  113. def setup_custom_property_inputs_outputs(mantis_node):
  114. from .utilities import get_ui_node
  115. if mantis_node.signature[0] == 'SCHEMA_AUTOGENERATED':
  116. from .base_definitions import custom_props_types
  117. if mantis_node.__class__.__name__ not in custom_props_types:
  118. # prRed(f"Reminder: figure out how to deal with custom property setting for Schema Node {mantis_node}")
  119. raise RuntimeError(wrapRed(f"Custom Properties not allowed for class {mantis_node.__class__.__name__}"))
  120. return
  121. else:
  122. ui_node = get_ui_node(mantis_node.signature, mantis_node.base_tree)
  123. if ui_node:
  124. setup_custom_props_from_np(mantis_node, ui_node)
  125. else:
  126. prRed("Failed to setup custom properties for: mantis_node")
  127. def setup_custom_props_from_np(mantis_node, ui_node):
  128. for inp in ui_node.inputs:
  129. if inp.identifier == "__extend__": continue
  130. if not (inp.name in mantis_node.inputs.keys()) :
  131. socket = NodeSocket(is_input = True, name = inp.name, node = mantis_node,)
  132. mantis_node.inputs[inp.name] = socket
  133. mantis_node.parameters[inp.name] = None
  134. for attr_name in ["min", "max", "soft_min", "soft_max", "description"]:
  135. try:
  136. setattr(socket, attr_name, getattr(inp, attr_name))
  137. except AttributeError:
  138. pass
  139. for out in ui_node.outputs:
  140. if out.identifier == "__extend__": continue
  141. if not (out.name in mantis_node.outputs.keys()) :
  142. mantis_node.outputs[out.name] = NodeSocket(is_input = False, name = out.name, node = mantis_node,)
  143. def prepare_parameters(mantis_node):
  144. # some nodes add new parameters at runtime, e.g. Drivers
  145. # so we need to take that stuff from the mantis_nodes that have
  146. # been executed prior to this node.
  147. for s_name, sock in mantis_node.inputs.items():
  148. if not (sock.is_linked):
  149. continue
  150. if (sock.name in sock.links[0].from_node.parameters.keys()):
  151. mantis_node.parameters[s_name] = sock.links[0].from_node.parameters[sock.name]
  152. # should work, this is ugly.
  153. def check_for_driver(mantis_node, input_name, index = None):
  154. prop = mantis_node.evaluate_input(input_name)
  155. if (index is not None):
  156. prop = prop[index]
  157. return (prop.__class__.__name__ == 'MantisDriver')
  158. # TODO: this should handle sub-properties better
  159. def evaluate_sockets(mantis_node, b_object, props_sockets,):
  160. # this is neccesary because some things use dict properties for dynamic properties and setattr doesn't work
  161. def safe_setattr(ob, att_name, val):
  162. if ob.__class__.__name__ in ["NodesModifier"]:
  163. ob[att_name]=val
  164. elif b_object.__class__.__name__ in ["Key"]:
  165. if not val: val=0
  166. ob.key_blocks[att_name].value=val
  167. elif "]." in att_name:
  168. # it is of the form prop[int].prop2
  169. prop=att_name.split('[')[0]
  170. prop1=att_name.split('.')[1]
  171. index = int(att_name.split('[')[1][0])
  172. setattr(getattr(b_object, prop)[index], prop1, val)
  173. else:
  174. try:
  175. setattr(ob, att_name, val)
  176. except Exception as e:
  177. prRed(ob, att_name, val); raise e
  178. for prop, (sock, default) in props_sockets.items():
  179. # annoyingly, sometimes the socket is an array
  180. index = None
  181. if isinstance(sock, tuple):
  182. index = sock[1]; sock = sock[0]
  183. if (check_for_driver(mantis_node, sock, index)):
  184. sock = (sock, index)
  185. original_prop = prop
  186. # TODO: deduplicate this terrible hack
  187. if ("." in prop) and not b_object.__class__.__name__ in ["Key"]: # this is a property of a property...
  188. sub_props = [b_object]
  189. while ("." in prop):
  190. split_prop = prop.split(".")
  191. prop = split_prop[1]
  192. sub_prop = (split_prop[0])
  193. if ("[" in sub_prop):
  194. sub_prop, index = sub_prop.split("[")
  195. index = int(index[0])
  196. sub_props.append(getattr(sub_props[-1], sub_prop)[index])
  197. else:
  198. sub_props.append(getattr(sub_props[-1], sub_prop))
  199. safe_setattr(sub_props[-1], prop, default)
  200. # this is really stupid
  201. else:
  202. safe_setattr(b_object, prop, default)
  203. if mantis_node.node_type in ['LINK',]:
  204. printname = wrapOrange(b_object.id_data.name)
  205. elif mantis_node.node_type in ['XFORM',]:
  206. printname = wrapOrange(mantis_node.bGetObject().name)
  207. else:
  208. printname = wrapOrange(mantis_node)
  209. print("Adding driver %s to %s in %s" % (wrapPurple(original_prop), wrapWhite(mantis_node.signature[-1]), printname))
  210. if b_object.__class__.__name__ in ["NodesModifier"]:
  211. mantis_node.drivers[sock] = "[\""+original_prop+"\"]" # lol. It is a dict element not a "true" property
  212. elif b_object.__class__.__name__ in ["Key"]:
  213. mantis_node.drivers[sock] = "key_blocks[\""+original_prop+"\"].value"
  214. else:
  215. mantis_node.drivers[sock] = original_prop
  216. else: # here we can do error checking for the socket if needed
  217. if (index is not None):
  218. safe_setattr(b_object, prop, mantis_node.evaluate_input(sock)[index])
  219. else: # 'mute' is better than 'enabled'
  220. # UGLY HACK # because it is available in older
  221. if (prop == 'mute'): # Blenders.
  222. safe_setattr(b_object, prop, not bool(mantis_node.evaluate_input(sock)))
  223. elif (prop == 'hide'): # this will not cast it for me, annoying.
  224. safe_setattr(b_object, prop, bool(mantis_node.evaluate_input(sock)))
  225. else:
  226. try:
  227. # prRed(b_object.name, mantis_node, prop, mantis_node.evaluate_input(sock) )
  228. # print( mantis_node.evaluate_input(sock))
  229. # value_eval = mantis_node.evaluate_input(sock)
  230. # just wanna see if we are dealing with some collection
  231. # check hasattr in case it is one of those ["such-and-such"] props, and ignore those
  232. if hasattr(b_object, prop) and (not isinstance(getattr(b_object, prop), str)) and hasattr(getattr(b_object, prop), "__getitem__"):
  233. # prGreen("Doing the thing")
  234. for val_index, value in enumerate(mantis_node.evaluate_input(sock)):
  235. # assume this will work, both because val should have the right number of elements, and because this should be the right data type.
  236. from .drivers import MantisDriver
  237. if isinstance(value, MantisDriver):
  238. getattr(b_object,prop)[val_index] = default[val_index]
  239. print("Adding driver %s to %s in %s" % (wrapPurple(prop), wrapWhite(mantis_node.signature[-1]), mantis_node))
  240. try:
  241. mantis_node.drivers[sock].append((prop, val_index))
  242. except:
  243. mantis_node.drivers[sock] = [(prop, val_index)]
  244. else:
  245. getattr(b_object,prop)[val_index] = value
  246. else:
  247. # prOrange("Skipping the Thing", getattr(b_object, prop))
  248. safe_setattr(b_object, prop, mantis_node.evaluate_input(sock))
  249. except Exception as e:
  250. prRed(b_object, mantis_node, prop, sock, mantis_node.evaluate_input(sock))
  251. raise e
  252. def finish_driver(mantis_node, b_object, driver_item, prop):
  253. # prWhite(mantis_node, prop)
  254. index = driver_item[1]; driver_sock = driver_item[0]
  255. driver_trace = trace_single_line(mantis_node, driver_sock)
  256. driver_provider, driver_socket = driver_trace[0][-1], driver_trace[1]
  257. if index is not None:
  258. driver = driver_provider.parameters[driver_socket.name][index].copy()
  259. # this is harmless and necessary for the weird ones where the property is a vector too
  260. driver["ind"] = index
  261. else:
  262. driver = driver_provider.parameters[driver_socket.name].copy()
  263. if driver:
  264. if ("." in prop) and mantis_node.__class__.__name__ != "DeformerMorphTargetDeform": # this is a property of a property...
  265. sub_props = [b_object]
  266. while ("." in prop):
  267. split_prop = prop.split(".")
  268. prop = split_prop[1]
  269. sub_prop = (split_prop[0])
  270. if ("[" in sub_prop):
  271. sub_prop, index = sub_prop.split("[")
  272. index = int(index[0])
  273. sub_props.append(getattr(sub_props[-1], sub_prop)[index])
  274. else:
  275. sub_props.append(getattr(sub_props[-1], sub_prop))
  276. driver["owner"] = sub_props[-1]
  277. elif mantis_node.node_type in ['XFORM',] and mantis_node.__class__.__name__ in ['xFormBone']:
  278. # TODO: I really shouldn't have to hardcode this. Look into better solutions.
  279. if prop in ['hide', 'show_wire']: # we need to get the bone, not the pose bone.
  280. bone_col = mantis_node.bGetParentArmature().data.bones
  281. else:
  282. bone_col = mantis_node.bGetParentArmature().pose.bones
  283. driver["owner"] = bone_col[b_object] # we use "unsafe" brackets instead of get() because we want to see any errors that occur
  284. # HACK having special cases here is indicitave of a deeper problem that should be refactored
  285. elif mantis_node.__class__.__name__ in ['xFormCurvePin'] and \
  286. prop in ['offset_factor', 'forward_axis', 'up_axis']:
  287. driver["owner"] = b_object.constraints['Curve Pin']
  288. else:
  289. driver["owner"] = b_object
  290. driver["prop"] = prop
  291. return driver
  292. else:
  293. prOrange("Provider", driver_provider)
  294. prGreen("socket", driver_socket)
  295. print (index)
  296. prPurple(driver_provider.parameters[driver_socket.name])
  297. prRed("Failed to create driver for %s" % prop)
  298. return None
  299. def finish_drivers(mantis_node):
  300. drivers = []
  301. if not hasattr(mantis_node, "drivers"):
  302. return # HACK
  303. for driver_item, prop in mantis_node.drivers.items():
  304. b_objects = [mantis_node.bObject]
  305. if mantis_node.node_type == 'LINK':
  306. b_objects = mantis_node.bObject # this is already a list
  307. for b_object in b_objects:
  308. if isinstance(prop, list):
  309. for sub_item in prop:
  310. drivers.append(finish_driver(mantis_node, b_object, (driver_item, sub_item[1]), sub_item[0]))
  311. else:
  312. drivers.append(finish_driver(mantis_node, b_object, (driver_item, sub_item[1]), sub_item[0]))
  313. else:
  314. drivers.append(finish_driver(mantis_node, b_object, driver_item, prop))
  315. from .drivers import CreateDrivers
  316. CreateDrivers(drivers)