misc_nodes.py 69 KB

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