base_definitions.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #Mantis Nodes Base
  2. import bpy
  3. from bpy.props import (BoolProperty, StringProperty, EnumProperty, CollectionProperty, \
  4. IntProperty, IntVectorProperty, PointerProperty, BoolVectorProperty)
  5. from . import ops_nodegroup
  6. from bpy.types import NodeTree, Node, PropertyGroup, Operator, UIList, Panel
  7. from .utilities import (prRed, prGreen, prPurple, prWhite,
  8. prOrange,
  9. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  10. wrapOrange,)
  11. from .utilities import get_socket_maps, relink_socket_map, do_relink
  12. def TellClasses():
  13. #Why use a function to do this? Because I don't need every class to register.
  14. return [ MantisTree,
  15. SchemaTree,
  16. MantisNodeGroup,
  17. SchemaGroup,
  18. ]
  19. def error_popup_draw(self, context):
  20. self.layout.label(text="Error executing tree. See Console.")
  21. mantis_root = ".".join(__name__.split('.')[:-1]) # absolute HACK
  22. # https://docs.blender.org/api/master/bpy.types.NodeTree.html#bpy.types.NodeTree.valid_socket_type
  23. # thank you, Sverchok
  24. def valid_interface_types(cls : NodeTree, socket_idname : str):
  25. from .socket_definitions import tell_valid_bl_idnames, TellClasses
  26. #TODO: do the versioning code to handle this so it can be in all versions
  27. if bpy.app.version <= (4,4,0): # should work in 4.4.1
  28. return socket_idname in [cls.bl_idname for cls in TellClasses()]
  29. else: # once versioning is finished this will be unnecesary.
  30. return socket_idname in tell_valid_bl_idnames()
  31. class MantisTree(NodeTree):
  32. '''A custom node tree type that will show up in the editor type list'''
  33. bl_idname = 'MantisTree'
  34. bl_label = "Rigging Nodes"
  35. bl_icon = 'OUTLINER_OB_ARMATURE'
  36. tree_valid:BoolProperty(default=False)
  37. do_live_update:BoolProperty(default=True) # use this to disable updates for e.g. scripts
  38. num_links:IntProperty(default=-1)
  39. filepath:StringProperty(default="", subtype='FILE_PATH')
  40. is_executing:BoolProperty(default=False)
  41. is_exporting:BoolProperty(default=False)
  42. execution_id:StringProperty(default='')
  43. mantis_version:IntVectorProperty(default=[0,9,2])
  44. # this prevents the node group from executing on the next depsgraph update
  45. # because I don't always have control over when the dg update happens.
  46. prevent_next_exec:BoolProperty(default=False)
  47. parsed_tree={}
  48. if (bpy.app.version < (4, 4, 0)): # in 4.4 this leads to a crash
  49. @classmethod
  50. def valid_socket_type(cls : NodeTree, socket_idname: str):
  51. return valid_interface_types(cls, socket_idname)
  52. def update_tree(self, context = None):
  53. if self.is_exporting:
  54. return
  55. # return
  56. self.is_executing = True
  57. from . import readtree
  58. prGreen("Validating Tree: %s" % self.name)
  59. try:
  60. self.parsed_tree = readtree.parse_tree(self)
  61. if context:
  62. self.display_update(context)
  63. self.is_executing = False
  64. self.tree_valid = True
  65. except GraphError as e:
  66. prRed("Failed to update node tree due to error.")
  67. self.tree_valid = False
  68. self.is_executing = False
  69. raise e
  70. finally:
  71. self.is_executing = False
  72. def display_update(self, context):
  73. if self.is_exporting:
  74. return
  75. self.is_executing = True
  76. current_tree = bpy.context.space_data.path[-1].node_tree
  77. for node in current_tree.nodes:
  78. if hasattr(node, "display_update"):
  79. try:
  80. node.display_update(self.parsed_tree, context)
  81. except Exception as e:
  82. print("Node \"%s\" failed to update display with error: %s" %(wrapGreen(node.name), wrapRed(e)))
  83. self.is_executing = False
  84. # TODO: deal with invalid links properly.
  85. # - Non-hierarchy links should be ignored in the circle-check and so the links should be marked valid in such a circle
  86. # - hierarchy-links should be marked invalid and prevent the tree from executing.
  87. def execute_tree(self,context, error_popups = False):
  88. self.prevent_next_exec = False
  89. if self.is_exporting:
  90. return
  91. # return
  92. prGreen("Executing Tree: %s" % self.name)
  93. self.is_executing = True
  94. from . import readtree
  95. try:
  96. readtree.execute_tree(self.parsed_tree, self, context, error_popups)
  97. except RecursionError as e:
  98. prRed("Recursion error while parsing tree.")
  99. finally:
  100. self.is_executing = False
  101. class SchemaTree(NodeTree):
  102. '''A node tree representing a schema to generate a Mantis tree'''
  103. bl_idname = 'SchemaTree'
  104. bl_label = "Rigging Nodes Schema"
  105. bl_icon = 'RIGID_BODY_CONSTRAINT'
  106. # these are only needed for consistent interface, but should not be used
  107. do_live_update:BoolProperty(default=True) # default to true so that updates work
  108. is_executing:BoolProperty(default=False)
  109. is_exporting:BoolProperty(default=False)
  110. mantis_version:IntVectorProperty(default=[0,9,2])
  111. if (bpy.app.version < (4, 4, 0)): # in 4.4 this leads to a crash
  112. @classmethod
  113. def valid_socket_type(cls : NodeTree, socket_idname: str):
  114. return valid_interface_types(cls, socket_idname)
  115. #TODO: do a better job explaining how MantisNode and MantisUINode relate.
  116. class MantisUINode:
  117. """
  118. This class contains the common user-interface features of Mantis nodes.
  119. MantisUINode objects will spawn one or more MantisNode objects when the graph is evaluated.
  120. The MantisNode objects will pull the data from the UI node and use it to generate the graph.
  121. """
  122. mantis_node_library=''
  123. mantis_node_class_name=''
  124. mantis_class=None
  125. @classmethod
  126. def poll(cls, ntree):
  127. return (ntree.bl_idname in ['MantisTree', 'SchemaTree'])
  128. @classmethod
  129. def set_mantis_class(self):
  130. from importlib import import_module
  131. # do not catch errors, they should cause a failure.
  132. try:
  133. module = import_module(self.mantis_node_library, package=mantis_root)
  134. self.mantis_class=getattr(module, self.mantis_node_class_name)
  135. except Exception as e:
  136. print(self)
  137. raise e
  138. def insert_link(self, link):
  139. if (bpy.app.version < (4, 4, 0)):
  140. return # this causes a crasah due to a bug.
  141. context = bpy.context
  142. if context.space_data:
  143. node_tree = context.space_data.path[0].node_tree
  144. if node_tree.do_live_update:
  145. node_tree.update_tree(context)
  146. if (link.to_socket.is_linked == False):
  147. node_tree.num_links+=1
  148. elif (link.to_socket.is_multi_input):
  149. node_tree.num_links+=1
  150. class SchemaUINode(MantisUINode):
  151. mantis_node_library='.schema_containers'
  152. @classmethod
  153. def poll(cls, ntree):
  154. return (ntree.bl_idname in ['SchemaTree'])
  155. class LinkNode(MantisUINode):
  156. mantis_node_library='.link_containers'
  157. @classmethod
  158. def poll(cls, ntree):
  159. return (ntree.bl_idname in ['MantisTree', 'SchemaTree'])
  160. class xFormNode(MantisUINode):
  161. mantis_node_library='.xForm_containers'
  162. @classmethod
  163. def poll(cls, ntree):
  164. return (ntree.bl_idname in ['MantisTree', 'SchemaTree'])
  165. class DeformerNode(MantisUINode):
  166. mantis_node_library='.deformer_containers'
  167. @classmethod
  168. def poll(cls, ntree):
  169. return (ntree.bl_idname in ['MantisTree', 'SchemaTree'])
  170. def poll_node_tree(self, object):
  171. if isinstance(object, MantisTree):
  172. return True
  173. return False
  174. # TODO: try to check identifiers instead of name.
  175. def node_group_update(node, force = False):
  176. if not force:
  177. if (node.id_data.do_live_update == False) or (node.id_data.is_executing == True):
  178. return
  179. # note: if (node.id_data.is_exporting == True) I need to be able to update so I can make links.
  180. toggle_update = node.id_data.do_live_update
  181. node.id_data.do_live_update = False
  182. identifiers_in={socket.identifier:socket for socket in node.inputs}
  183. identifiers_out={socket.identifier:socket for socket in node.outputs}
  184. indices_in,indices_out={},{} # check by INDEX to see if the socket's name/type match.
  185. for collection, map in [(node.inputs, indices_in), (node.outputs, indices_out)]:
  186. for i, socket in enumerate(collection):
  187. map[socket.identifier]=i
  188. if node.node_tree is None:
  189. node.inputs.clear(); node.outputs.clear()
  190. node.id_data.do_live_update = toggle_update
  191. return
  192. found_in, found_out = [], []
  193. update_input, update_output = False, False
  194. for item in node.node_tree.interface.items_tree:
  195. if item.item_type != "SOCKET": continue
  196. if item.in_out == 'OUTPUT':
  197. if s:= identifiers_out.get(item.identifier): # if the requested output doesn't exist, update
  198. found_out.append(item.identifier)
  199. if (indices_out[s.identifier]!=item.index): update_output=True; continue
  200. if update_output: continue
  201. if s.bl_idname != item.socket_type: update_output = True; continue
  202. else: update_output = True; continue
  203. else:
  204. if s:= identifiers_in.get(item.identifier): # if the requested input doesn't exist, update
  205. found_in.append(item.identifier)
  206. if (indices_in[s.identifier]!=item.index): update_input=True; continue
  207. if update_input: continue # done here
  208. if s.bl_idname != item.socket_type: update_input = True; continue
  209. else: update_input = True; continue
  210. # Schema has an extra input for Length and for Extend.
  211. if node.bl_idname == 'MantisSchemaGroup':
  212. found_in.extend(['Schema Length', ''])
  213. # if we have too many elements, just get rid of the ones we don't need
  214. if len(node.inputs) > len(found_in):#
  215. for inp in node.inputs:
  216. if inp.identifier in found_in: continue
  217. node.inputs.remove(inp)
  218. if len(node.outputs) > len(found_out):
  219. for out in node.outputs:
  220. if out.identifier in found_out: continue
  221. node.outputs.remove(out)
  222. #
  223. if len(node.inputs) > 0 and (inp := node.inputs[-1]).bl_idname == 'WildcardSocket' and inp.is_linked:
  224. update_input = True
  225. if len(node.outputs) > 0 and (out := node.outputs[-1]).bl_idname == 'WildcardSocket' and out.is_linked:
  226. update_output = True
  227. #
  228. if not (update_input or update_output):
  229. node.id_data.do_live_update = toggle_update
  230. return
  231. if update_input or update_output:
  232. socket_map_in, socket_map_out = get_socket_maps(node)
  233. if update_input :
  234. if node.bl_idname == 'MantisSchemaGroup':
  235. schema_length=0
  236. if sl := node.inputs.get("Schema Length"):
  237. schema_length = sl.default_value
  238. # sometimes this isn't available yet # TODO not happy about this solution
  239. node.inputs.clear()
  240. if node.bl_idname == 'MantisSchemaGroup':
  241. node.inputs.new("IntSocket", "Schema Length", identifier='Schema Length')
  242. node.inputs['Schema Length'].default_value = schema_length
  243. if update_output: node.outputs.clear()
  244. for item in node.node_tree.interface.items_tree:
  245. if item.item_type != "SOCKET": continue
  246. if (item.in_out == 'INPUT' and update_input):
  247. relink_socket_map(node, node.inputs, socket_map_in, item)
  248. if (item.in_out == 'OUTPUT' and update_output):
  249. relink_socket_map(node, node.outputs, socket_map_out, item)
  250. # at this point there is no wildcard socket
  251. if '__extend__' in socket_map_in.keys():
  252. do_relink(node, None, socket_map_in, in_out='INPUT', parent_name='Constant' )
  253. node.id_data.do_live_update = toggle_update
  254. def node_tree_prop_update(self, context):
  255. if self.is_updating: # update() can be called from update() and that leads to an infinite loop.
  256. return # so we check if an update is currently running.
  257. self.is_updating = True
  258. node_group_update(self)
  259. self.is_updating = False
  260. if self.bl_idname in ['MantisSchemaGroup'] and self.node_tree is not None:
  261. if len(self.inputs) == 0:
  262. self.inputs.new("IntSocket", "Schema Length", identifier='Schema Length')
  263. if self.inputs[-1].bl_idname != "WildcardSocket":
  264. self.inputs.new("WildcardSocket", "", identifier="__extend__")
  265. from bpy.types import NodeCustomGroup
  266. class MantisNodeGroup(Node, MantisUINode):
  267. bl_idname = "MantisNodeGroup"
  268. bl_label = "Node Group"
  269. node_tree:PointerProperty(type=NodeTree, poll=poll_node_tree, update=node_tree_prop_update,)
  270. is_updating:BoolProperty(default=False)
  271. def update(self):
  272. live_update = self.id_data.do_live_update
  273. if self.is_updating: # update() can be called from update() and that leads to an infinite loop.
  274. return # so we check if an update is currently running.
  275. try:
  276. self.is_updating = True
  277. node_group_update(self)
  278. self.is_updating = False
  279. finally: # we need to reset this regardless of whether or not the operation succeeds!
  280. self.is_updating = False
  281. self.id_data.do_live_update = live_update # ensure this remains the same
  282. def draw_buttons(self, context, layout):
  283. row = layout.row(align=True)
  284. row.prop(self, "node_tree", text="")
  285. row.operator("mantis.edit_group", text="", icon='NODETREE', emboss=True)
  286. class GraphError(Exception):
  287. pass
  288. def get_signature_from_edited_tree(node, context):
  289. sig_path=[None,]
  290. for item in context.space_data.path[:-1]:
  291. sig_path.append(item.node_tree.nodes.active.name)
  292. return tuple(sig_path+[node.name])
  293. def poll_node_tree_schema(self, object):
  294. if isinstance(object, SchemaTree):
  295. return True
  296. return False
  297. # TODO tiny UI problem - inserting new links into the tree will not place them in the right place.
  298. class SchemaGroup(Node, MantisUINode):
  299. bl_idname = "MantisSchemaGroup"
  300. bl_label = "Node Schema"
  301. node_tree:PointerProperty(type=NodeTree, poll=poll_node_tree_schema, update=node_tree_prop_update,)
  302. is_updating:BoolProperty(default=False)
  303. def draw_buttons(self, context, layout):
  304. row = layout.row(align=True)
  305. row.prop(self, "node_tree", text="")
  306. row.operator("mantis.edit_group", text="", icon='NODETREE', emboss=True)
  307. def update(self):
  308. live_update = self.id_data.do_live_update
  309. if self.is_updating: # update() can be called from update() and that leads to an infinite loop.
  310. return # so we check if an update is currently running.
  311. self.is_updating = True
  312. try:
  313. node_group_update(self)
  314. # reset things if necessary:
  315. if self.node_tree:
  316. if len(self.inputs) == 0:
  317. self.inputs.new("IntSocket", "Schema Length", identifier='Schema Length')
  318. if self.inputs[-1].bl_idname != "WildcardSocket":
  319. self.inputs.new("WildcardSocket", "", identifier="__extend__")
  320. finally: # we need to reset this regardless of whether or not the operation succeeds!
  321. self.is_updating = False
  322. self.id_data.do_live_update = live_update # ensure this remains the same
  323. NODES_REMOVED=["xFormRootNode"]
  324. # Node bl_idname, # Socket Name
  325. SOCKETS_REMOVED=[("UtilityDriverVariable", "Transform Channel"),
  326. ("xFormRootNode","World Out"),
  327. ("UtilitySwitch","xForm"),
  328. ("LinkDrivenParameter", "Enable")]
  329. # Node Class #Prior bl_idname # prior name # new bl_idname # new name, # Multi
  330. SOCKETS_RENAMED=[ ("LinkDrivenParameter", "DriverSocket", "Driver", "FloatSocket", "Value", False)]
  331. # NODE CLASS NAME IN_OUT SOCKET TYPE SOCKET NAME INDEX MULTI DEFAULT
  332. SOCKETS_ADDED=[("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Shape Key", 1, False, False),
  333. ("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Offset", 2, False, True),
  334. ("UtilityFCurve", 'INPUT', "eFCrvExtrapolationMode", "Extrapolation Mode", 0, False, 'CONSTANT')]
  335. # replace names with bl_idnames for reading the tree and solving schemas.
  336. replace_types = ["NodeGroupInput", "NodeGroupOutput", "SchemaIncomingConnection",
  337. "SchemaArrayInput", "SchemaConstInput", "SchemaConstOutput", "SchemaIndex",
  338. "SchemaOutgoingConnection", "SchemaConstantOutput", "SchemaArrayOutput",
  339. "SchemaArrayInputGet",]
  340. # anything that gets properties added in the graph... this is a clumsy approach but I need to watch for this
  341. # in schema generation and this is the easiest way to do it for now.
  342. custom_props_types = ["LinkArmature", "UtilityKeyframe", "UtilityFCurve", "UtilityDriver", "xFormBone"]
  343. # filters for determining if a link is a hierarchy link or a non-hierarchy (cyclic) link.
  344. from_name_filter = ["Driver",]
  345. to_name_filter = [
  346. "Custom Object xForm Override",
  347. "Custom Object",
  348. "Deform Bones",
  349. ]
  350. class MantisNode:
  351. """
  352. This class contains the basic interface for a Mantis Node.
  353. A MantisNode is used internally by Mantis to represent the final evaluated node graph.
  354. It gets generated with data from a MantisUINode when the graph is read.
  355. """
  356. def __init__(self, signature, base_tree):
  357. self.base_tree=base_tree
  358. self.signature = signature
  359. self.inputs = MantisNodeSocketCollection(node=self, is_input=True)
  360. self.outputs = MantisNodeSocketCollection(node=self, is_input=False)
  361. self.parameters = {}
  362. self.drivers = {}
  363. self.node_type='UNINITIALIZED'
  364. self.hierarchy_connections, self.connections = [], []
  365. self.hierarchy_dependencies, self.dependencies = [], []
  366. self.prepared = False
  367. self.executed = False
  368. def init_parameters(self, additional_parameters = {}):
  369. for socket in self.inputs:
  370. self.parameters[socket.name] = None
  371. for socket in self.outputs:
  372. self.parameters[socket.name] = None
  373. for key, value in additional_parameters.items():
  374. self.parameters[key]=value
  375. def set_traverse(self, traversal_pairs = [(str, str)]):
  376. for (a, b) in traversal_pairs:
  377. self.inputs[a].set_traverse_target(self.outputs[b])
  378. self.outputs[b].set_traverse_target(self.inputs[a])
  379. def flush_links(self):
  380. for inp in self.inputs.values():
  381. inp.flush_links()
  382. for out in self.outputs.values():
  383. out.flush_links()
  384. def evaluate_input(self, input_name, index=0):
  385. from .node_container_common import trace_single_line
  386. if not (self.inputs.get(input_name)): # get the named parameter if there is no input
  387. return self.parameters.get(input_name) # this will return None if the parameter does not exist.
  388. # this trace() should give a key error if there is a problem
  389. # it is NOT handled here because it should NOT happen - so I want the error message.
  390. trace = trace_single_line(self, input_name, index)
  391. prop = trace[0][-1].parameters[trace[1].name] #trace[0] = the list of traced nodes; read its parameters
  392. return prop
  393. def fill_parameters(self, ui_node=None):
  394. from .utilities import get_node_prototype
  395. from .node_container_common import get_socket_value
  396. if not ui_node:
  397. if ( (self.signature[0] in ["MANTIS_AUTOGENERATED", "SCHEMA_AUTOGENERATED" ]) or
  398. (self.signature[-1] in ["NodeGroupOutput", "NodeGroupInput"]) ): # I think this is harmless
  399. return None
  400. else:
  401. ui_node = get_node_prototype(self.signature, self.base_tree)
  402. if not ui_node:
  403. raise RuntimeError(wrapRed("No node prototype found for... %s" % ( [self.base_tree] + list(self.signature[1:]) ) ) )
  404. for key in self.parameters.keys():
  405. node_socket = ui_node.inputs.get(key)
  406. if self.parameters[key] is not None: # the parameters are usually initialized as None.
  407. continue # will be filled by the node itself
  408. if not node_socket: #maybe the node socket has no name
  409. if ( ( len(ui_node.inputs) == 0) and ( len(ui_node.outputs) == 1) ):
  410. # this is a simple input node.
  411. node_socket = ui_node.outputs[0]
  412. elif key == 'Name': # for Links we just use the Node Label, or if there is no label, the name.
  413. self.parameters[key] = ui_node.label if ui_node.label else ui_node.name
  414. continue
  415. else:
  416. pass
  417. if node_socket:
  418. if node_socket.bl_idname in ['RelationshipSocket', 'xFormSocket']:
  419. continue
  420. elif node_socket.is_linked and (not node_socket.is_output):
  421. pass # we will get the value from the link, because this is a linked input port.
  422. # very importantly, we do not pass linked outputs- fill these because they are probably Input nodes.
  423. elif hasattr(node_socket, "default_value"):
  424. if (value := get_socket_value(node_socket)) is not None:
  425. self.parameters[key] = value
  426. # TODO: try and remove the input if it is not needed (for performance speed)
  427. else:
  428. raise RuntimeError(wrapRed("No value found for " + self.__repr__() + " when filling out node parameters for " + ui_node.name + "::"+node_socket.name))
  429. else:
  430. pass
  431. def call_on_all_ancestors(self, *args, **kwargs):
  432. """Resolve the dependencies of this node with the named method and its arguments.
  433. First, dependencies are discovered by walking backwards through the tree. Once the root
  434. nodes are discovered, the method is called by each node in dependency order.
  435. The first argument MUST be the name of the method as a string.
  436. """
  437. prGreen(self)
  438. if args[0] == 'call_on_all_ancestors': raise RuntimeError("Very funny!")
  439. from .utilities import get_all_dependencies
  440. from collections import deque
  441. # get all dependencies by walking backward through the tree.
  442. all_dependencies = get_all_dependencies(self)
  443. # get just the roots
  444. can_solve = deque(filter(lambda a : len(a.hierarchy_connections) == 0, all_dependencies))
  445. solved = set()
  446. while can_solve:
  447. node = can_solve.pop()
  448. print(node)
  449. method = getattr(node, args[0])
  450. method(*args[0:], **kwargs)
  451. solved.add(node)
  452. can_solve.extendleft(filter(lambda a : a in all_dependencies, node.hierarchy_connections))
  453. # prPurple(can_solve)
  454. if self in solved:
  455. break
  456. # else:
  457. # for dep in all_dependencies:
  458. # if dep not in solved:
  459. # prOrange(dep)
  460. return
  461. def bPrepare(self, bContext=None):
  462. return
  463. def bExecute(self, bContext=None):
  464. return
  465. def bFinalize(self, bContext=None):
  466. return
  467. def __repr__(self):
  468. return self.signature.__repr__()
  469. # do I need this and the link class above?
  470. class DummyLink:
  471. #gonna use this for faking links to keep the interface consistent
  472. def __init__(self, from_socket, to_socket, nc_from=None, nc_to=None, original_from=None, multi_input_sort_id=0):
  473. self.from_socket = from_socket
  474. self.to_socket = to_socket
  475. self.nc_from = nc_from
  476. self.nc_to = nc_to
  477. self.multi_input_sort_id = multi_input_sort_id
  478. # self.from_node = from_socket.node
  479. # self.to_node = to_socket.node
  480. if (original_from):
  481. self.original_from = original_from
  482. else:
  483. self.original_from = self.from_socket
  484. def __repr__(self):
  485. return(self.nc_from.__repr__()+":"+self.from_socket.name + " -> " + self.nc_to.__repr__()+":"+self.to_socket.name)
  486. class NodeLink:
  487. from_node = None
  488. from_socket = None
  489. to_node = None
  490. to_socket = None
  491. def __init__(self, from_node, from_socket, to_node, to_socket, multi_input_sort_id=0):
  492. if from_node.signature == to_node.signature:
  493. raise RuntimeError("Cannot connect a node to itself.")
  494. self.from_node = from_node
  495. self.from_socket = from_socket
  496. self.to_node = to_node
  497. self.to_socket = to_socket
  498. self.from_node.outputs[self.from_socket].links.append(self)
  499. # it is the responsibility of the node that uses these links to sort them correctly based on the sort_id
  500. self.multi_input_sort_id = multi_input_sort_id
  501. self.to_node.inputs[self.to_socket].links.append(self)
  502. from .node_container_common import detect_hierarchy_link
  503. self.is_hierarchy = detect_hierarchy_link(from_node, from_socket, to_node, to_socket,)
  504. self.is_alive = True
  505. def __repr__(self):
  506. return self.from_node.outputs[self.from_socket].__repr__() + " --> " + self.to_node.inputs[self.to_socket].__repr__()
  507. # link_string = # if I need to colorize output for debugging.
  508. # if self.is_hierarchy:
  509. # return wrapOrange(link_string)
  510. # else:
  511. # return wrapWhite(link_string)
  512. def die(self):
  513. self.is_alive = False
  514. self.to_node.inputs[self.to_socket].flush_links()
  515. self.from_node.outputs[self.from_socket].flush_links()
  516. def insert_node(self, middle_node, middle_node_in, middle_node_out, re_init_hierarchy = True):
  517. to_node = self.to_node
  518. to_socket = self.to_socket
  519. self.to_node = middle_node
  520. self.to_socket = middle_node_in
  521. middle_node.outputs[middle_node_out].connect(to_node, to_socket)
  522. if re_init_hierarchy:
  523. from .utilities import init_connections, init_dependencies
  524. init_connections(self.from_node)
  525. init_connections(middle_node)
  526. init_dependencies(middle_node)
  527. init_dependencies(to_node)
  528. class NodeSocket:
  529. # @property # this is a read-only property.
  530. # def is_linked(self):
  531. # return bool(self.links)
  532. def __init__(self, is_input = False,
  533. node = None, name = None,
  534. traverse_target = None):
  535. self.can_traverse = False # to/from the other side of the parent node
  536. self.traverse_target = None
  537. self.node = node
  538. self.name = name
  539. self.is_input = is_input
  540. self.links = []
  541. self.is_linked = False
  542. if (traverse_target):
  543. self.can_traverse = True
  544. def connect(self, node, socket, sort_id=0):
  545. if (self.is_input):
  546. to_node = self.node; from_node = node
  547. to_socket = self.name; from_socket = socket
  548. else:
  549. from_node = self.node; to_node = node
  550. from_socket = self.name; to_socket = socket
  551. from_node.outputs[from_socket].is_linked = True
  552. to_node.inputs[to_socket].is_linked = True
  553. for l in from_node.outputs[from_socket].links:
  554. if l.to_node==to_node and l.to_socket==to_socket:
  555. return None
  556. new_link = NodeLink(
  557. from_node,
  558. from_socket,
  559. to_node,
  560. to_socket,
  561. sort_id)
  562. return new_link
  563. def set_traverse_target(self, traverse_target):
  564. self.traverse_target = traverse_target
  565. self.can_traverse = True
  566. def flush_links(self):
  567. """ Removes dead links from this socket."""
  568. self.links = [l for l in self.links if l.is_alive]
  569. self.is_linked = bool(self.links)
  570. @property
  571. def is_connected(self):
  572. return len(self.links)>0
  573. def __repr__(self):
  574. return self.node.__repr__() + "::" + self.name
  575. class MantisNodeSocketCollection(dict):
  576. def __init__(self, node, is_input=False):
  577. self.is_input = is_input
  578. self.node = node
  579. def init_sockets(self, sockets):
  580. for socket in sockets:
  581. if not isinstance(socket, str): raise RuntimeError("NodeSocketCollection keys must be str.")
  582. self[socket] = NodeSocket(is_input=self.is_input, name=socket, node=self.node)
  583. def __delitem__(self, key):
  584. """Deletes a node socket by name, and all its links."""
  585. socket = self[key]
  586. for l in socket.links:
  587. l.die()
  588. super().__delitem__(key)
  589. def __iter__(self):
  590. """Makes the class iterable"""
  591. return iter(self.values())