base_definitions.py 37 KB

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