misc_containers.py 65 KB

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