misc_nodes.py 50 KB

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