misc_nodes.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. from .node_container_common import *
  2. from .base_definitions import MantisNode, NodeSocket
  3. from .xForm_containers import xFormArmature, xFormBone
  4. from .misc_nodes_socket_templates import *
  5. from math import pi, tau
  6. def TellClasses():
  7. return [
  8. # utility
  9. InputFloat,
  10. InputIntNode,
  11. InputVector,
  12. InputBoolean,
  13. InputBooleanThreeTuple,
  14. InputRotationOrder,
  15. InputTransformSpace,
  16. InputString,
  17. InputMatrix,
  18. InputExistingGeometryObject,
  19. InputExistingGeometryData,
  20. UtilityGeometryOfXForm,
  21. UtilityNameOfXForm,
  22. UtilityPointFromCurve,
  23. UtilityMatrixFromCurve,
  24. UtilityMatricesFromCurve,
  25. UtilityNumberOfCurveSegments,
  26. UtilityMatrixFromCurveSegment,
  27. UtilityGetCurvePoint,
  28. UtilityGetNearestFactorOnCurve,
  29. UtilityKDChoosePoint,
  30. UtilityKDChooseXForm,
  31. UtilityMetaRig,
  32. UtilityBoneProperties,
  33. UtilityDriverVariable,
  34. UtilityDriver,
  35. UtilityFCurve,
  36. UtilityKeyframe,
  37. UtilitySwitch,
  38. UtilityCombineThreeBool,
  39. UtilityCombineVector,
  40. UtilitySeparateVector,
  41. UtilityCatStrings,
  42. UtilityGetBoneLength,
  43. UtilityPointFromBoneMatrix,
  44. UtilitySetBoneLength,
  45. UtilityMatrixSetLocation,
  46. UtilityMatrixGetLocation,
  47. UtilityMatrixFromXForm,
  48. UtilityAxesFromMatrix,
  49. UtilityBoneMatrixHeadTailFlip,
  50. UtilityMatrixTransform,
  51. UtilityMatrixInvert,
  52. UtilityMatrixCompose,
  53. UtilityTransformationMatrix,
  54. UtilityIntToString,
  55. UtilityArrayGet,
  56. UtilityArrayLength,
  57. UtilitySetBoneMatrixTail,
  58. # Control flow switches
  59. UtilityCompare,
  60. UtilityChoose,
  61. # useful NoOp:
  62. UtilityPrint,
  63. ]
  64. def matrix_from_head_tail(head, tail, normal=None):
  65. from mathutils import Vector, Quaternion, Matrix
  66. if normal is None:
  67. rotation = Vector((0,1,0)).rotation_difference((tail-head).normalized()).to_matrix()
  68. m= Matrix.LocRotScale(head, rotation, None)
  69. else: # construct a basis matrix
  70. m = Matrix.Identity(3)
  71. axis = (tail-head).normalized()
  72. conormal = axis.cross(normal)
  73. m[0]=conormal
  74. m[1]=axis
  75. m[2]=normal
  76. m = m.transposed().to_4x4()
  77. m.translation=head.copy()
  78. m[3][3]=(tail-head).length
  79. return m
  80. def get_mesh_from_curve(curve_name : str, execution_id : str, bContext, ribbon=True):
  81. from bpy import data
  82. curve = data.objects.get(curve_name)
  83. assert curve.type == 'CURVE', f"ERROR: object is not a curve: {curve_name}"
  84. from .utilities import mesh_from_curve
  85. curve_type='ribbon' if ribbon else 'wire'
  86. m_name = curve_name+'.'+str(hash(curve_name+'.'+curve_type+'.'+execution_id))
  87. if not (m := data.meshes.get(m_name)):
  88. m = mesh_from_curve(curve, bContext, ribbon)
  89. m.name = m_name
  90. return m
  91. def cleanup_curve(curve_name : str, execution_id : str) -> None:
  92. import bpy
  93. curve = bpy.data.objects.get(curve_name)
  94. m_name = curve_name+'.'+str(hash(curve.name+'.'+ execution_id))
  95. if (mesh := bpy.data.meshes.get(m_name)):
  96. bpy.data.meshes.remove(mesh)
  97. def kd_find(node, points, ref_pt, num_points):
  98. if num_points == 0:
  99. raise RuntimeError(f"Cannot find 0 points for {node}")
  100. from mathutils import kdtree
  101. kd = kdtree.KDTree(len(points))
  102. for i, pt in enumerate(points):
  103. try:
  104. kd.insert(pt, i)
  105. except (TypeError, ValueError) as e:
  106. prRed(f"Cannot get point from for {node}")
  107. raise e
  108. kd.balance()
  109. try:
  110. if num_points == 1: # make it a list to keep it consistent with
  111. result = [kd.find(ref_pt)] # find_n which returns a list
  112. else:
  113. result = kd.find_n(ref_pt, num_points)
  114. # the result of kd.find has some other stuff we don't care about
  115. except (TypeError, ValueError) as e:
  116. prRed(f"Reference Point {ref_pt} invalid for {node}")
  117. raise e
  118. return result
  119. def array_choose_relink(node, indices, array_input, output, ):
  120. """
  121. Used to choose the correct link to send out of an array-choose node.
  122. """
  123. keep_links = []
  124. for index in indices:
  125. l = node.inputs[array_input].links[index]
  126. keep_links.append(l)
  127. for link in node.outputs[output].links:
  128. to_node = link.to_node
  129. for l in keep_links:
  130. l.from_node.outputs[l.from_socket].connect(to_node, link.to_socket)
  131. link.die()
  132. def array_choose_data(node, data, output):
  133. """
  134. Used to choose the correct data to send out of an array-choose node.
  135. """
  136. # We need to make new outputs and link from each one based on the data in the array...
  137. node.outputs.init_sockets([output+"."+str(i).zfill(4) for i in range(len(data)) ])
  138. for i, data_item in enumerate(data):
  139. node.parameters[output+"."+str(i).zfill(4)] = data_item
  140. for link in node.outputs[output].links:
  141. to_node = link.to_node
  142. for i in range(len(data)):
  143. # Make a link from the new output.
  144. node.outputs[output+"."+str(i).zfill(4)].connect(to_node, link.to_socket)
  145. link.die()
  146. #*#-------------------------------#++#-------------------------------#*#
  147. # U T I L I T Y N O D E S
  148. #*#-------------------------------#++#-------------------------------#*#
  149. class InputFloat(MantisNode):
  150. '''A node representing float input'''
  151. def __init__(self, signature, base_tree):
  152. super().__init__(signature, base_tree)
  153. outputs = ["Float Input"]
  154. self.outputs.init_sockets(outputs)
  155. self.init_parameters()
  156. self.node_type = 'UTILITY'
  157. self.prepared = True
  158. self.executed = True
  159. class InputIntNode(MantisNode):
  160. '''A node representing integer input'''
  161. def __init__(self, signature, base_tree):
  162. super().__init__(signature, base_tree)
  163. outputs = ["Integer"]
  164. self.outputs.init_sockets(outputs)
  165. self.init_parameters()
  166. self.node_type = 'UTILITY'
  167. self.prepared = True
  168. self.executed = True
  169. class InputVector(MantisNode):
  170. '''A node representing vector input'''
  171. def __init__(self, signature, base_tree):
  172. super().__init__(signature, base_tree)
  173. outputs = [""]
  174. self.outputs.init_sockets(outputs)
  175. self.init_parameters()
  176. self.node_type = 'UTILITY'
  177. self.prepared = True
  178. self.executed = True
  179. class InputBoolean(MantisNode):
  180. '''A node representing boolean input'''
  181. def __init__(self, signature, base_tree):
  182. super().__init__(signature, base_tree)
  183. outputs = [""]
  184. self.outputs.init_sockets(outputs)
  185. self.init_parameters()
  186. self.node_type = 'UTILITY'
  187. self.prepared = True
  188. self.executed = True
  189. class InputBooleanThreeTuple(MantisNode):
  190. '''A node representing a tuple of three booleans'''
  191. def __init__(self, signature, base_tree):
  192. super().__init__(signature, base_tree)
  193. outputs = [""]
  194. self.outputs.init_sockets(outputs)
  195. self.init_parameters()
  196. self.node_type = 'UTILITY'
  197. self.prepared = True
  198. self.executed = True
  199. class InputRotationOrder(MantisNode):
  200. '''A node representing string input for rotation order'''
  201. def __init__(self, signature, base_tree):
  202. super().__init__(signature, base_tree)
  203. outputs = [""]
  204. self.outputs.init_sockets(outputs)
  205. self.init_parameters()
  206. self.node_type = 'UTILITY'
  207. self.prepared = True
  208. self.executed = True
  209. class InputTransformSpace(MantisNode):
  210. '''A node representing string input for transform space'''
  211. def __init__(self, signature, base_tree):
  212. super().__init__(signature, base_tree)
  213. outputs = [""]
  214. self.outputs.init_sockets(outputs)
  215. self.init_parameters()
  216. self.node_type = 'UTILITY'
  217. self.prepared = True
  218. self.executed = True
  219. def evaluate_input(self, input_name):
  220. return self.parameters[""]
  221. class InputString(MantisNode):
  222. '''A node representing string input'''
  223. def __init__(self, signature, base_tree):
  224. super().__init__(signature, base_tree)
  225. outputs = [""]
  226. self.outputs.init_sockets(outputs)
  227. self.init_parameters()
  228. self.node_type = 'UTILITY'
  229. self.prepared = True
  230. self.executed = True
  231. class InputMatrix(MantisNode):
  232. '''A node representing axis-angle quaternion input'''
  233. def __init__(self, signature, base_tree):
  234. super().__init__(signature, base_tree)
  235. outputs = ["Matrix",]
  236. self.outputs.init_sockets(outputs)
  237. self.init_parameters()
  238. self.node_type = 'UTILITY'
  239. self.prepared = True
  240. self.executed = True
  241. class UtilityMatrixFromCurve(MantisNode):
  242. '''Get a matrix from a curve'''
  243. def __init__(self, signature, base_tree):
  244. super().__init__(signature, base_tree, MatrixFromCurveSockets)
  245. self.init_parameters()
  246. self.node_type = "UTILITY"
  247. def bPrepare(self, bContext = None,):
  248. from mathutils import Matrix
  249. import bpy
  250. mat = Matrix.Identity(4)
  251. curve_name = self.evaluate_input("Curve")
  252. curve = bpy.data.objects.get(curve_name)
  253. if not curve:
  254. prRed(f"WARN: No curve found for {self}. Using an identity matrix instead.")
  255. mat[3][3] = 1.0
  256. elif curve.type != "CURVE":
  257. prRed(f"WARN: Object {curve.name} is not a curve. Using an identity matrix instead.")
  258. mat[3][3] = 1.0
  259. else:
  260. if bContext is None: bContext = bpy.context # is this wise?
  261. m = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  262. from .utilities import data_from_ribbon_mesh
  263. #
  264. num_divisions = self.evaluate_input("Total Divisions")
  265. m_index = self.evaluate_input("Matrix Index")
  266. spline_index = self.evaluate_input("Spline Index")
  267. splines_factors = [ [] for i in range (spline_index)]
  268. factors = [1/num_divisions*m_index, 1/num_divisions*(m_index+1)]
  269. splines_factors.append(factors)
  270. data = data_from_ribbon_mesh(m, splines_factors, curve.matrix_world)
  271. head=data[spline_index][0][0]
  272. tail= data[spline_index][0][1]
  273. axis = (tail-head).normalized()
  274. normal=data[spline_index][2][0]
  275. # make sure the normal is perpendicular to the tail
  276. from .utilities import make_perpendicular
  277. normal = make_perpendicular(axis, normal)
  278. mat = matrix_from_head_tail(head, tail, normal)
  279. # this is in world space... let's just convert it back
  280. mat.translation = head - curve.location
  281. # TODO HACK TODO
  282. # all the nodes should work in world-space, and it should be the responsibility
  283. # of the xForm node to convert!
  284. self.parameters["Matrix"] = mat
  285. self.prepared = True
  286. self.executed = True
  287. def bFinalize(self, bContext=None):
  288. cleanup_curve(self.evaluate_input("Curve"), self.base_tree.execution_id)
  289. class UtilityPointFromCurve(MantisNode):
  290. '''Get a point from a curve'''
  291. def __init__(self, signature, base_tree):
  292. super().__init__(signature, base_tree, PointFromCurveSockets)
  293. self.init_parameters()
  294. self.node_type = "UTILITY"
  295. def bPrepare(self, bContext = None,):
  296. import bpy
  297. curve = bpy.data.objects.get(self.evaluate_input("Curve"))
  298. if not curve:
  299. raise RuntimeError(f"No curve found for {self}.")
  300. elif curve.type != "CURVE":
  301. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  302. else:
  303. if bContext is None: bContext = bpy.context # is this wise?
  304. m = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  305. from .utilities import data_from_ribbon_mesh
  306. #
  307. num_divisions = 1
  308. spline_index = self.evaluate_input("Spline Index")
  309. splines_factors = [ [] for i in range (spline_index)]
  310. factors = [self.evaluate_input("Factor")]
  311. splines_factors.append(factors)
  312. data = data_from_ribbon_mesh(m, splines_factors, curve.matrix_world)
  313. p = data[spline_index][0][0] - curve.location
  314. self.parameters["Point"] = p
  315. self.prepared, self.executed = True, True
  316. def bFinalize(self, bContext=None):
  317. cleanup_curve(self.evaluate_input("Curve"), self.base_tree.execution_id)
  318. class UtilityMatricesFromCurve(MantisNode):
  319. '''Get matrices from a curve'''
  320. def __init__(self, signature, base_tree):
  321. super().__init__(signature, base_tree, MatricesFromCurveSockets)
  322. self.init_parameters()
  323. self.node_type = "UTILITY"
  324. def bPrepare(self, bContext = None,):
  325. import time
  326. # start_time = time.time()
  327. #
  328. from mathutils import Matrix
  329. import bpy
  330. m = Matrix.Identity(4)
  331. curve_name = self.evaluate_input("Curve")
  332. curve = bpy.data.objects.get(curve_name)
  333. if not curve:
  334. prRed(f"WARN: No curve found for {self}. Using an identity matrix instead.")
  335. m[3][3] = 1.0
  336. elif curve.type != "CURVE":
  337. prRed(f"WARN: Object {curve.name} is not a curve. Using an identity matrix instead.")
  338. m[3][3] = 1.0
  339. else:
  340. if bContext is None: bContext = bpy.context # is this wise?
  341. mesh = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  342. from .utilities import data_from_ribbon_mesh
  343. num_divisions = self.evaluate_input("Total Divisions")
  344. spline_index = self.evaluate_input("Spline Index")
  345. splines_factors = [ [] for i in range (spline_index)]
  346. factors = [0.0] + [(1/num_divisions*(i+1)) for i in range(num_divisions)]
  347. splines_factors.append(factors)
  348. data = data_from_ribbon_mesh(mesh, splines_factors, curve.matrix_world)
  349. # [spline_index][points,tangents,normals][datapoint_index]
  350. from .utilities import make_perpendicular
  351. matrices=[]
  352. for i in range(num_divisions):
  353. m = matrix_from_head_tail (
  354. data[spline_index][0][i], data[spline_index][0][i+1],
  355. make_perpendicular((data[spline_index][0][i+1]-data[spline_index][0][i]).normalized(), data[spline_index][2][i]),)
  356. m.translation = data[spline_index][0][i] - curve.location
  357. matrices.append(m)
  358. for link in self.outputs["Matrices"].links:
  359. for i, m in enumerate(matrices):
  360. name = "Matrix"+str(i).zfill(4)
  361. if not (out := self.outputs.get(name)): # reuse them if there are multiple links.
  362. out = self.outputs[name] = NodeSocket(name = name, node=self)
  363. c = out.connect(link.to_node, link.to_socket)
  364. # prOrange(c)
  365. self.parameters[name] = m
  366. # print (mesh)
  367. link.die()
  368. self.prepared = True
  369. self.executed = True
  370. # prGreen(f"Matrices from curves took {time.time() - start_time} seconds.")
  371. def bFinalize(self, bContext=None):
  372. import bpy
  373. curve_name = self.evaluate_input("Curve")
  374. curve = bpy.data.objects.get(curve_name)
  375. m_name = curve.name+'.'+self.base_tree.execution_id
  376. if (mesh := bpy.data.meshes.get(m_name)):
  377. prGreen(f"Freeing mesh data {m_name}...")
  378. bpy.data.meshes.remove(mesh)
  379. class UtilityNumberOfCurveSegments(MantisNode):
  380. def __init__(self, signature, base_tree):
  381. super().__init__(signature, base_tree)
  382. inputs = [
  383. "Curve" ,
  384. "Spline Index" ,
  385. ]
  386. outputs = [
  387. "Number of Segments" ,
  388. ]
  389. self.inputs.init_sockets(inputs)
  390. self.outputs.init_sockets(outputs)
  391. self.init_parameters()
  392. self.node_type = "UTILITY"
  393. def bPrepare(self, bContext = None,):
  394. import bpy
  395. curve_name = self.evaluate_input("Curve")
  396. curve = bpy.data.objects.get(curve_name)
  397. spline = curve.data.splines[self.evaluate_input("Spline Index")]
  398. if spline.type == "BEZIER":
  399. self.parameters["Number of Segments"] = len(spline.bezier_points)-1
  400. else:
  401. self.parameters["Number of Segments"] = len(spline.points)-1
  402. self.prepared = True
  403. self.executed = True
  404. class UtilityMatrixFromCurveSegment(MantisNode):
  405. def __init__(self, signature, base_tree):
  406. super().__init__(signature, base_tree, MatrixFromCurveSegmentSockets)
  407. self.init_parameters()
  408. self.node_type = "UTILITY"
  409. def bPrepare(self, bContext = None,):
  410. import bpy
  411. curve = bpy.data.objects.get(self.evaluate_input("Curve"))
  412. if not curve:
  413. raise RuntimeError(f"No curve found for {self}.")
  414. elif curve.type != "CURVE":
  415. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  416. else:
  417. if bContext is None: bContext = bpy.context # is this wise?
  418. m = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  419. from .utilities import data_from_ribbon_mesh
  420. # this section is dumb, but it is because the data_from_ribbon_mesh
  421. # function is designed to pull data from many splines at once (for optimization)
  422. # so we have to give it empty splines for each one we skip.
  423. # TODO: Refactor this to make it so I can select spline index
  424. spline_index = self.evaluate_input("Spline Index")
  425. spline = curve.data.splines[spline_index]
  426. splines_factors = [ [] for i in range (spline_index)]
  427. factors = [0.0]
  428. points = spline.bezier_points if spline.type == 'BEZIER' else spline.points
  429. total_length=0.0
  430. for i in range(len(points)-1):
  431. total_length+= (seg_length := (points[i+1].co - points[i].co).length)
  432. factors.append(seg_length)
  433. prev_length = 0.0
  434. for i in range(len(factors)):
  435. factors[i] = prev_length+factors[i]/total_length
  436. prev_length=factors[i]
  437. # Why does this happen? Floating point error?
  438. if factors[i]>1.0: factors[i] = 1.0
  439. splines_factors.append(factors)
  440. #
  441. data = data_from_ribbon_mesh(m, splines_factors, curve.matrix_world)
  442. segment_index = self.evaluate_input("Segment Index")
  443. head=data[spline_index][0][segment_index]
  444. tail= data[spline_index][0][segment_index+1]
  445. axis = (tail-head).normalized()
  446. normal=data[spline_index][2][segment_index]
  447. # make sure the normal is perpendicular to the tail
  448. from .utilities import make_perpendicular
  449. normal = make_perpendicular(axis, normal)
  450. m = matrix_from_head_tail(head, tail, normal)
  451. m.translation = head - curve.location
  452. self.parameters["Matrix"] = m
  453. self.prepared, self.executed = True, True
  454. def bFinalize(self, bContext=None):
  455. cleanup_curve(self.evaluate_input("Curve"), self.base_tree.execution_id)
  456. class UtilityGetCurvePoint(MantisNode):
  457. def __init__(self, signature, base_tree):
  458. super().__init__(signature, base_tree, GetCurvePointSockets)
  459. self.init_parameters()
  460. self.node_type = "UTILITY"
  461. def bPrepare(self, bContext=None):
  462. import bpy
  463. curve = bpy.data.objects.get(self.evaluate_input("Curve"))
  464. if not curve:
  465. raise RuntimeError(f"No curve found for {self}.")
  466. elif curve.type != "CURVE":
  467. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  468. spline = curve.data.splines[self.evaluate_input("Spline Index")]
  469. if spline.type == 'BEZIER':
  470. bez_pt = spline.bezier_points[self.evaluate_input("Index")]
  471. self.parameters["Point"]=bez_pt.co
  472. self.parameters["Left Handle"]=bez_pt.handle_left
  473. self.parameters["Right Handle"]=bez_pt.handle_right
  474. else:
  475. pt = spline.points[self.evaluate_input("Index")]
  476. self.parameters["Point"]=(pt.co[0], pt.co[1], pt.co[2])
  477. self.prepared, self.executed = True, True
  478. class UtilityGetNearestFactorOnCurve(MantisNode):
  479. def __init__(self, signature, base_tree):
  480. super().__init__(signature, base_tree, GetNearestFactorOnCurveSockets)
  481. self.init_parameters()
  482. self.node_type = "UTILITY"
  483. def bPrepare(self, bContext = None,):
  484. import bpy
  485. curve = bpy.data.objects.get(self.evaluate_input("Curve"))
  486. if not curve:
  487. raise RuntimeError(f"No curve found for {self}.")
  488. elif curve.type != "CURVE":
  489. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  490. else:
  491. if bContext is None: bContext = bpy.context # is this wise?
  492. m = get_mesh_from_curve(curve.name,
  493. self.base_tree.execution_id,
  494. bContext, ribbon=False)
  495. # this is confusing but I am not re-writing these old functions
  496. from .utilities import FindNearestPointOnWireMesh as nearest_point
  497. spline_index = self.evaluate_input("Spline Index")
  498. ref_pt = self.evaluate_input("Reference Point")
  499. splines_points = [ [] for i in range (spline_index)]
  500. splines_points.append([ref_pt])
  501. pt = nearest_point(m, splines_points)[spline_index][0]
  502. self.parameters["Factor"] = pt
  503. self.prepared, self.executed = True, True
  504. class UtilityKDChoosePoint(MantisNode):
  505. def __init__(self, signature, base_tree):
  506. super().__init__(signature, base_tree)
  507. inputs = [
  508. "Reference Point" ,
  509. "Points" ,
  510. "Number to Find" ,
  511. ]
  512. outputs = [
  513. "Result Point" ,
  514. "Result Index" ,
  515. "Result Distance" ,
  516. ]
  517. self.inputs.init_sockets(inputs)
  518. self.outputs.init_sockets(outputs)
  519. self.init_parameters()
  520. self.node_type = "UTILITY"
  521. def bPrepare(self, bContext = None,):
  522. from mathutils import Vector
  523. points= []
  524. ref_point = self.evaluate_input('Reference Point')
  525. num_points = self.evaluate_input('Number to Find')
  526. for i, l in enumerate(self.inputs['Points'].links):
  527. pt = self.evaluate_input('Points', i)
  528. points.append(pt)
  529. if not isinstance(pt, Vector):
  530. prRed(f"Cannot get point from {l.from_node} for {self}")
  531. assert ref_point is not None, wrapRed(f"Reference Point {ref_point} is invalid in node {self}")
  532. result = kd_find(self, points, ref_point, num_points)
  533. indices = [ found_point[1] for found_point in result ]
  534. distances = [ found_point[2] for found_point in result ]
  535. array_choose_relink(self, indices, "Points", "Result Point")
  536. array_choose_data(self, indices, "Result Index")
  537. array_choose_data(self, distances, "Result Distance")
  538. self.prepared, self.executed = True, True
  539. class UtilityKDChooseXForm(MantisNode):
  540. def __init__(self, signature, base_tree):
  541. super().__init__(signature, base_tree)
  542. inputs = [
  543. "Reference Point" ,
  544. "xForm Nodes" ,
  545. "Get Point Head/Tail" ,
  546. "Number to Find" ,
  547. ]
  548. outputs = [
  549. "Result xForm" ,
  550. "Result Index" ,
  551. "Result Distance" ,
  552. ]
  553. self.inputs.init_sockets(inputs)
  554. self.outputs.init_sockets(outputs)
  555. self.init_parameters()
  556. self.node_type = "UTILITY"
  557. def bPrepare(self, bContext = None,):
  558. if len(self.hierarchy_dependencies)==0 and len(self.hierarchy_connections)==0 and \
  559. len(self.connections)==0 and len(self.dependencies)==0:
  560. self.prepared, self.executed = True, True
  561. return #Either it is already done or it doesn't matter.
  562. from mathutils import Vector
  563. points= []
  564. ref_point = self.evaluate_input('Reference Point')
  565. num_points = self.evaluate_input('Number to Find')
  566. for i, l in enumerate(self.inputs['xForm Nodes'].links):
  567. matrix = l.from_node.evaluate_input('Matrix')
  568. if matrix is None:
  569. raise GraphError(f"Cannot get point from {l.from_node} for {self}. Does it have a matrix?")
  570. pt = matrix.translation
  571. if head_tail := self.evaluate_input("Get Point Head/Tail"):
  572. # get the Y-axis of the basis, assume it is normalized
  573. y_axis = Vector((matrix[0][1],matrix[1][1], matrix[2][1]))
  574. pt = pt.lerp(pt+y_axis*matrix[3][3], head_tail)
  575. points.append(pt)
  576. if not isinstance(pt, Vector):
  577. prRed(f"Cannot get point from {l.from_node} for {self}")
  578. assert ref_point is not None, wrapRed(f"Reference Point {ref_point} is invalid in node {self}")
  579. result = kd_find(self, points, ref_point, num_points)
  580. indices = [ found_point[1] for found_point in result ]
  581. distances = [ found_point[2] for found_point in result ]
  582. array_choose_relink(self, indices, "xForm Nodes", "Result xForm")
  583. array_choose_data(self, indices, "Result Index")
  584. array_choose_data(self, distances, "Result Distance")
  585. self.prepared, self.executed = True, True
  586. class UtilityMetaRig(MantisNode):
  587. '''A node representing an armature object'''
  588. def __init__(self, signature, base_tree):
  589. super().__init__(signature, base_tree)
  590. inputs = [
  591. "Meta-Armature" ,
  592. "Meta-Bone" ,
  593. ]
  594. outputs = [
  595. "Matrix" ,
  596. ]
  597. self.inputs.init_sockets(inputs)
  598. self.outputs.init_sockets(outputs)
  599. self.init_parameters()
  600. self.node_type = "UTILITY"
  601. def bPrepare(self, bContext = None,):
  602. #kinda clumsy, whatever
  603. import bpy
  604. from mathutils import Matrix
  605. m = Matrix.Identity(4)
  606. meta_rig = self.evaluate_input("Meta-Armature")
  607. meta_bone = self.evaluate_input("Meta-Bone")
  608. if meta_rig:
  609. if ( armOb := bpy.data.objects.get(meta_rig) ):
  610. m = armOb.matrix_world
  611. if ( b := armOb.data.bones.get(meta_bone)):
  612. # calculate the correct object-space matrix
  613. m = Matrix.Identity(3)
  614. bones = [] # from the last ancestor, mult the matrices until we get to b
  615. while (b): bones.append(b); b = b.parent
  616. while (bones): b = bones.pop(); m = m @ b.matrix
  617. m = Matrix.Translation(b.head_local) @ m.to_4x4()
  618. #
  619. m[3][3] = b.length # this is where I arbitrarily decided to store length
  620. # else:
  621. # prRed("no bone for MetaRig node ", self)
  622. else:
  623. raise RuntimeError(wrapRed(f"No meta-rig input for MetaRig node {self}"))
  624. self.parameters["Matrix"] = m
  625. self.prepared = True
  626. self.executed = True
  627. class UtilityBoneProperties(MantisNode):
  628. '''A node representing a bone's gettable properties'''
  629. def __init__(self, signature, base_tree):
  630. super().__init__(signature, base_tree)
  631. outputs = [
  632. "matrix" ,
  633. "matrix_local" ,
  634. "matrix_basis" ,
  635. "head" ,
  636. "tail" ,
  637. "length" ,
  638. "rotation" ,
  639. "location" ,
  640. "scale" ,
  641. ]
  642. self.outputs.init_sockets(outputs)
  643. self.init_parameters()
  644. self.node_type = "UTILITY"
  645. self.prepared = True
  646. self.executed = True
  647. def fill_parameters(self, prototype=None):
  648. return
  649. # TODO this should probably be moved to Links
  650. class UtilityDriverVariable(MantisNode):
  651. '''A node representing an armature object'''
  652. def __init__(self, signature, base_tree):
  653. super().__init__(signature, base_tree)
  654. inputs = [
  655. "Variable Type" ,
  656. "Property" ,
  657. "Property Index" ,
  658. "Evaluation Space",
  659. "Rotation Mode" ,
  660. "xForm 1" ,
  661. "xForm 2" ,
  662. ]
  663. outputs = [
  664. "Driver Variable",
  665. ]
  666. self.inputs.init_sockets(inputs)
  667. self.outputs.init_sockets(outputs)
  668. self.init_parameters()
  669. self.node_type = "DRIVER" # MUST be run in Pose mode
  670. self.prepared = True
  671. def evaluate_input(self, input_name):
  672. if input_name == 'Property':
  673. if self.inputs.get('Property'):
  674. if self.inputs['Property'].is_linked:
  675. # get the name instead...
  676. trace = trace_single_line(self, input_name)
  677. return trace[1].name # the name of the socket
  678. return self.parameters["Property"]
  679. return super().evaluate_input(input_name)
  680. def GetxForm(self, index=1):
  681. trace = trace_single_line(self, "xForm 1" if index == 1 else "xForm 2")
  682. for node in trace[0]:
  683. if (node.__class__ in [xFormArmature, xFormBone]):
  684. return node #this will fetch the first one, that's good!
  685. return None
  686. def bExecute(self, bContext = None,):
  687. prepare_parameters(self)
  688. #prPurple ("Executing Driver Variable Node")
  689. xForm1 = self.GetxForm()
  690. xForm2 = self.GetxForm(index=2)
  691. # kinda clumsy
  692. if xForm1 : xForm1 = xForm1.bGetObject()
  693. if xForm2 : xForm2 = xForm2.bGetObject()
  694. v_type = self.evaluate_input("Variable Type")
  695. i = self.evaluate_input("Property Index"); dVarChannel = ""
  696. if (i >= 0): #negative values will use the vector property.
  697. if self.evaluate_input("Property") == 'location':
  698. if i == 0: dVarChannel = "LOC_X"
  699. elif i == 1: dVarChannel = "LOC_Y"
  700. elif i == 2: dVarChannel = "LOC_Z"
  701. else: raise RuntimeError("Invalid property index for %s" % self)
  702. if self.evaluate_input("Property") == 'rotation':
  703. if i == 0: dVarChannel = "ROT_X"
  704. elif i == 1: dVarChannel = "ROT_Y"
  705. elif i == 2: dVarChannel = "ROT_Z"
  706. elif i == 3: dVarChannel = "ROT_W"
  707. else: raise RuntimeError("Invalid property index for %s" % self)
  708. if self.evaluate_input("Property") == 'scale':
  709. if i == 0: dVarChannel = "SCALE_X"
  710. elif i == 1: dVarChannel = "SCALE_Y"
  711. elif i == 2: dVarChannel = "SCALE_Z"
  712. elif i == 3: dVarChannel = "SCALE_AVG"
  713. else: raise RuntimeError("Invalid property index for %s" % self)
  714. if self.evaluate_input("Property") == 'scale_average':
  715. dVarChannel = "SCALE_AVG"
  716. if dVarChannel: v_type = "TRANSFORMS"
  717. my_var = {
  718. "owner" : xForm1, # will be filled in by Driver
  719. "prop" : self.evaluate_input("Property"), # will be filled in by Driver
  720. "type" : v_type,
  721. "space" : self.evaluate_input("Evaluation Space"),
  722. "rotation_mode" : self.evaluate_input("Rotation Mode"),
  723. "xForm 1" : xForm1,#self.GetxForm(index = 1),
  724. "xForm 2" : xForm2,#self.GetxForm(index = 2),
  725. "channel" : dVarChannel,}
  726. # Push parameter to downstream connected node.connected:
  727. if (out := self.outputs["Driver Variable"]).is_linked:
  728. self.parameters[out.name] = my_var
  729. for link in out.links:
  730. link.to_node.parameters[link.to_socket] = my_var
  731. self.executed = True
  732. class UtilityKeyframe(MantisNode):
  733. '''A node representing a keyframe for a F-Curve'''
  734. def __init__(self, signature, base_tree):
  735. super().__init__(signature, base_tree)
  736. inputs = [
  737. "Frame" ,
  738. "Value" ,
  739. ]
  740. outputs = [
  741. "Keyframe" ,
  742. ]
  743. additional_parameters = {"Keyframe":{}}
  744. self.inputs.init_sockets(inputs)
  745. self.outputs.init_sockets(outputs)
  746. self.init_parameters( additional_parameters=additional_parameters)
  747. self.node_type = "DRIVER" # MUST be run in Pose mode
  748. setup_custom_props(self)
  749. def bPrepare(self, bContext = None,):
  750. key = self.parameters["Keyframe"]
  751. from mathutils import Vector
  752. key["co"]= Vector( (self.evaluate_input("Frame"), self.evaluate_input("Value"),))
  753. key["type"]="GENERATED"
  754. key["interpolation"] = "LINEAR"
  755. # eventually this will have the right data, TODO
  756. # self.parameters["Keyframe"] = key
  757. self.prepared = True
  758. self.executed = True
  759. class UtilityFCurve(MantisNode):
  760. '''A node representing an armature object'''
  761. def __init__(self, signature, base_tree):
  762. super().__init__(signature, base_tree)
  763. inputs = [
  764. "Extrapolation Mode",
  765. ]
  766. outputs = [
  767. "fCurve",
  768. ]
  769. self.inputs.init_sockets(inputs)
  770. self.outputs.init_sockets(outputs)
  771. self.init_parameters()
  772. self.node_type = "UTILITY"
  773. setup_custom_props(self)
  774. self.prepared = True
  775. def evaluate_input(self, input_name):
  776. return super().evaluate_input(input_name)
  777. def bExecute(self, bContext = None,):
  778. prepare_parameters(self)
  779. from .utilities import get_node_prototype
  780. np = get_node_prototype(self.signature, self.base_tree)
  781. extrap_mode = self.evaluate_input("Extrapolation Mode")
  782. keys = [] # ugly but whatever
  783. #['amplitude', 'back', 'bl_rna', 'co', 'co_ui', 'easing', 'handle_left', 'handle_left_type', 'handle_right', 'handle_right_type',
  784. # 'interpolation', 'period', 'rna_type', 'select_control_point', 'select_left_handle', 'select_right_handle', 'type']
  785. for k in self.inputs.keys():
  786. if k == 'Extrapolation Mode' : continue
  787. # print (self.inputs[k])
  788. if (key := self.evaluate_input(k)) is None:
  789. prOrange(f"WARN: No keyframe connected to {self}:{k}. Skipping Link.")
  790. else:
  791. keys.append(key)
  792. if len(keys) <1:
  793. prOrange(f"WARN: no keys in fCurve {self}.")
  794. keys.append(extrap_mode)
  795. self.parameters["fCurve"] = keys
  796. self.executed = True
  797. #TODO make the fCurve data a data class instead of a dict
  798. class UtilityDriver(MantisNode):
  799. '''A node representing an armature object'''
  800. def __init__(self, signature, base_tree):
  801. super().__init__(signature, base_tree)
  802. inputs = [
  803. "Driver Type" ,
  804. "Expression" ,
  805. "fCurve" ,
  806. ]
  807. outputs = [
  808. "Driver",
  809. ]
  810. from .drivers import MantisDriver
  811. additional_parameters = {
  812. "Driver":MantisDriver(),
  813. }
  814. self.inputs.init_sockets(inputs)
  815. self.outputs.init_sockets(outputs)
  816. self.init_parameters(additional_parameters=additional_parameters)
  817. self.node_type = "DRIVER" # MUST be run in Pose mode
  818. setup_custom_props(self)
  819. self.prepared = True
  820. def bExecute(self, bContext = None,):
  821. prepare_parameters(self)
  822. from .drivers import MantisDriver
  823. #prPurple("Executing Driver Node")
  824. my_vars = []
  825. keys = self.evaluate_input("fCurve")
  826. if keys is None or len(keys) <2:
  827. prWhite(f"INFO: no fCurve connected to {self}; using default fCurve.")
  828. from mathutils import Vector
  829. keys = [
  830. {"co":Vector( (0, 0,)), "type":"GENERATED", "interpolation":"LINEAR" },
  831. {"co":Vector( (1, 1,)), "type":"GENERATED", "interpolation":"LINEAR" },
  832. "CONSTANT",]
  833. for inp in list(self.inputs.keys() )[3:]:
  834. if (new_var := self.evaluate_input(inp)):
  835. new_var["name"] = inp
  836. my_vars.append(new_var)
  837. else:
  838. raise RuntimeError(f"Failed to initialize Driver variable for {self}")
  839. my_driver ={ "owner" : None,
  840. "prop" : None, # will be filled out in the node that uses the driver
  841. "expression" : self.evaluate_input("Expression"),
  842. "ind" : -1, # same here
  843. "type" : self.evaluate_input("Driver Type"),
  844. "vars" : my_vars,
  845. "keys" : keys[:-1],
  846. "extrapolation" : keys[-1] }
  847. my_driver = MantisDriver(my_driver)
  848. self.parameters["Driver"].update(my_driver)
  849. print("Initializing driver %s " % (wrapPurple(self.__repr__())) )
  850. self.executed = True
  851. class UtilitySwitch(MantisNode):
  852. '''A node representing an armature object'''
  853. def __init__(self, signature, base_tree):
  854. super().__init__(signature, base_tree)
  855. inputs = {
  856. "Parameter" ,
  857. "Parameter Index" ,
  858. "Invert Switch" ,
  859. }
  860. outputs = [
  861. "Driver",
  862. ]
  863. from .drivers import MantisDriver
  864. additional_parameters = {
  865. "Driver":MantisDriver(),
  866. }
  867. self.inputs.init_sockets(inputs)
  868. self.outputs.init_sockets(outputs)
  869. self.init_parameters(additional_parameters=additional_parameters)
  870. self.node_type = "DRIVER" # MUST be run in Pose mode
  871. self.prepared = True
  872. def evaluate_input(self, input_name):
  873. if input_name == 'Parameter':
  874. if self.inputs['Parameter'].is_connected:
  875. trace = trace_single_line(self, input_name)
  876. return trace[1].name # the name of the socket
  877. return self.parameters["Parameter"]
  878. return super().evaluate_input(input_name)
  879. def GetxForm(self,):
  880. trace = trace_single_line(self, "Parameter" )
  881. for node in trace[0]:
  882. if (node.__class__ in [xFormArmature, xFormBone]):
  883. return node #this will fetch the first one, that's good!
  884. return None
  885. def bExecute(self, bContext = None,):
  886. #prepare_parameters(self)
  887. #prPurple ("Executing Switch Node")
  888. xForm = self.GetxForm()
  889. if xForm : xForm = xForm.bGetObject()
  890. if not xForm:
  891. raise RuntimeError("Could not evaluate xForm for %s" % self)
  892. from .drivers import MantisDriver
  893. my_driver ={ "owner" : None,
  894. "prop" : None, # will be filled out in the node that uses the driver
  895. "ind" : -1, # same here
  896. "type" : "SCRIPTED",
  897. "vars" : [ { "owner" : xForm,
  898. "prop" : self.evaluate_input("Parameter"),
  899. "name" : "a",
  900. "type" : "SINGLE_PROP", } ],
  901. "keys" : [ { "co":(0,0),
  902. "interpolation": "LINEAR",
  903. "type":"KEYFRAME",}, #display type
  904. { "co":(1,1),
  905. "interpolation": "LINEAR",
  906. "type":"KEYFRAME",},],
  907. "extrapolation": 'CONSTANT', }
  908. my_driver ["expression"] = "a"
  909. my_driver = MantisDriver(my_driver)
  910. # this makes it so I can check for type later!
  911. if self.evaluate_input("Invert Switch") == True:
  912. my_driver ["expression"] = "1 - a"
  913. # this way, regardless of what order things are handled, the
  914. # driver is sent to the next node.
  915. # In the case of some drivers, the parameter may be sent out
  916. # before it's filled in (because there is a circular dependency)
  917. # I want to support this behaviour because Blender supports it.
  918. # We do not make a copy. We update the driver, so that
  919. # the same instance is filled out.
  920. self.parameters["Driver"].update(my_driver)
  921. print("Initializing driver %s " % (wrapPurple(self.__repr__())) )
  922. self.executed = True
  923. class UtilityCombineThreeBool(MantisNode):
  924. '''A node for combining three booleans into a boolean three-tuple'''
  925. def __init__(self, signature, base_tree):
  926. super().__init__(signature, base_tree)
  927. inputs = [
  928. "X" ,
  929. "Y" ,
  930. "Z" ,
  931. ]
  932. outputs = [
  933. "Three-Bool",
  934. ]
  935. self.inputs.init_sockets(inputs)
  936. self.outputs.init_sockets(outputs)
  937. self.init_parameters()
  938. self.node_type = "UTILITY"
  939. def bPrepare(self, bContext = None,):
  940. self.parameters["Three-Bool"] = (
  941. self.evaluate_input("X"),
  942. self.evaluate_input("Y"),
  943. self.evaluate_input("Z"), )
  944. self.prepared = True
  945. self.executed = True
  946. # Note this is a copy of the above. This needs to be de-duplicated.
  947. class UtilityCombineVector(MantisNode):
  948. '''A node for combining three floats into a vector'''
  949. def __init__(self, signature, base_tree):
  950. super().__init__(signature, base_tree)
  951. super().__init__(signature, base_tree)
  952. inputs = [
  953. "X" ,
  954. "Y" ,
  955. "Z" ,
  956. ]
  957. outputs = [
  958. "Vector",
  959. ]
  960. self.inputs.init_sockets(inputs)
  961. self.outputs.init_sockets(outputs)
  962. self.init_parameters()
  963. self.node_type = "UTILITY"
  964. def bPrepare(self, bContext = None,):
  965. #prPurple("Executing CombineVector Node")
  966. prepare_parameters(self)
  967. self.parameters["Vector"] = (
  968. self.evaluate_input("X"),
  969. self.evaluate_input("Y"),
  970. self.evaluate_input("Z"), )
  971. self.prepared = True
  972. self.executed = True
  973. class UtilitySeparateVector(MantisNode):
  974. '''A node for separating a vector into three floats'''
  975. def __init__(self, signature, base_tree):
  976. super().__init__(signature, base_tree)
  977. inputs = [
  978. "Vector"
  979. ]
  980. outputs = [
  981. "X" ,
  982. "Y" ,
  983. "Z" ,
  984. ]
  985. self.inputs.init_sockets(inputs)
  986. self.outputs.init_sockets(outputs)
  987. self.init_parameters()
  988. self.node_type = "UTILITY"
  989. def bPrepare(self, bContext = None,):
  990. self.parameters["X"] = self.evaluate_input("Vector")[0]
  991. self.parameters["Y"] = self.evaluate_input("Vector")[1]
  992. self.parameters["Z"] = self.evaluate_input("Vector")[2]
  993. self.prepared = True
  994. self.executed = True
  995. class UtilityCatStrings(MantisNode):
  996. '''A node representing an armature object'''
  997. def __init__(self, signature, base_tree):
  998. super().__init__(signature, base_tree)
  999. inputs = [
  1000. "String_1" ,
  1001. "String_2" ,
  1002. ]
  1003. outputs = [
  1004. "OutputString" ,
  1005. ]
  1006. self.inputs.init_sockets(inputs)
  1007. self.outputs.init_sockets(outputs)
  1008. self.init_parameters()
  1009. self.node_type = "UTILITY"
  1010. def bPrepare(self, bContext = None,):
  1011. self.parameters["OutputString"] = self.evaluate_input("String_1")+self.evaluate_input("String_2")
  1012. self.prepared = True
  1013. self.executed = True
  1014. class InputExistingGeometryObject(MantisNode):
  1015. '''A node representing an existing object'''
  1016. def __init__(self, signature, base_tree):
  1017. super().__init__(signature, base_tree)
  1018. inputs = [
  1019. "Name" ,
  1020. ]
  1021. outputs = [
  1022. "Object" ,
  1023. ]
  1024. self.inputs.init_sockets(inputs)
  1025. self.outputs.init_sockets(outputs)
  1026. self.init_parameters()
  1027. self.node_type = "XFORM"
  1028. def bPrepare(self, bContext=None):
  1029. from bpy import data
  1030. name = self.evaluate_input("Name")
  1031. if name:
  1032. self.bObject = data.objects.get( name )
  1033. else:
  1034. self.bObject = None
  1035. if self is None and (name := self.evaluate_input("Name")):
  1036. prRed(f"No object found with name {name} in {self}")
  1037. self.prepared = True; self.executed = True
  1038. def bGetObject(self, mode=''):
  1039. return self.bObject
  1040. class InputExistingGeometryData(MantisNode):
  1041. '''A node representing existing object data'''
  1042. def __init__(self, signature, base_tree):
  1043. super().__init__(signature, base_tree)
  1044. inputs = [
  1045. "Name" ,
  1046. ]
  1047. outputs = [
  1048. "Geometry" ,
  1049. ]
  1050. self.inputs.init_sockets(inputs)
  1051. self.outputs.init_sockets(outputs)
  1052. self.init_parameters()
  1053. self.node_type = "UTILITY"
  1054. self.prepared = True
  1055. self.executed = True
  1056. # the mode argument is only for interface consistency
  1057. def bGetObject(self, mode=''):
  1058. from bpy import data
  1059. # first try Curve, then try Mesh
  1060. bObject = data.curves.get(self.evaluate_input("Name"))
  1061. if not bObject:
  1062. bObject = data.meshes.get(self.evaluate_input("Name"))
  1063. if bObject is None:
  1064. raise RuntimeError(f"Could not find a mesh or curve datablock named \"{self.evaluate_input('Name')}\" for node {self}")
  1065. return bObject
  1066. class UtilityGeometryOfXForm(MantisNode):
  1067. '''A node representing existing object data'''
  1068. def __init__(self, signature, base_tree):
  1069. super().__init__(signature, base_tree)
  1070. inputs = [
  1071. "xForm" ,
  1072. ]
  1073. outputs = [
  1074. "Geometry" ,
  1075. ]
  1076. self.inputs.init_sockets(inputs)
  1077. self.outputs.init_sockets(outputs)
  1078. self.init_parameters()
  1079. self.node_type = "UTILITY"
  1080. self.prepared = True
  1081. self.executed = True
  1082. # mode for interface consistency
  1083. def bGetObject(self, mode=''):
  1084. if not (self.inputs.get('xForm') and self.inputs['xForm'].links):
  1085. prOrange(f"WARN: Cannot retrieve data from {self}, there is no xForm node connected.")
  1086. return None
  1087. xf = self.inputs["xForm"].links[0].from_node
  1088. if xf.node_type == 'XFORM':
  1089. xf_ob = xf.bGetObject()
  1090. if xf_ob.type in ['MESH', 'CURVE']:
  1091. return xf_ob.data
  1092. prOrange(f"WARN: Cannot retrieve data from {self}, the connected xForm is not a mesh or curve.")
  1093. return None
  1094. class UtilityNameOfXForm(MantisNode):
  1095. '''A node representing existing object data'''
  1096. def __init__(self, signature, base_tree):
  1097. super().__init__(signature, base_tree)
  1098. inputs = [
  1099. "xForm" ,
  1100. ]
  1101. outputs = [
  1102. "Name" ,
  1103. ]
  1104. self.inputs.init_sockets(inputs)
  1105. self.outputs.init_sockets(outputs)
  1106. self.init_parameters()
  1107. self.node_type = "UTILITY"
  1108. # mode for interface consistency
  1109. def bPrepare(self, bContext = None,):
  1110. if not (self.inputs.get('xForm') and self.inputs['xForm'].links):
  1111. prOrange(f"WARN: Cannot retrieve data from {self}, there is no xForm node connected.")
  1112. return ''
  1113. xf = self.inputs["xForm"].links[0].from_node
  1114. self.parameters["Name"] = xf.evaluate_input('Name')
  1115. self.prepared, self.executed = True, True
  1116. class UtilityGetBoneLength(MantisNode):
  1117. '''A node to get the length of a bone matrix'''
  1118. def __init__(self, signature, base_tree):
  1119. super().__init__(signature, base_tree)
  1120. inputs = [
  1121. "Bone Matrix" ,
  1122. ]
  1123. outputs = [
  1124. "Bone Length" ,
  1125. ]
  1126. self.inputs.init_sockets(inputs)
  1127. self.outputs.init_sockets(outputs)
  1128. self.init_parameters()
  1129. self.node_type = "UTILITY"
  1130. def bPrepare(self, bContext = None,):
  1131. if (l := self.evaluate_input("Bone Matrix")) is not None:
  1132. self.parameters["Bone Length"] = l[3][3]
  1133. else:
  1134. other = self.inputs["Bone Matrix"].links[0].from_node
  1135. raise RuntimeError(f"Cannot get matrix for {self} from {other}")
  1136. self.prepared = True
  1137. self.executed = True
  1138. class UtilityPointFromBoneMatrix(MantisNode):
  1139. '''A node representing an armature object'''
  1140. def __init__(self, signature, base_tree):
  1141. super().__init__(signature, base_tree)
  1142. inputs = [
  1143. "Bone Matrix" ,
  1144. "Head/Tail" ,
  1145. ]
  1146. outputs = [
  1147. "Point" ,
  1148. ]
  1149. self.inputs.init_sockets(inputs)
  1150. self.outputs.init_sockets(outputs)
  1151. self.init_parameters()
  1152. self.node_type = "UTILITY"
  1153. # TODO: find out why this is sometimes not ready at bPrepare phase
  1154. def bPrepare(self, bContext = None,):
  1155. from mathutils import Vector
  1156. matrix = self.evaluate_input("Bone Matrix")
  1157. head, rotation, _scale = matrix.copy().decompose()
  1158. tail = head.copy() + (rotation @ Vector((0,1,0)))*matrix[3][3]
  1159. self.parameters["Point"] = head.lerp(tail, self.evaluate_input("Head/Tail"))
  1160. self.prepared = True
  1161. self.executed = True
  1162. class UtilitySetBoneLength(MantisNode):
  1163. '''Sets the length of a Bone's matrix'''
  1164. def __init__(self, signature, base_tree):
  1165. super().__init__(signature, base_tree)
  1166. inputs = [
  1167. "Bone Matrix" ,
  1168. "Length" ,
  1169. ]
  1170. outputs = [
  1171. "Bone Matrix" ,
  1172. ]
  1173. self.inputs.init_sockets(inputs)
  1174. self.outputs.init_sockets(outputs)
  1175. self.init_parameters()
  1176. self.node_type = "UTILITY"
  1177. def bPrepare(self, bContext = None,):
  1178. from mathutils import Vector
  1179. if matrix := self.evaluate_input("Bone Matrix"):
  1180. matrix = matrix.copy()
  1181. # print (self.inputs["Length"].links)
  1182. matrix[3][3] = self.evaluate_input("Length")
  1183. self.parameters["Length"] = self.evaluate_input("Length")
  1184. self.parameters["Bone Matrix"] = matrix
  1185. else:
  1186. raise RuntimeError(f"Cannot get matrix for {self}")
  1187. self.prepared = True
  1188. self.executed = True
  1189. class UtilityMatrixSetLocation(MantisNode):
  1190. '''Sets the location of a matrix'''
  1191. def __init__(self, signature, base_tree):
  1192. super().__init__(signature, base_tree)
  1193. inputs = [
  1194. "Matrix" ,
  1195. "Location" ,
  1196. ]
  1197. outputs = [
  1198. "Matrix" ,
  1199. ]
  1200. self.inputs.init_sockets(inputs)
  1201. self.outputs.init_sockets(outputs)
  1202. self.init_parameters()
  1203. self.node_type = "UTILITY"
  1204. def bPrepare(self, bContext = None,):
  1205. from mathutils import Vector
  1206. if matrix := self.evaluate_input("Matrix"):
  1207. matrix = matrix.copy()
  1208. # print (self.inputs["Length"].links)
  1209. loc = self.evaluate_input("Location")
  1210. matrix[0][3] = loc[0]; matrix[1][3] = loc[1]; matrix[2][3] = loc[2]
  1211. self.parameters["Matrix"] = matrix
  1212. self.prepared = True
  1213. self.executed = True
  1214. class UtilityMatrixGetLocation(MantisNode):
  1215. '''Gets the location of a matrix'''
  1216. def __init__(self, signature, base_tree):
  1217. super().__init__(signature, base_tree)
  1218. inputs = [
  1219. "Matrix" ,
  1220. ]
  1221. outputs = [
  1222. "Location" ,
  1223. ]
  1224. self.inputs.init_sockets(inputs)
  1225. self.outputs.init_sockets(outputs)
  1226. self.init_parameters()
  1227. self.node_type = "UTILITY"
  1228. def bPrepare(self, bContext = None,):
  1229. from mathutils import Vector
  1230. if matrix := self.evaluate_input("Matrix"):
  1231. self.parameters["Location"] = matrix.to_translation()
  1232. self.prepared = True; self.executed = True
  1233. class UtilityMatrixFromXForm(MantisNode):
  1234. """Returns the matrix of the given xForm node."""
  1235. def __init__(self, signature, base_tree):
  1236. super().__init__(signature, base_tree)
  1237. inputs = [
  1238. "xForm" ,
  1239. ]
  1240. outputs = [
  1241. "Matrix" ,
  1242. ]
  1243. self.node_type = "UTILITY"
  1244. self.inputs.init_sockets(inputs)
  1245. self.outputs.init_sockets(outputs)
  1246. self.init_parameters()
  1247. def GetxForm(self):
  1248. trace = trace_single_line(self, "xForm")
  1249. for node in trace[0]:
  1250. if (node.node_type == 'XFORM'):
  1251. return node
  1252. raise GraphError("%s is not connected to an xForm" % self)
  1253. def bPrepare(self, bContext = None,):
  1254. from mathutils import Vector, Matrix
  1255. self.parameters["Matrix"] = Matrix.Identity(4)
  1256. if matrix := self.GetxForm().parameters.get("Matrix"):
  1257. self.parameters["Matrix"] = matrix.copy()
  1258. elif hasattr(self.GetxForm().bObject, "matrix"):
  1259. self.parameters["Matrix"] = self.GetxForm().bObject.matrix.copy()
  1260. elif hasattr(self.GetxForm().bObject, "matrix_world"):
  1261. self.parameters["Matrix"] = self.GetxForm().bObject.matrix_world.copy()
  1262. else:
  1263. prRed(f"Could not find matrix for {self} - check if the referenced object exists.")
  1264. self.prepared = True; self.executed = True
  1265. class UtilityAxesFromMatrix(MantisNode):
  1266. """Returns the axes of the given matrix."""
  1267. def __init__(self, signature, base_tree):
  1268. super().__init__(signature, base_tree)
  1269. inputs = [
  1270. "Matrix" ,
  1271. ]
  1272. outputs = [
  1273. "X Axis" ,
  1274. "Y Axis" ,
  1275. "Z Axis" ,
  1276. ]
  1277. self.inputs.init_sockets(inputs)
  1278. self.outputs.init_sockets(outputs)
  1279. self.init_parameters()
  1280. self.node_type = "UTILITY"
  1281. def bPrepare(self, bContext = None,):
  1282. from mathutils import Vector
  1283. if matrix := self.evaluate_input("Matrix"):
  1284. matrix= matrix.copy().to_3x3(); matrix.transpose()
  1285. self.parameters['X Axis'] = matrix[0]
  1286. self.parameters['Y Axis'] = matrix[1]
  1287. self.parameters['Z Axis'] = matrix[2]
  1288. self.prepared = True; self.executed = True
  1289. class UtilityBoneMatrixHeadTailFlip(MantisNode):
  1290. def __init__(self, signature, base_tree):
  1291. super().__init__(signature, base_tree)
  1292. inputs = [
  1293. "Bone Matrix" ,
  1294. ]
  1295. outputs = [
  1296. "Bone Matrix" ,
  1297. ]
  1298. self.inputs.init_sockets(inputs)
  1299. self.outputs.init_sockets(outputs)
  1300. self.init_parameters()
  1301. self.node_type = "UTILITY"
  1302. def bPrepare(self, bContext = None,):
  1303. from mathutils import Vector, Matrix, Quaternion
  1304. from bpy.types import Bone
  1305. if matrix := self.evaluate_input("Bone Matrix"):
  1306. axis, roll = Bone.AxisRollFromMatrix(matrix.to_3x3())
  1307. new_mat = Bone.MatrixFromAxisRoll(-1*axis, roll)
  1308. length = matrix[3][3]
  1309. new_mat.resize_4x4() # last column contains
  1310. new_mat[0][3] = matrix[0][3] + axis[0]*length # x location
  1311. new_mat[1][3] = matrix[1][3] + axis[1]*length # y location
  1312. new_mat[2][3] = matrix[2][3] + axis[2]*length # z location
  1313. new_mat[3][3] = length # length
  1314. self.parameters["Bone Matrix"] = new_mat
  1315. self.prepared, self.executed = True, True
  1316. class UtilityMatrixTransform(MantisNode):
  1317. def __init__(self, signature, base_tree):
  1318. super().__init__(signature, base_tree)
  1319. inputs = [
  1320. "Matrix 1" ,
  1321. "Matrix 2" ,
  1322. ]
  1323. outputs = [
  1324. "Out Matrix" ,
  1325. ]
  1326. self.inputs.init_sockets(inputs)
  1327. self.outputs.init_sockets(outputs)
  1328. self.init_parameters()
  1329. self.node_type = "UTILITY"
  1330. def bPrepare(self, bContext = None,):
  1331. from mathutils import Vector
  1332. mat1 = self.evaluate_input("Matrix 1"); mat2 = self.evaluate_input("Matrix 2")
  1333. if mat1 and mat2:
  1334. mat1copy = mat1.copy()
  1335. self.parameters["Out Matrix"] = mat2 @ mat1copy
  1336. self.parameters["Out Matrix"].translation = mat1copy.to_translation()+ mat2.to_translation()
  1337. else:
  1338. raise RuntimeError(wrapRed(f"Node {self} did not receive all matrix inputs... found input 1? {mat1 is not None}, 2? {mat2 is not None}"))
  1339. self.prepared = True
  1340. self.executed = True
  1341. class UtilityMatrixInvert(MantisNode):
  1342. def __init__(self, signature, base_tree):
  1343. super().__init__(signature, base_tree, MatrixInvertSockets)
  1344. self.init_parameters()
  1345. self.node_type = "UTILITY"
  1346. def bPrepare(self, bContext = None,):
  1347. from mathutils import Vector
  1348. mat1 = self.evaluate_input("Matrix 1")
  1349. if mat1:
  1350. mat1copy = mat1.copy()
  1351. try:
  1352. self.parameters["Matrix"] = mat1copy.inverted()
  1353. except ValueError as e:
  1354. prRed(f"ERROR: {self}: The matrix cannot be inverted.")
  1355. prOrange(mat1)
  1356. raise e
  1357. else:
  1358. raise RuntimeError(wrapRed(f"Node {self} did not receive all matrix inputs... found input 1? {mat1 is not None}"))
  1359. self.prepared = True
  1360. self.executed = True
  1361. class UtilityMatrixCompose(MantisNode):
  1362. def __init__(self, signature, base_tree):
  1363. super().__init__(signature, base_tree, MatrixComposeSockets)
  1364. self.init_parameters()
  1365. self.node_type = "UTILITY"
  1366. def bPrepare(self, bContext = None,):
  1367. from mathutils import Matrix
  1368. matrix= Matrix.Identity(3)
  1369. matrix[0] = self.evaluate_input('X Basis Vector')
  1370. matrix[1] = self.evaluate_input('Y Basis Vector')
  1371. matrix[2] = self.evaluate_input('Z Basis Vector')
  1372. matrix.transpose(); matrix=matrix.to_4x4()
  1373. matrix.translation = self.evaluate_input('Translation')
  1374. self.parameters['Matrix']=matrix
  1375. self.prepared = True
  1376. self.executed = True
  1377. class UtilityTransformationMatrix(MantisNode):
  1378. def __init__(self, signature, base_tree):
  1379. super().__init__(signature, base_tree)
  1380. inputs = [
  1381. "Operation" ,
  1382. "Vector" ,
  1383. "W" ,
  1384. ]
  1385. outputs = [
  1386. "Matrix" ,
  1387. ]
  1388. self.inputs.init_sockets(inputs)
  1389. self.outputs.init_sockets(outputs)
  1390. self.init_parameters()
  1391. self.node_type = "UTILITY"
  1392. def bPrepare(self, bContext = None,):
  1393. from mathutils import Matrix, Vector
  1394. if (operation := self.evaluate_input("Operation")) == 'ROTATE_AXIS_ANGLE':
  1395. # this can, will, and should fail if the axis is 0,0,0
  1396. self.parameters["Matrix"] = rotMat = Matrix.Rotation(self.evaluate_input("W"), 4, Vector(self.evaluate_input("Vector")).normalized())
  1397. elif (operation := self.evaluate_input("Operation")) == 'TRANSLATE':
  1398. m = Matrix.Identity(4)
  1399. if axis := self.evaluate_input("Vector"):
  1400. m[0][3]=axis[0];m[1][3]=axis[1];m[2][3]=axis[2]
  1401. self.parameters['Matrix'] = m
  1402. elif (operation := self.evaluate_input("Operation")) == 'SCALE':
  1403. self.parameters["Matrix"] = Matrix.Scale(self.evaluate_input("W"), 4, Vector(self.evaluate_input("Vector")).normalized())
  1404. else:
  1405. raise NotImplementedError(self.evaluate_input("Operation").__repr__()+ " Operation not yet implemented.")
  1406. self.prepared = True
  1407. self.executed = True
  1408. class UtilityIntToString(MantisNode):
  1409. def __init__(self, signature, base_tree):
  1410. super().__init__(signature, base_tree)
  1411. inputs = [
  1412. "Number" ,
  1413. "Zero Padding" ,
  1414. ]
  1415. outputs = [
  1416. "String" ,
  1417. ]
  1418. self.inputs.init_sockets(inputs)
  1419. self.outputs.init_sockets(outputs)
  1420. self.init_parameters()
  1421. self.node_type = "UTILITY"
  1422. def bPrepare(self, bContext = None,):
  1423. number = self.evaluate_input("Number")
  1424. zeroes = self.evaluate_input("Zero Padding")
  1425. # I'm casting to int because I want to support any number, even though the node asks for int.
  1426. self.parameters["String"] = str(int(number)).zfill(int(zeroes))
  1427. self.prepared = True
  1428. self.executed = True
  1429. class UtilityArrayGet(MantisNode):
  1430. def __init__(self, signature, base_tree):
  1431. super().__init__(signature, base_tree)
  1432. inputs = [
  1433. "Index" ,
  1434. "OoB Behaviour" ,
  1435. "Array" ,
  1436. ]
  1437. outputs = [
  1438. "Output" ,
  1439. ]
  1440. self.inputs.init_sockets(inputs)
  1441. self.outputs.init_sockets(outputs)
  1442. self.init_parameters()
  1443. self.node_type = "UTILITY"
  1444. def bPrepare(self, bContext = None,):
  1445. if len(self.hierarchy_dependencies)==0 and len(self.hierarchy_connections)==0 and \
  1446. len(self.connections)==0 and len(self.dependencies)==0:
  1447. self.prepared, self.executed = True, True
  1448. return #Either it is already done or it doesn't matter.
  1449. elif self.prepared == False:
  1450. # sort the array entries
  1451. for inp in self.inputs.values():
  1452. inp.links.sort(key=lambda a : -a.multi_input_sort_id)
  1453. oob = self.evaluate_input("OoB Behaviour")
  1454. index = self.evaluate_input("Index")
  1455. from .utilities import cap, wrap
  1456. # we must assume that the array has sent the correct number of links
  1457. if oob == 'WRAP':
  1458. index = index % len(self.inputs['Array'].links)
  1459. if oob == 'HOLD':
  1460. index = cap(index, len(self.inputs['Array'].links)-1)
  1461. array_choose_relink(self, [index], "Array", "Output")
  1462. self.prepared, self.executed = True, True
  1463. class UtilityArrayLength(MantisNode):
  1464. def __init__(self, signature, base_tree):
  1465. super().__init__(signature, base_tree)
  1466. inputs = [
  1467. "Array" ,
  1468. ]
  1469. outputs = [
  1470. "Length" ,
  1471. ]
  1472. self.inputs.init_sockets(inputs)
  1473. self.outputs.init_sockets(outputs)
  1474. self.init_parameters()
  1475. self.node_type = "UTILITY"
  1476. def bPrepare(self, bContext = None,):
  1477. self.parameters["Length"] = len(self.inputs["Array"].links)
  1478. self.prepared, self.executed = True, True
  1479. class UtilitySetBoneMatrixTail(MantisNode):
  1480. def __init__(self, signature, base_tree):
  1481. super().__init__(signature, base_tree)
  1482. inputs = {
  1483. "Matrix" ,
  1484. "Tail Location" ,
  1485. }
  1486. outputs = [
  1487. "Result" ,
  1488. ]
  1489. self.inputs.init_sockets(inputs)
  1490. self.outputs.init_sockets(outputs)
  1491. self.init_parameters()
  1492. self.node_type = "UTILITY"
  1493. def bPrepare(self, bContext = None,):
  1494. from mathutils import Matrix
  1495. matrix = self.evaluate_input("Matrix")
  1496. if matrix is None: matrix = Matrix.Identity(4)
  1497. #just do this for now lol
  1498. self.parameters["Result"] = matrix_from_head_tail(matrix.translation, self.evaluate_input("Tail Location"))
  1499. self.prepared = True
  1500. self.executed = True
  1501. class UtilityPrint(MantisNode):
  1502. def __init__(self, signature, base_tree):
  1503. super().__init__(signature, base_tree)
  1504. inputs = [
  1505. "Input" ,
  1506. ]
  1507. self.inputs.init_sockets(inputs)
  1508. self.init_parameters()
  1509. self.node_type = "UTILITY"
  1510. def bPrepare(self, bContext = None,):
  1511. if my_input := self.evaluate_input("Input"):
  1512. print("Preparation phase: ", wrapWhite(self), wrapGreen(my_input))
  1513. # else:
  1514. # prRed("No input to print.")
  1515. self.prepared = True
  1516. def bExecute(self, bContext = None,):
  1517. if my_input := self.evaluate_input("Input"):
  1518. print("Execution phase: ", wrapWhite(self), wrapGreen(my_input))
  1519. # else:
  1520. # prRed("No input to print.")
  1521. self.executed = True
  1522. class UtilityCompare(MantisNode):
  1523. def __init__(self, signature, base_tree):
  1524. super().__init__(signature, base_tree, CompareSockets)
  1525. self.init_parameters()
  1526. self.node_type = "UTILITY"
  1527. def bPrepare(self, bContext = None,):
  1528. operation=self.evaluate_input("Comparison")
  1529. a = self.evaluate_input("A")
  1530. b = self.evaluate_input("B")
  1531. if isinstance(a, str) and isinstance(b, str) and \
  1532. operation not in ['EQUAL', 'NOT_EQUAL']:
  1533. raise GraphError("Strings do not have numerical value to"
  1534. " compute greater than or less than.")
  1535. match operation:
  1536. case "EQUAL":
  1537. self.parameters["Result"] = a == b
  1538. case "NOT_EQUAL":
  1539. self.parameters["Result"] = a != b
  1540. case "GREATER_THAN":
  1541. self.parameters["Result"] = a > b
  1542. case "GREATER_THAN_EQUAL":
  1543. self.parameters["Result"] = a >= b
  1544. case "LESS_THAN":
  1545. self.parameters["Result"] = a < b
  1546. case "LESS_THAN_EQUAL":
  1547. self.parameters["Result"] = a <= b
  1548. self.prepared = True; self.executed = True
  1549. class UtilityChoose(MantisNode):
  1550. def __init__(self, signature, base_tree):
  1551. super().__init__(signature, base_tree)
  1552. inputs = [
  1553. "Condition" ,
  1554. "A" ,
  1555. "B" ,
  1556. ]
  1557. outputs = [
  1558. "Result" ,
  1559. ]
  1560. self.inputs.init_sockets(inputs)
  1561. self.outputs.init_sockets(outputs)
  1562. self.init_parameters()
  1563. self.node_type = "UTILITY"
  1564. def bPrepare(self, bContext = None,):
  1565. prGreen(f"Executing Choose Node {self}")
  1566. print (self.parameters.items())
  1567. condition = self.evaluate_input("Condition")
  1568. if self.evaluate_input('A') is not None and self.evaluate_input('B') is not None:
  1569. self.parameters['Result'] = self.evaluate_input('B') if condition else self.evaluate_input('A')
  1570. elif self.evaluate_input('A') is None and self.evaluate_input('B') is None:
  1571. if condition: link = self.inputs['B'].links[0]
  1572. else: link = self.inputs['A'].links[0]
  1573. from_node = link.from_node; from_socket = link.from_socket
  1574. for link in self.outputs['Result'].links:
  1575. from_node.outputs[from_socket].connect(link.to_node, link.to_socket)
  1576. link.die()
  1577. self.flush_links()
  1578. # attempting to init the connections seems more error prone than leaving them be.
  1579. else:
  1580. raise GraphError(f"Choose Node {self} has incorrect types.")
  1581. self.prepared = True; self.executed = True