misc_nodes.py 55 KB

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