base_definitions.py 26 KB

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