misc_nodes.py 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  1. from .node_common import *
  2. from .base_definitions import MantisNode, NodeSocket, FLOAT_EPSILON
  3. from .xForm_nodes import xFormArmature, xFormBone
  4. from .misc_nodes_socket_templates import *
  5. from math import pi, tau
  6. def TellClasses():
  7. return [
  8. # utility
  9. InputFloat,
  10. InputIntNode,
  11. InputVector,
  12. InputBoolean,
  13. InputBooleanThreeTuple,
  14. InputRotationOrder,
  15. InputTransformSpace,
  16. InputString,
  17. InputMatrix,
  18. InputWidget,
  19. InputExistingGeometryObject,
  20. InputExistingGeometryData,
  21. InputThemeBoneColorSets,
  22. InputColorSetPallete,
  23. UtilityCustomProperty,
  24. UtilityDeclareCollections,
  25. UtilityCollectionJoin,
  26. UtilityCollectionHierarchy,
  27. UtilityGeometryOfXForm,
  28. UtilityNameOfXForm,
  29. UtilityPointFromCurve,
  30. UtilityMatrixFromCurve,
  31. UtilityMatricesFromCurve,
  32. UtilityNumberOfCurveSegments,
  33. UtilityNumberOfSplines,
  34. UtilityMatrixFromCurveSegment,
  35. UtilityGetCurvePoint,
  36. UtilityGetNearestFactorOnCurve,
  37. UtilityKDChoosePoint,
  38. UtilityKDChooseXForm,
  39. UtilityMetaRig,
  40. UtilityBoneProperties,
  41. UtilityDriverVariable,
  42. UtilityDriver,
  43. UtilityFCurve,
  44. UtilityKeyframe,
  45. UtilitySwitch,
  46. UtilityCombineThreeBool,
  47. UtilityCombineVector,
  48. UtilitySeparateVector,
  49. UtilityCatStrings,
  50. UtilityGetBoneLength,
  51. UtilityPointFromBoneMatrix,
  52. UtilitySetBoneLength,
  53. UtilityMatrixSetLocation,
  54. UtilityMatrixGetLocation,
  55. UtilityMatrixFromXForm,
  56. UtilityAxesFromMatrix,
  57. UtilityBoneMatrixHeadTailFlip,
  58. UtilityMatrixTransform,
  59. UtilityMatrixInvert,
  60. UtilityMatrixCompose,
  61. UtilityMatrixAlignRoll,
  62. UtilityTransformationMatrix,
  63. UtilityIntToString,
  64. UtilityArrayGet,
  65. UtilityArrayLength,
  66. UtilitySetBoneMatrixTail,
  67. # Control flow switches
  68. UtilityCompare,
  69. UtilityChoose,
  70. # useful NoOp:
  71. UtilityPrint,
  72. ]
  73. def matrix_from_head_tail(head, tail, normal=None):
  74. from mathutils import Vector, Matrix
  75. if normal is None:
  76. rotation = Vector((0,1,0)).rotation_difference((tail-head).normalized()).to_matrix()
  77. m= Matrix.LocRotScale(head, rotation, None)
  78. else: # construct a basis matrix
  79. m = Matrix.Identity(3)
  80. axis = (tail-head).normalized()
  81. conormal = axis.cross(normal)
  82. m[0]=conormal
  83. m[1]=axis
  84. m[2]=normal
  85. m = m.transposed().to_4x4()
  86. m.translation=head.copy()
  87. m[3][3]=(tail-head).length
  88. return m
  89. def get_mesh_from_curve(curve_name : str, execution_id : str, bContext, ribbon=True):
  90. from bpy import data
  91. curve = data.objects.get(curve_name)
  92. assert curve.type == 'CURVE', f"ERROR: object is not a curve: {curve_name}"
  93. from .utilities import mesh_from_curve
  94. curve_type='ribbon' if ribbon else 'wire'
  95. m_name = curve_name+'.'+str(hash(curve_name+'.'+curve_type+'.'+execution_id))
  96. if not (m := data.meshes.get(m_name)):
  97. m = mesh_from_curve(curve, bContext, ribbon)
  98. m.name = m_name
  99. return m
  100. def cleanup_curve(curve_name : str, execution_id : str) -> None:
  101. import bpy
  102. curve = bpy_object_get_guarded(curve_name)
  103. m_name = curve_name+'.'+str(hash(curve.name+'.'+ execution_id))
  104. if (mesh := bpy.data.meshes.get(m_name)):
  105. bpy.data.meshes.remove(mesh)
  106. def kd_find(node, points, ref_pt, num_points):
  107. if num_points == 0:
  108. raise RuntimeError(f"Cannot find 0 points for {node}")
  109. from mathutils import kdtree
  110. kd = kdtree.KDTree(len(points))
  111. for i, pt in enumerate(points):
  112. try:
  113. kd.insert(pt, i)
  114. except (TypeError, ValueError) as e:
  115. prRed(f"Cannot get point from for {node}")
  116. raise e
  117. kd.balance()
  118. try:
  119. if num_points == 1: # make it a list to keep it consistent with
  120. result = [kd.find(ref_pt)] # find_n which returns a list
  121. else:
  122. result = kd.find_n(ref_pt, num_points)
  123. # the result of kd.find has some other stuff we don't care about
  124. except (TypeError, ValueError) as e:
  125. prRed(f"Reference Point {ref_pt} invalid for {node}")
  126. raise e
  127. return result
  128. def array_link_init_hierarchy(new_link):
  129. " Sets up hierarchy connection/dependencies for links created by Arrays."
  130. if new_link.is_hierarchy:
  131. connections = new_link.from_node.hierarchy_connections
  132. dependencies = new_link.to_node.hierarchy_dependencies
  133. else:
  134. connections = new_link.from_node.connections
  135. dependencies = new_link.to_node.dependencies
  136. connections.append(new_link.to_node)
  137. dependencies.append(new_link.from_node)
  138. def array_choose_relink(node, indices, array_input, output, ):
  139. """
  140. Used to choose the correct link to send out of an array-choose node.
  141. """
  142. keep_links = []
  143. for index in indices:
  144. l = node.inputs[array_input].links[index]
  145. keep_links.append(l)
  146. for link in node.outputs[output].links:
  147. to_node = link.to_node
  148. for l in keep_links:
  149. new_link = l.from_node.outputs[l.from_socket].connect(to_node, link.to_socket)
  150. array_link_init_hierarchy(new_link)
  151. node.rerouted.append(new_link) # so I can access this in Schema Solve
  152. link.die()
  153. def array_choose_data(node, data, output):
  154. """
  155. Used to choose the correct data to send out of an array-choose node.
  156. """
  157. # We need to make new outputs and link from each one based on the data in the array...
  158. node.outputs.init_sockets([output+"."+str(i).zfill(4) for i in range(len(data)) ])
  159. for i, data_item in enumerate(data):
  160. node.parameters[output+"."+str(i).zfill(4)] = data_item
  161. for link in node.outputs[output].links:
  162. to_node = link.to_node
  163. for i in range(len(data)):
  164. # Make a link from the new output.
  165. new_link = node.outputs[output+"."+str(i).zfill(4)].connect(to_node, link.to_socket)
  166. array_link_init_hierarchy(new_link)
  167. link.die()
  168. def zero_radius_error_message(node, curve):
  169. return f"ERROR: cannot get matrix from zero-radius curve point "\
  170. "in curve object: {curve.name} for node: {node}. "\
  171. "This is a limitation of Mantis (For now). Please inspect the curve and ensure "\
  172. "that each curve point has a radius greater than 0. Sometimes, this error is " \
  173. "caused by drivers. "
  174. def bpy_object_get_guarded(get_name, node=None):
  175. result=None
  176. if not isinstance(get_name, str):
  177. raise RuntimeError(f"Cannot get object for {node} because the """
  178. f"requested name is not a string, but {type(get_name)}. ")
  179. try:
  180. import bpy
  181. result = bpy.data.objects.get(get_name)
  182. except SystemError:
  183. raise SystemError(f"184 {node} Cannot get object, {get_name}"
  184. " please report this as a bug.")
  185. return result
  186. #*#-------------------------------#++#-------------------------------#*#
  187. # B A S E C L A S S E S
  188. #*#-------------------------------#++#-------------------------------#*#
  189. class SimpleInputNode(MantisNode):
  190. def __init__(self, signature, base_tree, socket_templates=[]):
  191. super().__init__(signature, base_tree, socket_templates)
  192. self.node_type = 'UTILITY'
  193. self.prepared, self.executed = True, True
  194. #*#-------------------------------#++#-------------------------------#*#
  195. # U T I L I T Y N O D E S
  196. #*#-------------------------------#++#-------------------------------#*#
  197. class InputFloat(SimpleInputNode):
  198. '''A node representing float input'''
  199. def __init__(self, signature, base_tree):
  200. super().__init__(signature, base_tree)
  201. outputs = ["Float Input"]
  202. self.outputs.init_sockets(outputs)
  203. self.init_parameters()
  204. class InputIntNode(SimpleInputNode):
  205. '''A node representing integer input'''
  206. def __init__(self, signature, base_tree):
  207. super().__init__(signature, base_tree)
  208. outputs = ["Integer"]
  209. self.outputs.init_sockets(outputs)
  210. self.init_parameters()
  211. class InputVector(SimpleInputNode):
  212. '''A node representing vector input'''
  213. def __init__(self, signature, base_tree):
  214. super().__init__(signature, base_tree)
  215. outputs = [""]
  216. self.outputs.init_sockets(outputs)
  217. self.init_parameters()
  218. class InputBoolean(SimpleInputNode):
  219. '''A node representing boolean input'''
  220. def __init__(self, signature, base_tree):
  221. super().__init__(signature, base_tree)
  222. outputs = [""]
  223. self.outputs.init_sockets(outputs)
  224. self.init_parameters()
  225. class InputBooleanThreeTuple(SimpleInputNode):
  226. '''A node representing a tuple of three booleans'''
  227. def __init__(self, signature, base_tree):
  228. super().__init__(signature, base_tree)
  229. outputs = [""]
  230. self.outputs.init_sockets(outputs)
  231. self.init_parameters()
  232. class InputRotationOrder(SimpleInputNode):
  233. '''A node representing string input for rotation order'''
  234. def __init__(self, signature, base_tree):
  235. super().__init__(signature, base_tree)
  236. outputs = [""]
  237. self.outputs.init_sockets(outputs)
  238. self.init_parameters()
  239. class InputTransformSpace(SimpleInputNode):
  240. '''A node representing string input for transform space'''
  241. def __init__(self, signature, base_tree):
  242. super().__init__(signature, base_tree)
  243. outputs = [""]
  244. self.outputs.init_sockets(outputs)
  245. self.init_parameters()
  246. def evaluate_input(self, input_name):
  247. return self.parameters[""]
  248. class InputString(SimpleInputNode):
  249. '''A node representing string input'''
  250. def __init__(self, signature, base_tree):
  251. super().__init__(signature, base_tree)
  252. outputs = [""]
  253. self.outputs.init_sockets(outputs)
  254. self.init_parameters()
  255. class InputMatrix(SimpleInputNode):
  256. '''A node representing axis-angle quaternion input'''
  257. def __init__(self, signature, base_tree):
  258. super().__init__(signature, base_tree)
  259. outputs = ["Matrix",]
  260. self.outputs.init_sockets(outputs)
  261. self.init_parameters()
  262. class InputThemeBoneColorSets(SimpleInputNode):
  263. '''A node representing the theme's colors'''
  264. def __init__(self, signature, base_tree):
  265. super().__init__(signature, base_tree)
  266. from .mantis_dataclasses import MantisSocketTemplate
  267. outputs = []
  268. for i in range(20):
  269. outputs.append (MantisSocketTemplate(
  270. name = f"Color {str(i).zfill(2)}",
  271. ))
  272. self.outputs.init_sockets(outputs)
  273. self.init_parameters()
  274. # we'll go ahead and fill them here
  275. def fill_parameters(self, ui_node=None):
  276. if not ui_node:
  277. from .utilities import get_ui_node
  278. ui_node = get_ui_node(self.ui_signature, self.base_tree)
  279. for i in range(20):
  280. self.parameters[f"Color {str(i).zfill(2)}"] = ui_node.outputs[i].default_value
  281. return super().fill_parameters(ui_node)
  282. class InputColorSetPallete(SimpleInputNode):
  283. '''A node representing the theme's colors'''
  284. def __init__(self, signature, base_tree):
  285. super().__init__(signature, base_tree)
  286. def fill_parameters(self, ui_node=None):
  287. if not ui_node:
  288. from .utilities import get_ui_node
  289. ui_node = get_ui_node(self.ui_signature, self.base_tree)
  290. from .mantis_dataclasses import MantisSocketTemplate
  291. outputs = []
  292. for o in ui_node.outputs:
  293. outputs.append (MantisSocketTemplate( name = o.name,))
  294. self.outputs.init_sockets(outputs)
  295. self.init_parameters()
  296. for o in ui_node.outputs:
  297. self.parameters[o.name] = o.default_value
  298. return super().fill_parameters(ui_node)
  299. class UtilityCustomProperty(MantisNode):
  300. def __init__(self, signature, base_tree):
  301. super().__init__(signature, base_tree, UtilityCustomPropertySockets)
  302. self.init_parameters()
  303. self.node_type = "UTILITY"
  304. # this should output a dataclass containing custom prop info
  305. # then the xForm will use it to assign custom properties
  306. def bPrepare(self, bContext=None):
  307. from .mantis_dataclasses import custom_prop_template
  308. # it's kind of silly to do it this way, but whatever
  309. name = self.evaluate_input("Name")
  310. assert name, "The custom property must have a name."
  311. prop_type = self.evaluate_input("Type")
  312. kwargs = {
  313. "name" : name,
  314. "prop_type" : prop_type,
  315. "description" : self.evaluate_input("Description"),
  316. }
  317. match prop_type:
  318. case 'BOOL':
  319. kwargs['default_value_bool'] = self.evaluate_input("Default (Bool)")
  320. case 'INT':
  321. kwargs['default_value_int'] = self.evaluate_input("Default (Int)")
  322. kwargs['min'] = self.evaluate_input("Min (Int)")
  323. kwargs['soft_min'] = self.evaluate_input("Soft Min (Int)")
  324. kwargs['max'] = self.evaluate_input("Max (Int)")
  325. kwargs['soft_max'] = self.evaluate_input("Soft Max (Int)")
  326. case 'FLOAT':
  327. kwargs['default_value_float'] = self.evaluate_input("Default (Float)")
  328. kwargs['min'] = self.evaluate_input("Min (Float)")
  329. kwargs['soft_min'] = self.evaluate_input("Soft Min (Float)")
  330. kwargs['max'] = self.evaluate_input("Max (Float)")
  331. kwargs['soft_max'] = self.evaluate_input("Soft Max (Float)")
  332. case 'VECTOR':
  333. kwargs['default_value_vector'] = self.evaluate_input("Default (Vector)")
  334. kwargs['min'] = self.evaluate_input("Min (Float)")
  335. kwargs['soft_min'] = self.evaluate_input("Soft Min (Float)")
  336. kwargs['max'] = self.evaluate_input("Max (Float)")
  337. kwargs['soft_max'] = self.evaluate_input("Soft Max (Float)")
  338. case 'STRING':
  339. kwargs['default_value_string'] = self.evaluate_input("Default (String)")
  340. my_data = custom_prop_template( **kwargs)
  341. self.parameters['Custom Property'] = my_data
  342. self.prepared = True; self.executed = True
  343. class UtilityMatrixFromCurve(MantisNode):
  344. '''Get a matrix from a curve'''
  345. def __init__(self, signature, base_tree):
  346. super().__init__(signature, base_tree, MatrixFromCurveSockets)
  347. self.init_parameters()
  348. self.node_type = "UTILITY"
  349. def bPrepare(self, bContext = None,):
  350. from mathutils import Matrix
  351. import bpy
  352. mat = Matrix.Identity(4)
  353. curve_name = self.evaluate_input("Curve")
  354. curve = bpy_object_get_guarded( curve_name, self)
  355. if not curve:
  356. prRed(f"WARN: No curve found for {self}. Using an identity matrix instead.")
  357. mat[3][3] = 1.0
  358. elif curve.type != "CURVE":
  359. prRed(f"WARN: Object {curve.name} is not a curve. Using an identity matrix instead.")
  360. mat[3][3] = 1.0
  361. else:
  362. if bContext is None: bContext = bpy.context # is this wise?
  363. m = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  364. from .utilities import data_from_ribbon_mesh
  365. #
  366. num_divisions = self.evaluate_input("Total Divisions")
  367. if num_divisions <= 0:
  368. raise GraphError("The number of divisions in the curve must be 1 or greater.")
  369. m_index = self.evaluate_input("Matrix Index")
  370. if m_index >= num_divisions:
  371. prRed(m_index, num_divisions)
  372. raise GraphError(f"{self} tried to get a matrix-index greater the total number of divisions."
  373. "The matrix index starts at 0. You're probably off by +1.")
  374. spline_index = self.evaluate_input("Spline Index")
  375. if spline_index > len(curve.data.splines)-1:
  376. raise GraphError(f"{self} is attempting to read from a spline in {curve.name} that does not exist."
  377. " Try and reduce the value of Spline Index.")
  378. splines_factors = [ [] for i in range (spline_index)]
  379. factors = [1/num_divisions*m_index, 1/num_divisions*(m_index+1)]
  380. splines_factors.append(factors)
  381. data = data_from_ribbon_mesh(m, splines_factors, curve.matrix_world)
  382. if data[spline_index][1][0] < FLOAT_EPSILON: # radius is None:
  383. raise RuntimeError(zero_radius_error_message(self, curve))
  384. head=data[spline_index][0][0]
  385. tail= data[spline_index][0][1]
  386. axis = (tail-head).normalized()
  387. if axis.length_squared < FLOAT_EPSILON:
  388. raise RuntimeError(f"Failed to read the curve {curve.name}.")
  389. normal=data[spline_index][2][0]
  390. # make sure the normal is perpendicular to the tail
  391. from .utilities import make_perpendicular
  392. normal = make_perpendicular(axis, normal)
  393. mat = matrix_from_head_tail(head, tail, normal)
  394. # this is in world space... let's just convert it back
  395. mat.translation = head - curve.location
  396. # TODO HACK TODO
  397. # all the nodes should work in world-space, and it should be the responsibility
  398. # of the xForm node to convert!
  399. self.parameters["Matrix"] = mat
  400. self.prepared = True
  401. self.executed = True
  402. def bFinalize(self, bContext=None):
  403. cleanup_curve(self.evaluate_input("Curve"), self.base_tree.execution_id)
  404. class UtilityPointFromCurve(MantisNode):
  405. '''Get a point from a curve'''
  406. def __init__(self, signature, base_tree):
  407. super().__init__(signature, base_tree, PointFromCurveSockets)
  408. self.init_parameters()
  409. self.node_type = "UTILITY"
  410. def bPrepare(self, bContext = None,):
  411. import bpy
  412. curve_name = self.evaluate_input("Curve")
  413. curve = bpy_object_get_guarded( curve_name, self)
  414. if not curve:
  415. raise RuntimeError(f"No curve found for {self}.")
  416. elif curve.type != "CURVE":
  417. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  418. else:
  419. if bContext is None: bContext = bpy.context # is this wise?
  420. m = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  421. from .utilities import data_from_ribbon_mesh
  422. #
  423. num_divisions = 1
  424. spline_index = self.evaluate_input("Spline Index")
  425. splines_factors = [ [] for i in range (spline_index)]
  426. factors = [self.evaluate_input("Factor")]
  427. splines_factors.append(factors)
  428. data = data_from_ribbon_mesh(m, splines_factors, curve.matrix_world)
  429. p = data[spline_index][0][0] - curve.location
  430. self.parameters["Point"] = p
  431. self.prepared, self.executed = True, True
  432. def bFinalize(self, bContext=None):
  433. cleanup_curve(self.evaluate_input("Curve"), self.base_tree.execution_id)
  434. class UtilityMatricesFromCurve(MantisNode):
  435. '''Get matrices from a curve'''
  436. def __init__(self, signature, base_tree):
  437. super().__init__(signature, base_tree, MatricesFromCurveSockets)
  438. self.init_parameters()
  439. self.node_type = "UTILITY"
  440. def bPrepare(self, bContext = None,):
  441. import time
  442. # start_time = time.time()
  443. #
  444. from mathutils import Matrix
  445. import bpy
  446. m = Matrix.Identity(4)
  447. curve_name = self.evaluate_input("Curve")
  448. curve = bpy_object_get_guarded( curve_name, self)
  449. if not curve:
  450. prRed(f"WARN: No curve found for {self}. Using an identity matrix instead.")
  451. m[3][3] = 1.0
  452. elif curve.type != "CURVE":
  453. prRed(f"WARN: Object {curve.name} is not a curve. Using an identity matrix instead.")
  454. m[3][3] = 1.0
  455. else:
  456. if bContext is None: bContext = bpy.context # is this wise?
  457. mesh = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  458. from .utilities import data_from_ribbon_mesh
  459. num_divisions = self.evaluate_input("Total Divisions")
  460. spline_index = self.evaluate_input("Spline Index")
  461. splines_factors = [ [] for i in range (spline_index)]
  462. factors = [0.0] + [(1/num_divisions*(i+1)) for i in range(num_divisions)]
  463. splines_factors.append(factors)
  464. data = data_from_ribbon_mesh(mesh, splines_factors, curve.matrix_world)
  465. # [spline_index][points,tangents,normals][datapoint_index]
  466. from .utilities import make_perpendicular
  467. matrices=[]
  468. for i in range(num_divisions):
  469. if data[spline_index][1][i] < FLOAT_EPSILON: # radius is None:
  470. raise RuntimeError(zero_radius_error_message(self, curve))
  471. m = matrix_from_head_tail (
  472. data[spline_index][0][i], data[spline_index][0][i+1],
  473. make_perpendicular((data[spline_index][0][i+1]-data[spline_index][0][i]).normalized(), data[spline_index][2][i]),)
  474. m.translation = data[spline_index][0][i] - curve.location
  475. matrices.append(m)
  476. for link in self.outputs["Matrices"].links:
  477. for i, m in enumerate(matrices):
  478. name = "Matrix"+str(i).zfill(4)
  479. if not (out := self.outputs.get(name)): # reuse them if there are multiple links.
  480. out = self.outputs[name] = NodeSocket(name = name, node=self)
  481. c = out.connect(link.to_node, link.to_socket)
  482. # prOrange(c)
  483. self.parameters[name] = m
  484. # print (mesh)
  485. link.die()
  486. self.prepared = True
  487. self.executed = True
  488. # prGreen(f"Matrices from curves took {time.time() - start_time} seconds.")
  489. def bFinalize(self, bContext=None):
  490. import bpy
  491. curve_name = self.evaluate_input("Curve")
  492. curve = bpy_object_get_guarded( curve_name, self)
  493. m_name = curve.name+'.'+self.base_tree.execution_id
  494. if (mesh := bpy.data.meshes.get(m_name)):
  495. prGreen(f"Freeing mesh data {m_name}...")
  496. bpy.data.meshes.remove(mesh)
  497. class UtilityNumberOfCurveSegments(MantisNode):
  498. def __init__(self, signature, base_tree):
  499. super().__init__(signature, base_tree)
  500. inputs = [
  501. "Curve" ,
  502. "Spline Index" ,
  503. ]
  504. outputs = [
  505. "Number of Segments" ,
  506. ]
  507. self.inputs.init_sockets(inputs)
  508. self.outputs.init_sockets(outputs)
  509. self.init_parameters()
  510. self.node_type = "UTILITY"
  511. def bPrepare(self, bContext = None,):
  512. curve_name = self.evaluate_input("Curve")
  513. curve = bpy_object_get_guarded( curve_name, self)
  514. spline = curve.data.splines[self.evaluate_input("Spline Index")]
  515. if spline.type == "BEZIER":
  516. self.parameters["Number of Segments"] = len(spline.bezier_points)-1
  517. else:
  518. self.parameters["Number of Segments"] = len(spline.points)-1
  519. self.prepared = True
  520. self.executed = True
  521. class UtilityNumberOfSplines(MantisNode):
  522. def __init__(self, signature, base_tree):
  523. super().__init__(signature, base_tree, NumberOfSplinesSockets)
  524. self.init_parameters()
  525. self.node_type = "UTILITY"
  526. def bPrepare(self, bContext = None,):
  527. curve_name = self.evaluate_input("Curve")
  528. curve = bpy_object_get_guarded( curve_name, self)
  529. self.parameters["Number of Splines"] = len(curve.data.splines)
  530. self.prepared, self.executed = True, True
  531. class UtilityMatrixFromCurveSegment(MantisNode):
  532. def __init__(self, signature, base_tree):
  533. super().__init__(signature, base_tree, MatrixFromCurveSegmentSockets)
  534. self.init_parameters()
  535. self.node_type = "UTILITY"
  536. def bPrepare(self, bContext = None,):
  537. import bpy
  538. curve_name = self.evaluate_input("Curve")
  539. curve = bpy_object_get_guarded( curve_name, self)
  540. if not curve:
  541. raise RuntimeError(f"No curve found for {self}.")
  542. elif curve.type != "CURVE":
  543. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  544. else:
  545. if bContext is None: bContext = bpy.context # is this wise?
  546. m = get_mesh_from_curve(curve.name, self.base_tree.execution_id, bContext)
  547. from .utilities import data_from_ribbon_mesh
  548. # this section is dumb, but it is because the data_from_ribbon_mesh
  549. # function is designed to pull data from many splines at once (for optimization)
  550. # so we have to give it empty splines for each one we skip.
  551. # TODO: Refactor this to make it so I can select spline index
  552. spline_index = self.evaluate_input("Spline Index")
  553. spline = curve.data.splines[spline_index]
  554. splines_factors = [ [] for i in range (spline_index)]
  555. factors = [0.0]
  556. points = spline.bezier_points if spline.type == 'BEZIER' else spline.points
  557. total_length=0.0
  558. for i in range(len(points)-1):
  559. total_length+= (seg_length := (points[i+1].co - points[i].co).length)
  560. factors.append(seg_length)
  561. prev_length = 0.0
  562. for i in range(len(factors)):
  563. factors[i] = prev_length+factors[i]/total_length
  564. prev_length=factors[i]
  565. # Why does this happen? Floating point error?
  566. if factors[i]>1.0: factors[i] = 1.0
  567. splines_factors.append(factors)
  568. #
  569. data = data_from_ribbon_mesh(m, splines_factors, curve.matrix_world)
  570. segment_index = self.evaluate_input("Segment Index")
  571. if data[spline_index][1][segment_index] < FLOAT_EPSILON: # radius is None:
  572. raise RuntimeError(zero_radius_error_message(self, curve))
  573. head=data[spline_index][0][segment_index]
  574. tail= data[spline_index][0][segment_index+1]
  575. axis = (tail-head).normalized()
  576. normal=data[spline_index][2][segment_index]
  577. # make sure the normal is perpendicular to the tail
  578. from .utilities import make_perpendicular
  579. normal = make_perpendicular(axis, normal)
  580. m = matrix_from_head_tail(head, tail, normal)
  581. m.translation = head - curve.location
  582. self.parameters["Matrix"] = m
  583. self.prepared, self.executed = True, True
  584. def bFinalize(self, bContext=None):
  585. cleanup_curve(self.evaluate_input("Curve"), self.base_tree.execution_id)
  586. class UtilityGetCurvePoint(MantisNode):
  587. def __init__(self, signature, base_tree):
  588. super().__init__(signature, base_tree, GetCurvePointSockets)
  589. self.init_parameters()
  590. self.node_type = "UTILITY"
  591. def bPrepare(self, bContext=None):
  592. import bpy
  593. curve_name = self.evaluate_input("Curve")
  594. curve = bpy_object_get_guarded( curve_name, self)
  595. if not curve:
  596. raise RuntimeError(f"No curve found for {self}.")
  597. elif curve.type != "CURVE":
  598. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  599. spline = curve.data.splines[self.evaluate_input("Spline Index")]
  600. if spline.type == 'BEZIER':
  601. bez_pt = spline.bezier_points[self.evaluate_input("Index")]
  602. self.parameters["Point"]=bez_pt.co
  603. self.parameters["Left Handle"]=bez_pt.handle_left
  604. self.parameters["Right Handle"]=bez_pt.handle_right
  605. else:
  606. pt = spline.points[self.evaluate_input("Index")]
  607. self.parameters["Point"]=(pt.co[0], pt.co[1], pt.co[2])
  608. self.prepared, self.executed = True, True
  609. class UtilityGetNearestFactorOnCurve(MantisNode):
  610. def __init__(self, signature, base_tree):
  611. super().__init__(signature, base_tree, GetNearestFactorOnCurveSockets)
  612. self.init_parameters()
  613. self.node_type = "UTILITY"
  614. def bPrepare(self, bContext = None,):
  615. import bpy
  616. curve_name = self.evaluate_input("Curve")
  617. curve = bpy_object_get_guarded( curve_name, self)
  618. if not curve:
  619. raise RuntimeError(f"No curve found for {self}.")
  620. elif curve.type != "CURVE":
  621. raise GraphError(f"ERROR: Object {curve.name} is not a curve.")
  622. else:
  623. if bContext is None: bContext = bpy.context # is this wise?
  624. m = get_mesh_from_curve(curve.name,
  625. self.base_tree.execution_id,
  626. bContext, ribbon=False)
  627. # this is confusing but I am not re-writing these old functions
  628. from .utilities import FindNearestPointOnWireMesh as nearest_point
  629. spline_index = self.evaluate_input("Spline Index")
  630. ref_pt = self.evaluate_input("Reference Point")
  631. splines_points = [ [] for i in range (spline_index)]
  632. splines_points.append([ref_pt])
  633. pt = nearest_point(m, splines_points)[spline_index][0]
  634. self.parameters["Factor"] = pt
  635. self.prepared, self.executed = True, True
  636. class UtilityKDChoosePoint(MantisNode):
  637. def __init__(self, signature, base_tree):
  638. super().__init__(signature, base_tree)
  639. inputs = [
  640. "Reference Point" ,
  641. "Points" ,
  642. "Number to Find" ,
  643. ]
  644. outputs = [
  645. "Result Point" ,
  646. "Result Index" ,
  647. "Result Distance" ,
  648. ]
  649. self.inputs.init_sockets(inputs)
  650. self.outputs.init_sockets(outputs)
  651. self.init_parameters()
  652. self.node_type = "UTILITY"
  653. self.rerouted=[]
  654. def bPrepare(self, bContext = None,):
  655. from mathutils import Vector
  656. points= []
  657. ref_point = self.evaluate_input('Reference Point')
  658. num_points = self.evaluate_input('Number to Find')
  659. for i, l in enumerate(self.inputs['Points'].links):
  660. pt = self.evaluate_input('Points', i)
  661. points.append(pt)
  662. if not isinstance(pt, Vector):
  663. prRed(f"Cannot get point from {l.from_node} for {self}")
  664. assert ref_point is not None, wrapRed(f"Reference Point {ref_point} is invalid in node {self}")
  665. result = kd_find(self, points, ref_point, num_points)
  666. indices = [ found_point[1] for found_point in result ]
  667. distances = [ found_point[2] for found_point in result ]
  668. array_choose_relink(self, indices, "Points", "Result Point")
  669. array_choose_data(self, indices, "Result Index")
  670. array_choose_data(self, distances, "Result Distance")
  671. self.prepared, self.executed = True, True
  672. class UtilityKDChooseXForm(MantisNode):
  673. def __init__(self, signature, base_tree):
  674. super().__init__(signature, base_tree)
  675. inputs = [
  676. "Reference Point" ,
  677. "xForm Nodes" ,
  678. "Get Point Head/Tail" ,
  679. "Number to Find" ,
  680. ]
  681. outputs = [
  682. "Result xForm" ,
  683. "Result Index" ,
  684. "Result Distance" ,
  685. ]
  686. self.inputs.init_sockets(inputs)
  687. self.outputs.init_sockets(outputs)
  688. self.init_parameters()
  689. self.node_type = "UTILITY"
  690. self.rerouted=[]
  691. def bPrepare(self, bContext = None,):
  692. if len(self.hierarchy_dependencies)==0 and len(self.hierarchy_connections)==0 and \
  693. len(self.connections)==0 and len(self.dependencies)==0:
  694. self.prepared, self.executed = True, True
  695. return #Either it is already done or it doesn't matter.
  696. from mathutils import Vector
  697. points= []
  698. ref_point = self.evaluate_input('Reference Point')
  699. num_points = self.evaluate_input('Number to Find')
  700. for i, l in enumerate(self.inputs['xForm Nodes'].links):
  701. matrix = l.from_node.evaluate_input('Matrix')
  702. if matrix is None:
  703. raise GraphError(f"Cannot get point from {l.from_node} for {self}. Does it have a matrix?")
  704. pt = matrix.translation
  705. if head_tail := self.evaluate_input("Get Point Head/Tail"):
  706. # get the Y-axis of the basis, assume it is normalized
  707. y_axis = Vector((matrix[0][1],matrix[1][1], matrix[2][1]))
  708. pt = pt.lerp(pt+y_axis*matrix[3][3], head_tail)
  709. points.append(pt)
  710. if not isinstance(pt, Vector):
  711. prRed(f"Cannot get point from {l.from_node} for {self}")
  712. assert ref_point is not None, wrapRed(f"Reference Point {ref_point} is invalid in node {self}")
  713. result = kd_find(self, points, ref_point, num_points)
  714. indices = [ found_point[1] for found_point in result ]
  715. distances = [ found_point[2] for found_point in result ]
  716. array_choose_relink(self, indices, "xForm Nodes", "Result xForm")
  717. array_choose_data(self, indices, "Result Index")
  718. array_choose_data(self, distances, "Result Distance")
  719. self.prepared, self.executed = True, True
  720. class UtilityMetaRig(MantisNode):
  721. '''A node representing an armature object'''
  722. def __init__(self, signature, base_tree):
  723. super().__init__(signature, base_tree)
  724. inputs = [
  725. "Meta-Armature" ,
  726. "Meta-Bone" ,
  727. ]
  728. outputs = [
  729. "Matrix" ,
  730. ]
  731. self.inputs.init_sockets(inputs)
  732. self.outputs.init_sockets(outputs)
  733. self.init_parameters()
  734. self.node_type = "UTILITY"
  735. def bPrepare(self, bContext = None,):
  736. #kinda clumsy, whatever
  737. import bpy
  738. from mathutils import Matrix
  739. m = Matrix.Identity(4)
  740. meta_rig = self.evaluate_input("Meta-Armature")
  741. if meta_rig is None:
  742. raise RuntimeError("Invalid input for Meta-Armature.")
  743. meta_bone = self.evaluate_input("Meta-Bone")
  744. if meta_rig is None or meta_bone is None:
  745. raise RuntimeError("Invalid input for Meta-Bone.")
  746. if meta_rig:
  747. if ( armOb := bpy.data.objects.get(meta_rig) ):
  748. m = armOb.matrix_world
  749. if ( b := armOb.data.bones.get(meta_bone)):
  750. # calculate the correct object-space matrix
  751. m = Matrix.Identity(3)
  752. bones = [] # from the last ancestor, mult the matrices until we get to b
  753. while (b): bones.append(b); b = b.parent
  754. while (bones): b = bones.pop(); m = m @ b.matrix
  755. m = Matrix.Translation(b.head_local) @ m.to_4x4()
  756. #
  757. m[3][3] = b.length # this is where I arbitrarily decided to store length
  758. # else:
  759. # prRed("no bone for MetaRig node ", self)
  760. else:
  761. raise RuntimeError(wrapRed(f"No meta-rig input for MetaRig node {self}"))
  762. self.parameters["Matrix"] = m
  763. self.prepared = True
  764. self.executed = True
  765. class UtilityBoneProperties(SimpleInputNode):
  766. '''A node representing a bone's gettable properties'''
  767. def __init__(self, signature, base_tree):
  768. super().__init__(signature, base_tree)
  769. outputs = [
  770. "matrix" ,
  771. "matrix_local" ,
  772. "matrix_basis" ,
  773. "head" ,
  774. "tail" ,
  775. "length" ,
  776. "rotation" ,
  777. "location" ,
  778. "scale" ,
  779. ]
  780. self.outputs.init_sockets(outputs)
  781. self.init_parameters()
  782. def fill_parameters(self, ui_node=None):
  783. return
  784. # TODO this should probably be moved to Links
  785. class UtilityDriverVariable(MantisNode):
  786. '''A node representing an armature object'''
  787. def __init__(self, signature, base_tree):
  788. super().__init__(signature, base_tree)
  789. inputs = [
  790. "Variable Type" ,
  791. "Property" ,
  792. "Property Index" ,
  793. "Evaluation Space",
  794. "Rotation Mode" ,
  795. "xForm 1" ,
  796. "xForm 2" ,
  797. ]
  798. outputs = [
  799. "Driver Variable",
  800. ]
  801. self.inputs.init_sockets(inputs)
  802. self.outputs.init_sockets(outputs)
  803. self.init_parameters()
  804. self.node_type = "DRIVER" # MUST be run in Pose mode
  805. self.prepared = True
  806. def reset_execution(self):
  807. super().reset_execution()
  808. # clear this to ensure there are no stale reference pointers
  809. self.parameters["Driver Variable"] = None
  810. self.prepared=True
  811. def evaluate_input(self, input_name):
  812. if input_name == 'Property':
  813. if self.inputs.get('Property'):
  814. if self.inputs['Property'].is_linked:
  815. trace = trace_single_line(self, input_name)
  816. # CANNOT UNDERSTATE HOW CRITICAL THIS CHECK IS
  817. if trace[0][-1].node_type == 'XFORM':
  818. return trace[1].name # the name of the socket
  819. return self.parameters["Property"]
  820. return super().evaluate_input(input_name)
  821. def GetxForm(self, index=1):
  822. trace = trace_single_line(self, "xForm 1" if index == 1 else "xForm 2")
  823. for node in trace[0]:
  824. if (node.__class__ in [xFormArmature, xFormBone]):
  825. return node #this will fetch the first one, that's good!
  826. return None
  827. def bRelationshipPass(self, bContext = None,):
  828. prepare_parameters(self)
  829. #prPurple ("Executing Driver Variable Node")
  830. xF1 = self.GetxForm()
  831. xF2 = self.GetxForm(index=2)
  832. # kinda clumsy
  833. xForm1, xForm2 = None, None
  834. if xF1 : xForm1 = xF1.bGetObject()
  835. if xF2 : xForm2 = xF2.bGetObject()
  836. v_type = self.evaluate_input("Variable Type")
  837. i = self.evaluate_input("Property Index"); dVarChannel = ""
  838. if not isinstance(i, (int, float)):
  839. raise RuntimeError(f" {self} has invalid input for \"Property Index\".")
  840. if (i >= 0): #negative values will use the vector property.
  841. if self.evaluate_input("Property") == 'location':
  842. if i == 0: dVarChannel = "LOC_X"
  843. elif i == 1: dVarChannel = "LOC_Y"
  844. elif i == 2: dVarChannel = "LOC_Z"
  845. else: raise RuntimeError("Invalid property index for %s" % self)
  846. if self.evaluate_input("Property") == 'rotation':
  847. if i == 0: dVarChannel = "ROT_X"
  848. elif i == 1: dVarChannel = "ROT_Y"
  849. elif i == 2: dVarChannel = "ROT_Z"
  850. elif i == 3: dVarChannel = "ROT_W"
  851. else: raise RuntimeError("Invalid property index for %s" % self)
  852. if self.evaluate_input("Property") == 'scale':
  853. if i == 0: dVarChannel = "SCALE_X"
  854. elif i == 1: dVarChannel = "SCALE_Y"
  855. elif i == 2: dVarChannel = "SCALE_Z"
  856. elif i == 3: dVarChannel = "SCALE_AVG"
  857. else: raise RuntimeError("Invalid property index for %s" % self)
  858. if self.evaluate_input("Property") == 'scale_average':
  859. dVarChannel = "SCALE_AVG"
  860. if dVarChannel: v_type = "TRANSFORMS"
  861. my_var = {
  862. "owner" : xForm1, # will be filled in by Driver
  863. "prop" : self.evaluate_input("Property"), # will be filled in by Driver
  864. "type" : v_type,
  865. "space" : self.evaluate_input("Evaluation Space"),
  866. "rotation_mode" : self.evaluate_input("Rotation Mode"),
  867. "xForm 1" : xForm1,#self.GetxForm(index = 1),
  868. "xForm 2" : xForm2,#self.GetxForm(index = 2),
  869. "channel" : dVarChannel,}
  870. self.parameters["Driver Variable"] = my_var
  871. print (my_var['prop'])
  872. self.executed = True
  873. class UtilityKeyframe(MantisNode):
  874. '''A node representing a keyframe for a F-Curve'''
  875. def __init__(self, signature, base_tree):
  876. super().__init__(signature, base_tree)
  877. inputs = [
  878. "Frame" ,
  879. "Value" ,
  880. ]
  881. outputs = [
  882. "Keyframe" ,
  883. ]
  884. additional_parameters = {"Keyframe":{}}
  885. self.inputs.init_sockets(inputs)
  886. self.outputs.init_sockets(outputs)
  887. self.init_parameters( additional_parameters=additional_parameters)
  888. self.node_type = "DRIVER" # MUST be run in Pose mode
  889. setup_custom_property_inputs_outputs(self)
  890. def bPrepare(self, bContext = None,):
  891. key = self.parameters["Keyframe"]
  892. from mathutils import Vector
  893. key["co"]= Vector( (self.evaluate_input("Frame"), self.evaluate_input("Value"),))
  894. key["type"]="GENERATED"
  895. key["interpolation"] = "LINEAR"
  896. # eventually this will have the right data, TODO
  897. # self.parameters["Keyframe"] = key
  898. self.prepared = True
  899. self.executed = True
  900. class UtilityFCurve(MantisNode):
  901. '''A node representing an armature object'''
  902. def __init__(self, signature, base_tree):
  903. super().__init__(signature, base_tree)
  904. inputs = [
  905. "Extrapolation Mode",
  906. ]
  907. outputs = [
  908. "fCurve",
  909. ]
  910. self.inputs.init_sockets(inputs)
  911. self.outputs.init_sockets(outputs)
  912. self.init_parameters()
  913. self.node_type = "UTILITY"
  914. setup_custom_property_inputs_outputs(self)
  915. self.prepared = True
  916. def reset_execution(self):
  917. super().reset_execution()
  918. self.prepared=True
  919. def evaluate_input(self, input_name):
  920. return super().evaluate_input(input_name)
  921. def bTransformPass(self, bContext = None,):
  922. prepare_parameters(self)
  923. extrap_mode = self.evaluate_input("Extrapolation Mode")
  924. keys = [] # ugly but whatever
  925. #['amplitude', 'back', 'bl_rna', 'co', 'co_ui', 'easing', 'handle_left', 'handle_left_type', 'handle_right', 'handle_right_type',
  926. # 'interpolation', 'period', 'rna_type', 'select_control_point', 'select_left_handle', 'select_right_handle', 'type']
  927. for k in self.inputs.keys():
  928. if k == 'Extrapolation Mode' : continue
  929. # print (self.inputs[k])
  930. if (key := self.evaluate_input(k)) is None:
  931. prOrange(f"WARN: No keyframe connected to {self}:{k}. Skipping Link.")
  932. else:
  933. keys.append(key)
  934. if len(keys) <1:
  935. prOrange(f"WARN: no keys in fCurve {self}.")
  936. keys.append(extrap_mode)
  937. self.parameters["fCurve"] = keys
  938. self.executed = True
  939. #TODO make the fCurve data a data class instead of a dict
  940. class UtilityDriver(MantisNode):
  941. '''A node representing an armature object'''
  942. def __init__(self, signature, base_tree):
  943. super().__init__(signature, base_tree)
  944. inputs = [
  945. "Driver Type" ,
  946. "Expression" ,
  947. "fCurve" ,
  948. ]
  949. outputs = [
  950. "Driver",
  951. ]
  952. from .drivers import MantisDriver
  953. additional_parameters = {
  954. "Driver":MantisDriver(),
  955. }
  956. self.inputs.init_sockets(inputs)
  957. self.outputs.init_sockets(outputs)
  958. self.init_parameters(additional_parameters=additional_parameters)
  959. self.node_type = "DRIVER" # MUST be run in Pose mode
  960. setup_custom_property_inputs_outputs(self)
  961. self.prepared = True
  962. def reset_execution(self):
  963. super().reset_execution()
  964. from .drivers import MantisDriver
  965. self.parameters["Driver"]=MantisDriver()
  966. self.prepared=True
  967. def bRelationshipPass(self, bContext = None,):
  968. prepare_parameters(self)
  969. from .drivers import MantisDriver
  970. #prPurple("Executing Driver Node")
  971. my_vars = []
  972. keys = self.evaluate_input("fCurve")
  973. if keys is None or len(keys) <2:
  974. prWhite(f"INFO: no fCurve connected to {self}; using default fCurve.")
  975. from mathutils import Vector
  976. keys = [
  977. {"co":Vector( (0, 0,)), "type":"GENERATED", "interpolation":"LINEAR" },
  978. {"co":Vector( (1, 1,)), "type":"GENERATED", "interpolation":"LINEAR" },
  979. "CONSTANT",]
  980. for inp in list(self.inputs.keys() )[3:]:
  981. if (new_var := self.evaluate_input(inp)):
  982. new_var["name"] = inp
  983. my_vars.append(new_var)
  984. else:
  985. raise RuntimeError(f"Failed to initialize Driver variable for {self}")
  986. my_driver ={ "owner" : None,
  987. "prop" : None, # will be filled out in the node that uses the driver
  988. "expression" : self.evaluate_input("Expression"),
  989. "ind" : -1, # same here
  990. "type" : self.evaluate_input("Driver Type"),
  991. "vars" : my_vars,
  992. "keys" : keys[:-1],
  993. "extrapolation" : keys[-1] }
  994. my_driver = MantisDriver(my_driver)
  995. self.parameters["Driver"].update(my_driver)
  996. print("Initializing driver %s " % (wrapPurple(self.__repr__())) )
  997. self.executed = True
  998. class UtilitySwitch(MantisNode):
  999. '''A node representing an armature object'''
  1000. def __init__(self, signature, base_tree):
  1001. super().__init__(signature, base_tree)
  1002. inputs = {
  1003. "Parameter" ,
  1004. "Parameter Index" ,
  1005. "Invert Switch" ,
  1006. }
  1007. outputs = [
  1008. "Driver",
  1009. ]
  1010. from .drivers import MantisDriver
  1011. additional_parameters = {
  1012. "Driver":MantisDriver(),
  1013. }
  1014. self.inputs.init_sockets(inputs)
  1015. self.outputs.init_sockets(outputs)
  1016. self.init_parameters(additional_parameters=additional_parameters)
  1017. self.node_type = "DRIVER" # MUST be run in Pose mode
  1018. self.prepared = True
  1019. def evaluate_input(self, input_name):
  1020. if input_name == 'Parameter':
  1021. if self.inputs['Parameter'].is_connected:
  1022. trace = trace_single_line(self, input_name)
  1023. return trace[1].name # the name of the socket
  1024. return self.parameters["Parameter"]
  1025. return super().evaluate_input(input_name)
  1026. def GetxForm(self,):
  1027. trace = trace_single_line(self, "Parameter" )
  1028. for node in trace[0]:
  1029. if (node.__class__ in [xFormArmature, xFormBone]):
  1030. return node #this will fetch the first one, that's good!
  1031. return None
  1032. def reset_execution(self):
  1033. super().reset_execution()
  1034. from .drivers import MantisDriver
  1035. self.parameters["Driver"]=MantisDriver()
  1036. self.prepared=True
  1037. def bRelationshipPass(self, bContext = None,):
  1038. #prepare_parameters(self)
  1039. #prPurple ("Executing Switch Node")
  1040. xForm = self.GetxForm()
  1041. if xForm : xForm = xForm.bGetObject()
  1042. if not xForm:
  1043. raise RuntimeError("Could not evaluate xForm for %s" % self)
  1044. from .drivers import MantisDriver
  1045. my_driver ={ "owner" : None,
  1046. "prop" : None, # will be filled out in the node that uses the driver
  1047. "ind" : -1, # same here
  1048. "type" : "SCRIPTED",
  1049. "vars" : [ { "owner" : xForm,
  1050. "prop" : self.evaluate_input("Parameter"),
  1051. "name" : "a",
  1052. "type" : "SINGLE_PROP", } ],
  1053. "keys" : [ { "co":(0,0),
  1054. "interpolation": "LINEAR",
  1055. "type":"KEYFRAME",}, #display type
  1056. { "co":(1,1),
  1057. "interpolation": "LINEAR",
  1058. "type":"KEYFRAME",},],
  1059. "extrapolation": 'CONSTANT', }
  1060. my_driver ["expression"] = "a"
  1061. my_driver = MantisDriver(my_driver)
  1062. # this makes it so I can check for type later!
  1063. if self.evaluate_input("Invert Switch") == True:
  1064. my_driver ["expression"] = "1 - a"
  1065. # this way, regardless of what order things are handled, the
  1066. # driver is sent to the next node.
  1067. # In the case of some drivers, the parameter may be sent out
  1068. # before it's filled in (because there is a circular dependency)
  1069. # I want to support this behaviour because Blender supports it.
  1070. # We do not make a copy. We update the driver, so that
  1071. # the same instance is filled out.
  1072. self.parameters["Driver"].update(my_driver)
  1073. print("Initializing driver %s " % (wrapPurple(self.__repr__())) )
  1074. self.executed = True
  1075. class UtilityCombineThreeBool(MantisNode):
  1076. '''A node for combining three booleans into a boolean three-tuple'''
  1077. def __init__(self, signature, base_tree):
  1078. super().__init__(signature, base_tree)
  1079. inputs = [
  1080. "X" ,
  1081. "Y" ,
  1082. "Z" ,
  1083. ]
  1084. outputs = [
  1085. "Three-Bool",
  1086. ]
  1087. self.inputs.init_sockets(inputs)
  1088. self.outputs.init_sockets(outputs)
  1089. self.init_parameters()
  1090. self.node_type = "UTILITY"
  1091. def reset_execution(self): # need to make sure any references are deleted
  1092. super().reset_execution() # so we prepare the node again to reset them
  1093. if self.parameters["Three-Bool"] is not None:
  1094. for param in self.parameters["Three-Bool"]:
  1095. if isinstance(param, dict):
  1096. self.prepared=False; break
  1097. def bPrepare(self, bContext = None,):
  1098. self.parameters["Three-Bool"] = (
  1099. self.evaluate_input("X"),
  1100. self.evaluate_input("Y"),
  1101. self.evaluate_input("Z"), )
  1102. self.prepared = True
  1103. self.executed = True
  1104. # Note this is a copy of the above. This needs to be de-duplicated.
  1105. class UtilityCombineVector(MantisNode):
  1106. '''A node for combining three floats into a vector'''
  1107. def __init__(self, signature, base_tree):
  1108. super().__init__(signature, base_tree)
  1109. super().__init__(signature, base_tree)
  1110. inputs = [
  1111. "X" ,
  1112. "Y" ,
  1113. "Z" ,
  1114. ]
  1115. outputs = [
  1116. "Vector",
  1117. ]
  1118. self.inputs.init_sockets(inputs)
  1119. self.outputs.init_sockets(outputs)
  1120. self.init_parameters()
  1121. self.node_type = "UTILITY"
  1122. def reset_execution(self): # need to make sure any references are deleted
  1123. super().reset_execution() # so we prepare the node again to reset them
  1124. if self.parameters["Vector"] is not None:
  1125. for param in self.parameters["Vector"]:
  1126. if isinstance(param, dict):
  1127. self.prepared=False; break
  1128. def bPrepare(self, bContext = None,):
  1129. #prPurple("Executing CombineVector Node")
  1130. prepare_parameters(self)
  1131. self.parameters["Vector"] = (
  1132. self.evaluate_input("X"),
  1133. self.evaluate_input("Y"),
  1134. self.evaluate_input("Z"), )
  1135. self.prepared, self.executed = True, True
  1136. class UtilitySeparateVector(MantisNode):
  1137. '''A node for separating a vector into three floats'''
  1138. def __init__(self, signature, base_tree):
  1139. super().__init__(signature, base_tree)
  1140. inputs = [
  1141. "Vector"
  1142. ]
  1143. outputs = [
  1144. "X" ,
  1145. "Y" ,
  1146. "Z" ,
  1147. ]
  1148. self.inputs.init_sockets(inputs)
  1149. self.outputs.init_sockets(outputs)
  1150. self.init_parameters()
  1151. self.node_type = "UTILITY"
  1152. def bPrepare(self, bContext = None,):
  1153. self.parameters["X"] = self.evaluate_input("Vector")[0]
  1154. self.parameters["Y"] = self.evaluate_input("Vector")[1]
  1155. self.parameters["Z"] = self.evaluate_input("Vector")[2]
  1156. self.prepared, self.executed = True, True
  1157. class UtilityCatStrings(MantisNode):
  1158. '''A node representing an armature object'''
  1159. def __init__(self, signature, base_tree):
  1160. super().__init__(signature, base_tree)
  1161. inputs = [
  1162. "String_1" ,
  1163. "String_2" ,
  1164. ]
  1165. outputs = [
  1166. "OutputString" ,
  1167. ]
  1168. self.inputs.init_sockets(inputs)
  1169. self.outputs.init_sockets(outputs)
  1170. self.init_parameters()
  1171. self.node_type = "UTILITY"
  1172. def bPrepare(self, bContext = None,):
  1173. self.parameters["OutputString"] = self.evaluate_input("String_1")+self.evaluate_input("String_2")
  1174. self.prepared, self.executed = True, True
  1175. # TODO move this to the Xform file
  1176. class InputWidget(MantisNode):
  1177. '''A node representing an existing object'''
  1178. def __init__(self, signature, base_tree):
  1179. super().__init__(signature, base_tree, InputWidgetSockets)
  1180. self.init_parameters()
  1181. self.node_type = "XFORM"
  1182. def reset_execution(self):
  1183. super().reset_execution()
  1184. self.prepared=False
  1185. def bPrepare(self, bContext=None):
  1186. print(wrapGreen("Executing ")+wrapOrange("InputWidget Node ")+wrapWhite(f"{self}"))
  1187. path = self.evaluate_input('Name')
  1188. axes_flipped = self.evaluate_input('Flip Axes')
  1189. scaling = self.evaluate_input('Scale')
  1190. add_scale_modifier = False
  1191. if scaling[0] != 1.0 or scaling[1] != 1.0 or scaling[2] != 1.0:
  1192. add_scale_modifier = True
  1193. do_mirror = True
  1194. from os import path as os_path
  1195. from .preferences import get_bl_addon_object
  1196. bl_mantis_addon = get_bl_addon_object()
  1197. widgets_path = bl_mantis_addon.preferences.WidgetsLibraryFolder
  1198. path = widgets_path+path # this guards the widgets root so the end-user
  1199. # can easily change the widgets directory without breaking things
  1200. from .utilities import get_default_collection
  1201. collection = get_default_collection(collection_type='WIDGET')
  1202. file_name = os_path.split(path)[-1]
  1203. obj_name = os_path.splitext(file_name)[0]
  1204. obj_name_full = obj_name
  1205. if add_scale_modifier:
  1206. obj_name_full+="_scaled_"+".".join(self.ui_signature[1:])
  1207. if any(axes_flipped):
  1208. obj_name_full+="_flipped_"
  1209. for i, axis in enumerate("XYZ"):
  1210. if axes_flipped[i]: obj_name_full+=axis
  1211. from bpy import data
  1212. # what is this code doing? I thought I already linked it... TODO find out
  1213. if obj_name in data.objects.keys() and not \
  1214. obj_name_full in data.objects.keys():
  1215. self.bObject = data.objects.get(obj_name).copy()
  1216. self.bObject.name = obj_name_full
  1217. collection.objects.link(self.bObject)
  1218. # now check to see if it exists
  1219. elif obj_name_full in data.objects.keys():
  1220. prWhite(f"INFO: {obj_name_full} is already in this .blend file; skipping import.")
  1221. self.bObject = data.objects.get(obj_name_full)
  1222. if any(axes_flipped): # check if we need to add a Flip modifier
  1223. if len(self.bObject.modifiers) > 1 and self.bObject.modifiers[-1].name == "Simple Flip":
  1224. do_mirror=False
  1225. else:
  1226. from .utilities import import_object_from_file
  1227. self.bObject = import_object_from_file(path)
  1228. if any(axes_flipped) or add_scale_modifier:
  1229. self.bObject = self.bObject.copy()
  1230. self.bObject.name = obj_name_full
  1231. collection.objects.link(self.bObject)
  1232. # do the scaling...
  1233. if add_scale_modifier:
  1234. if (scale_modifier := self.bObject.modifiers.get("Scale Object Data")) is None:
  1235. scale_modifier = self.bObject.modifiers.new("Scale Object Data", type='NODES')
  1236. ng = data.node_groups.get("Scale Object Data")
  1237. if ng is None:
  1238. from .geometry_node_graphgen import gen_scale_object_data_modifier
  1239. ng = gen_scale_object_data_modifier()
  1240. scale_modifier.node_group = ng
  1241. scale_modifier['Socket_2']=scaling
  1242. # now we'll check for the mirrors.
  1243. if any(axes_flipped) and do_mirror:
  1244. if (flip_modifier := self.bObject.modifiers.get("Simple Flip")) is None:
  1245. flip_modifier = self.bObject.modifiers.new("Simple Flip", type="NODES")
  1246. ng = data.node_groups.get("Simple Flip")
  1247. if ng is None:
  1248. from .geometry_node_graphgen import gen_simple_flip_modifier
  1249. ng = gen_simple_flip_modifier()
  1250. flip_modifier.node_group = ng
  1251. flip_modifier["Socket_2"]=axes_flipped[0]
  1252. flip_modifier["Socket_3"]=axes_flipped[1]
  1253. flip_modifier["Socket_4"]=axes_flipped[2]
  1254. self.prepared, self.executed = True, True
  1255. def bGetObject(self, mode=''):
  1256. return self.bObject
  1257. # TODO move this to the Xform file
  1258. class InputExistingGeometryObject(MantisNode):
  1259. '''A node representing an existing object'''
  1260. def __init__(self, signature, base_tree):
  1261. super().__init__(signature, base_tree)
  1262. inputs = [
  1263. "Name" ,
  1264. ]
  1265. outputs = [
  1266. "Object" ,
  1267. ]
  1268. self.inputs.init_sockets(inputs)
  1269. self.outputs.init_sockets(outputs)
  1270. self.init_parameters()
  1271. self.node_type = "XFORM"
  1272. def reset_execution(self):
  1273. super().reset_execution()
  1274. self.prepared=False
  1275. def bPrepare(self, bContext=None):
  1276. from bpy import data
  1277. ob = None
  1278. if name := self.evaluate_input("Name"):
  1279. ob= data.objects.get( name )
  1280. if ob is None and name:
  1281. prRed(f"No object found with name {name} in {self}")
  1282. self.bObject=ob
  1283. self.prepared, self.executed = True, True
  1284. def bGetObject(self, mode=''):
  1285. return self.bObject
  1286. class InputExistingGeometryData(MantisNode):
  1287. '''A node representing existing object data'''
  1288. def __init__(self, signature, base_tree):
  1289. super().__init__(signature, base_tree)
  1290. inputs = [
  1291. "Name" ,
  1292. ]
  1293. outputs = [
  1294. "Geometry" ,
  1295. ]
  1296. self.inputs.init_sockets(inputs)
  1297. self.outputs.init_sockets(outputs)
  1298. self.init_parameters()
  1299. self.node_type = "UTILITY"
  1300. self.prepared = True; self.executed = True
  1301. def reset_execution(self):
  1302. super().reset_execution()
  1303. self.prepared, self.executed = True, True
  1304. # the mode argument is only for interface consistency
  1305. def bGetObject(self, mode=''):
  1306. from bpy import data
  1307. # first try Curve, then try Mesh
  1308. bObject = data.curves.get(self.evaluate_input("Name"))
  1309. if not bObject:
  1310. bObject = data.meshes.get(self.evaluate_input("Name"))
  1311. if bObject is None:
  1312. raise RuntimeError(f"Could not find a mesh or curve datablock named \"{self.evaluate_input('Name')}\" for node {self}")
  1313. return bObject
  1314. class UtilityDeclareCollections(MantisNode):
  1315. '''A node to help manage bone collections'''
  1316. def __init__(self, signature, base_tree):
  1317. super().__init__(signature, base_tree)
  1318. self.node_type = "UTILITY"
  1319. self.prepared, self.executed = True, True
  1320. def reset_execution(self):
  1321. super().reset_execution()
  1322. self.prepared, self.executed = True, True
  1323. def fill_parameters(self, ui_node=None):
  1324. if ui_node is None:
  1325. from .utilities import get_ui_node
  1326. ui_node = get_ui_node(self.ui_signature, self.base_tree)
  1327. templates=[]
  1328. for out in ui_node.outputs:
  1329. if not (out.name in self.outputs.keys()) :
  1330. templates.append(SockTemplate(name=out.name,
  1331. identifier=out.identifier, is_input=False,))
  1332. self.outputs.init_sockets(templates)
  1333. # now we have our parameters, fill them. This is a little inefficient I guess.
  1334. for out in ui_node.outputs:
  1335. self.parameters[out.name] = out.default_value
  1336. class UtilityCollectionJoin(MantisNode):
  1337. '''A node to help manage bone collections'''
  1338. def __init__(self, signature, base_tree):
  1339. super().__init__(signature, base_tree, CollectionJoinSockets)
  1340. self.init_parameters()
  1341. self.node_type = "UTILITY"
  1342. self.prepared, self.executed = False, False
  1343. def reset_execution(self):
  1344. super().reset_execution()
  1345. self.prepared, self.executed = False, False
  1346. def bPrepare(self, bContext = None,):
  1347. if self.inputs['Collections'].links:
  1348. bCol_groups = []
  1349. for i, l in enumerate(self.inputs['Collections'].links):
  1350. bCol_group = self.evaluate_input("Collections", index=i)
  1351. if not isinstance(bCol_group, str):
  1352. bCol_group = str(bCol_group)
  1353. prOrange(f"Warning: coercing invalid input ({i}) to String in node: {self}")
  1354. bCol_groups.append(bCol_group)
  1355. bCols = '|'.join(bCol_groups)
  1356. else:
  1357. bCols = self.evaluate_input("Collections")
  1358. if not isinstance(bCols, str):
  1359. bCols = str(bCols)
  1360. prOrange(f"Warning: coercing invalid input to String in node: {self}")
  1361. self.parameters['Collection']=bCols
  1362. self.prepared, self.executed = True, True
  1363. class UtilityCollectionHierarchy(MantisNode):
  1364. '''A node to help manage bone collections'''
  1365. def __init__(self, signature, base_tree):
  1366. super().__init__(signature, base_tree, CollectionHierarchySockets)
  1367. self.init_parameters()
  1368. self.node_type = "UTILITY"
  1369. self.prepared, self.executed = False, False
  1370. def reset_execution(self):
  1371. super().reset_execution()
  1372. self.prepared, self.executed = False, False
  1373. def bPrepare(self, bContext = None,):
  1374. parent_col = self.evaluate_input('Parent Collection')
  1375. if not isinstance(parent_col, str):
  1376. parent_col = str(parent_col)
  1377. prOrange(f"Warning: coercing invalid Parent Collection to String in node: {self}")
  1378. child_col = self.evaluate_input('Child Collection')
  1379. if not isinstance(child_col, str):
  1380. child_col = str(child_col)
  1381. prOrange(f"Warning: coercing invalid Child Collection to String in node: {self}")
  1382. result = parent_col +">"+child_col
  1383. self.parameters['Collection']=result
  1384. self.prepared, self.executed = True, True
  1385. class UtilityGeometryOfXForm(MantisNode):
  1386. '''A node representing existing object data'''
  1387. def __init__(self, signature, base_tree):
  1388. super().__init__(signature, base_tree)
  1389. inputs = [
  1390. "xForm" ,
  1391. ]
  1392. outputs = [
  1393. "Geometry" ,
  1394. ]
  1395. self.inputs.init_sockets(inputs)
  1396. self.outputs.init_sockets(outputs)
  1397. self.init_parameters()
  1398. self.node_type = "UTILITY"
  1399. self.prepared = True
  1400. self.executed = True
  1401. def reset_execution(self):
  1402. super().reset_execution()
  1403. self.prepared, self.executed = True, True
  1404. # mode for interface consistency
  1405. def bGetObject(self, mode=''):
  1406. if not (self.inputs.get('xForm') and self.inputs['xForm'].links):
  1407. prOrange(f"WARN: Cannot retrieve data from {self}, there is no xForm node connected.")
  1408. return None
  1409. xf = self.inputs["xForm"].links[0].from_node
  1410. if xf.node_type == 'XFORM':
  1411. xf_ob = xf.bGetObject()
  1412. if (xf_ob is not None) and xf_ob.type in ['MESH', 'CURVE']:
  1413. return xf_ob.data
  1414. prOrange(f"WARN: Cannot retrieve data from {self}, the connected xForm is not a mesh or curve.")
  1415. return None
  1416. class UtilityNameOfXForm(MantisNode):
  1417. '''A node representing existing object data'''
  1418. def __init__(self, signature, base_tree):
  1419. super().__init__(signature, base_tree)
  1420. inputs = [
  1421. "xForm" ,
  1422. ]
  1423. outputs = [
  1424. "Name" ,
  1425. ]
  1426. self.inputs.init_sockets(inputs)
  1427. self.outputs.init_sockets(outputs)
  1428. self.init_parameters()
  1429. self.node_type = "UTILITY"
  1430. # mode for interface consistency
  1431. def bPrepare(self, bContext = None,):
  1432. if not (self.inputs.get('xForm') and self.inputs['xForm'].links):
  1433. raise RuntimeError( f"WARN: Cannot retrieve data from {self},"
  1434. " there is no xForm node connected.")
  1435. xf = self.inputs["xForm"].links[0].from_node
  1436. self.parameters["Name"] = xf.evaluate_input('Name')
  1437. self.prepared, self.executed = True, True
  1438. class UtilityGetBoneLength(MantisNode):
  1439. '''A node to get the length of a bone matrix'''
  1440. def __init__(self, signature, base_tree):
  1441. super().__init__(signature, base_tree)
  1442. inputs = [
  1443. "Bone Matrix" ,
  1444. ]
  1445. outputs = [
  1446. "Bone Length" ,
  1447. ]
  1448. self.inputs.init_sockets(inputs)
  1449. self.outputs.init_sockets(outputs)
  1450. self.init_parameters()
  1451. self.node_type = "UTILITY"
  1452. def bPrepare(self, bContext = None,):
  1453. if (l := self.evaluate_input("Bone Matrix")) is not None:
  1454. self.parameters["Bone Length"] = l[3][3]
  1455. else:
  1456. other = self.inputs["Bone Matrix"].links[0].from_node
  1457. raise RuntimeError(f"Cannot get matrix for {self} from {other}")
  1458. self.prepared, self.executed = True, True
  1459. class UtilityPointFromBoneMatrix(MantisNode):
  1460. '''A node representing an armature object'''
  1461. def __init__(self, signature, base_tree):
  1462. super().__init__(signature, base_tree)
  1463. inputs = [
  1464. "Bone Matrix" ,
  1465. "Head/Tail" ,
  1466. ]
  1467. outputs = [
  1468. "Point" ,
  1469. ]
  1470. self.inputs.init_sockets(inputs)
  1471. self.outputs.init_sockets(outputs)
  1472. self.init_parameters()
  1473. self.node_type = "UTILITY"
  1474. # TODO: find out why this is sometimes not ready at bPrepare phase
  1475. def bPrepare(self, bContext = None,):
  1476. from mathutils import Vector
  1477. matrix = self.evaluate_input("Bone Matrix")
  1478. head, rotation, _scale = matrix.copy().decompose()
  1479. tail = head.copy() + (rotation @ Vector((0,1,0)))*matrix[3][3]
  1480. self.parameters["Point"] = head.lerp(tail, self.evaluate_input("Head/Tail"))
  1481. self.prepared, self.executed = True, True
  1482. class UtilitySetBoneLength(MantisNode):
  1483. '''Sets the length of a Bone's matrix'''
  1484. def __init__(self, signature, base_tree):
  1485. super().__init__(signature, base_tree)
  1486. inputs = [
  1487. "Bone Matrix" ,
  1488. "Length" ,
  1489. ]
  1490. outputs = [
  1491. "Bone Matrix" ,
  1492. ]
  1493. self.inputs.init_sockets(inputs)
  1494. self.outputs.init_sockets(outputs)
  1495. self.init_parameters()
  1496. self.node_type = "UTILITY"
  1497. def bPrepare(self, bContext = None,):
  1498. from mathutils import Vector
  1499. if matrix := self.evaluate_input("Bone Matrix"):
  1500. matrix = matrix.copy()
  1501. # print (self.inputs["Length"].links)
  1502. matrix[3][3] = self.evaluate_input("Length")
  1503. self.parameters["Length"] = self.evaluate_input("Length")
  1504. self.parameters["Bone Matrix"] = matrix
  1505. else:
  1506. raise RuntimeError(f"Cannot get matrix for {self}")
  1507. self.prepared, self.executed = True, True
  1508. class UtilityMatrixSetLocation(MantisNode):
  1509. '''Sets the location of a matrix'''
  1510. def __init__(self, signature, base_tree):
  1511. super().__init__(signature, base_tree)
  1512. inputs = [
  1513. "Matrix" ,
  1514. "Location" ,
  1515. ]
  1516. outputs = [
  1517. "Matrix" ,
  1518. ]
  1519. self.inputs.init_sockets(inputs)
  1520. self.outputs.init_sockets(outputs)
  1521. self.init_parameters()
  1522. self.node_type = "UTILITY"
  1523. def bPrepare(self, bContext = None,):
  1524. from mathutils import Vector
  1525. if matrix := self.evaluate_input("Matrix"):
  1526. matrix = matrix.copy()
  1527. # print (self.inputs["Length"].links)
  1528. loc = self.evaluate_input("Location")
  1529. matrix[0][3] = loc[0]; matrix[1][3] = loc[1]; matrix[2][3] = loc[2]
  1530. self.parameters["Matrix"] = matrix
  1531. self.prepared, self.executed = True, True
  1532. class UtilityMatrixGetLocation(MantisNode):
  1533. '''Gets the location of a matrix'''
  1534. def __init__(self, signature, base_tree):
  1535. super().__init__(signature, base_tree)
  1536. inputs = [
  1537. "Matrix" ,
  1538. ]
  1539. outputs = [
  1540. "Location" ,
  1541. ]
  1542. self.inputs.init_sockets(inputs)
  1543. self.outputs.init_sockets(outputs)
  1544. self.init_parameters()
  1545. self.node_type = "UTILITY"
  1546. def bPrepare(self, bContext = None,):
  1547. from mathutils import Vector
  1548. if matrix := self.evaluate_input("Matrix"):
  1549. self.parameters["Location"] = matrix.to_translation()
  1550. self.prepared = True; self.executed = True
  1551. class UtilityMatrixFromXForm(MantisNode):
  1552. """Returns the matrix of the given xForm node."""
  1553. def __init__(self, signature, base_tree):
  1554. super().__init__(signature, base_tree)
  1555. inputs = [
  1556. "xForm" ,
  1557. ]
  1558. outputs = [
  1559. "Matrix" ,
  1560. ]
  1561. self.node_type = "UTILITY"
  1562. self.inputs.init_sockets(inputs)
  1563. self.outputs.init_sockets(outputs)
  1564. self.init_parameters()
  1565. def GetxForm(self):
  1566. trace = trace_single_line(self, "xForm")
  1567. for node in trace[0]:
  1568. if (node.node_type == 'XFORM'):
  1569. return node
  1570. raise GraphError("%s is not connected to an xForm" % self)
  1571. def bPrepare(self, bContext = None,):
  1572. from mathutils import Vector, Matrix
  1573. self.parameters["Matrix"] = Matrix.Identity(4)
  1574. if matrix := self.GetxForm().parameters.get("Matrix"):
  1575. self.parameters["Matrix"] = matrix.copy()
  1576. elif hasattr(self.GetxForm().bObject, "matrix"):
  1577. self.parameters["Matrix"] = self.GetxForm().bObject.matrix.copy()
  1578. elif hasattr(self.GetxForm().bObject, "matrix_world"):
  1579. self.parameters["Matrix"] = self.GetxForm().bObject.matrix_world.copy()
  1580. else:
  1581. prRed(f"Could not find matrix for {self} - check if the referenced object exists.")
  1582. self.prepared = True; self.executed = True
  1583. class UtilityAxesFromMatrix(MantisNode):
  1584. """Returns the axes of the given matrix."""
  1585. def __init__(self, signature, base_tree):
  1586. super().__init__(signature, base_tree)
  1587. inputs = [
  1588. "Matrix" ,
  1589. ]
  1590. outputs = [
  1591. "X Axis" ,
  1592. "Y Axis" ,
  1593. "Z Axis" ,
  1594. ]
  1595. self.inputs.init_sockets(inputs)
  1596. self.outputs.init_sockets(outputs)
  1597. self.init_parameters()
  1598. self.node_type = "UTILITY"
  1599. def bPrepare(self, bContext = None,):
  1600. from mathutils import Vector
  1601. if matrix := self.evaluate_input("Matrix"):
  1602. matrix= matrix.copy().to_3x3(); matrix.transpose()
  1603. self.parameters['X Axis'] = matrix[0]
  1604. self.parameters['Y Axis'] = matrix[1]
  1605. self.parameters['Z Axis'] = matrix[2]
  1606. self.prepared = True; self.executed = True
  1607. class UtilityBoneMatrixHeadTailFlip(MantisNode):
  1608. def __init__(self, signature, base_tree):
  1609. super().__init__(signature, base_tree)
  1610. inputs = [
  1611. "Bone Matrix" ,
  1612. ]
  1613. outputs = [
  1614. "Bone Matrix" ,
  1615. ]
  1616. self.inputs.init_sockets(inputs)
  1617. self.outputs.init_sockets(outputs)
  1618. self.init_parameters()
  1619. self.node_type = "UTILITY"
  1620. def bPrepare(self, bContext = None,):
  1621. from mathutils import Vector, Matrix, Quaternion
  1622. from bpy.types import Bone
  1623. if matrix := self.evaluate_input("Bone Matrix"):
  1624. axis, roll = Bone.AxisRollFromMatrix(matrix.to_3x3())
  1625. new_mat = Bone.MatrixFromAxisRoll(-1*axis, roll)
  1626. length = matrix[3][3]
  1627. new_mat.resize_4x4() # last column contains
  1628. new_mat[0][3] = matrix[0][3] + axis[0]*length # x location
  1629. new_mat[1][3] = matrix[1][3] + axis[1]*length # y location
  1630. new_mat[2][3] = matrix[2][3] + axis[2]*length # z location
  1631. new_mat[3][3] = length # length
  1632. self.parameters["Bone Matrix"] = new_mat
  1633. self.prepared, self.executed = True, True
  1634. class UtilityMatrixTransform(MantisNode):
  1635. def __init__(self, signature, base_tree):
  1636. super().__init__(signature, base_tree)
  1637. inputs = [
  1638. "Matrix 1" ,
  1639. "Matrix 2" ,
  1640. ]
  1641. outputs = [
  1642. "Out Matrix" ,
  1643. ]
  1644. self.inputs.init_sockets(inputs)
  1645. self.outputs.init_sockets(outputs)
  1646. self.init_parameters()
  1647. self.node_type = "UTILITY"
  1648. def bPrepare(self, bContext = None,):
  1649. from mathutils import Vector
  1650. mat1 = self.evaluate_input("Matrix 1"); mat2 = self.evaluate_input("Matrix 2")
  1651. if mat1 and mat2:
  1652. mat1copy = mat1.copy()
  1653. self.parameters["Out Matrix"] = mat2 @ mat1copy
  1654. self.parameters["Out Matrix"].translation = mat1copy.to_translation()+ mat2.to_translation()
  1655. else:
  1656. raise RuntimeError(wrapRed(f"Node {self} did not receive all matrix inputs..."
  1657. " found input 1? {mat1 is not None}, 2? {mat2 is not None}"))
  1658. self.prepared, self.executed = True, True
  1659. class UtilityMatrixInvert(MantisNode):
  1660. def __init__(self, signature, base_tree):
  1661. super().__init__(signature, base_tree, MatrixInvertSockets)
  1662. self.init_parameters()
  1663. self.node_type = "UTILITY"
  1664. def bPrepare(self, bContext = None,):
  1665. from mathutils import Vector
  1666. mat1 = self.evaluate_input("Matrix 1")
  1667. if mat1:
  1668. mat1copy = mat1.copy()
  1669. try:
  1670. self.parameters["Matrix"] = mat1copy.inverted()
  1671. except ValueError as e:
  1672. prRed(f"ERROR: {self}: The matrix cannot be inverted."); prOrange(mat1)
  1673. raise e
  1674. else:
  1675. raise RuntimeError(wrapRed(f"Node {self} did not receive all matrix inputs..."
  1676. " found input 1? {mat1 is not None}"))
  1677. self.prepared, self.executed = True, True
  1678. class UtilityMatrixCompose(MantisNode):
  1679. def __init__(self, signature, base_tree):
  1680. super().__init__(signature, base_tree, MatrixComposeSockets)
  1681. self.init_parameters()
  1682. self.node_type = "UTILITY"
  1683. def bPrepare(self, bContext = None,):
  1684. from mathutils import Matrix
  1685. matrix= Matrix.Identity(3)
  1686. matrix[0] = self.evaluate_input('X Basis Vector')
  1687. matrix[1] = self.evaluate_input('Y Basis Vector')
  1688. matrix[2] = self.evaluate_input('Z Basis Vector')
  1689. matrix.transpose(); matrix=matrix.to_4x4()
  1690. matrix.translation = self.evaluate_input('Translation')
  1691. self.parameters['Matrix']=matrix
  1692. self.prepared = True; self.executed = True
  1693. class UtilityMatrixAlignRoll(MantisNode):
  1694. def __init__(self, signature, base_tree):
  1695. super().__init__(signature, base_tree, MatrixAlignRollSockets)
  1696. self.init_parameters()
  1697. self.node_type = "UTILITY"
  1698. def bPrepare(self, bContext = None,):
  1699. from mathutils import Vector, Matrix
  1700. align_axis = Vector(self.evaluate_input('Alignment Vector'))
  1701. # why do I have to construct a vector here?
  1702. # why is the socket returning a bpy_prop_array ?
  1703. if align_axis.length_squared==0:
  1704. raise RuntimeError(f"WARN: cannot align matrix in {self}"
  1705. " because the alignment vector is zero.")
  1706. input=self.evaluate_input('Matrix').copy()
  1707. y_axis= input.to_3x3().transposed()[1]
  1708. from .utilities import project_point_to_plane
  1709. projected=project_point_to_plane(
  1710. align_axis.normalized(), Vector((0,0,0)), y_axis).normalized()
  1711. # now that we have the projected vector, transform the points from
  1712. # the plane of the y_axis to flat space and get the signed angle
  1713. from math import atan2
  1714. try:
  1715. flattened = (input.to_3x3().inverted() @ projected)
  1716. except ValueError:
  1717. raise ValueError(f"Cannot align the matrix in {self} because it is degenerate.")
  1718. rotation = Matrix.Rotation(atan2(flattened.x, flattened.z), 4, y_axis)
  1719. matrix = rotation @ input.copy()
  1720. matrix.translation=input.translation
  1721. matrix[3][3] = input[3][3]
  1722. self.parameters['Matrix'] = matrix
  1723. self.prepared = True; self.executed = True
  1724. # NOTE: I tried other ways of setting the matrix, including composing
  1725. # it directly from the Y axis, the normalized projection of the align
  1726. # axis, and their cross-product. That only nearly worked.
  1727. # this calculation should not work better, but it does. Why?
  1728. class UtilityTransformationMatrix(MantisNode):
  1729. def __init__(self, signature, base_tree):
  1730. super().__init__(signature, base_tree)
  1731. inputs = [
  1732. "Operation" ,
  1733. "Vector" ,
  1734. "W" ,
  1735. ]
  1736. outputs = [
  1737. "Matrix" ,
  1738. ]
  1739. self.inputs.init_sockets(inputs)
  1740. self.outputs.init_sockets(outputs)
  1741. self.init_parameters()
  1742. self.node_type = "UTILITY"
  1743. def bPrepare(self, bContext = None,):
  1744. from mathutils import Matrix, Vector
  1745. if (operation := self.evaluate_input("Operation")) == 'ROTATE_AXIS_ANGLE':
  1746. # this can, will, and should fail if the axis is 0,0,0
  1747. self.parameters["Matrix"] = rotMat = Matrix.Rotation(self.evaluate_input("W"), 4, Vector(self.evaluate_input("Vector")).normalized())
  1748. elif (operation := self.evaluate_input("Operation")) == 'TRANSLATE':
  1749. m = Matrix.Identity(4)
  1750. if axis := self.evaluate_input("Vector"):
  1751. m[0][3]=axis[0];m[1][3]=axis[1];m[2][3]=axis[2]
  1752. self.parameters['Matrix'] = m
  1753. elif (operation := self.evaluate_input("Operation")) == 'SCALE':
  1754. self.parameters["Matrix"] = Matrix.Scale(self.evaluate_input("W"), 4, Vector(self.evaluate_input("Vector")).normalized())
  1755. else:
  1756. raise NotImplementedError(self.evaluate_input("Operation").__repr__()+ " Operation not yet implemented.")
  1757. self.prepared = True; self.executed = True
  1758. class UtilityIntToString(MantisNode):
  1759. def __init__(self, signature, base_tree):
  1760. super().__init__(signature, base_tree)
  1761. inputs = [
  1762. "Number" ,
  1763. "Zero Padding" ,
  1764. ]
  1765. outputs = [
  1766. "String" ,
  1767. ]
  1768. self.inputs.init_sockets(inputs)
  1769. self.outputs.init_sockets(outputs)
  1770. self.init_parameters()
  1771. self.node_type = "UTILITY"
  1772. def bPrepare(self, bContext = None,):
  1773. number = self.evaluate_input("Number")
  1774. zeroes = self.evaluate_input("Zero Padding")
  1775. # I'm casting to int because I want to support any number, even though the node asks for int.
  1776. self.parameters["String"] = str(int(number)).zfill(int(zeroes))
  1777. self.prepared = True; self.executed = True
  1778. class UtilityArrayGet(MantisNode):
  1779. def __init__(self, signature, base_tree):
  1780. super().__init__(signature, base_tree)
  1781. inputs = [
  1782. "Index" ,
  1783. "OoB Behaviour" ,
  1784. "Array" ,
  1785. ]
  1786. outputs = [
  1787. "Output" ,
  1788. ]
  1789. self.inputs.init_sockets(inputs)
  1790. self.outputs.init_sockets(outputs)
  1791. self.init_parameters()
  1792. self.node_type = "UTILITY"
  1793. self.rerouted=[]
  1794. def bPrepare(self, bContext = None,):
  1795. if len(self.rerouted)>0:
  1796. self.prepared, self.executed = True, True
  1797. return #Either it is already done or it doesn't matter.
  1798. elif self.prepared == False:
  1799. # sort the array entries
  1800. for inp in self.inputs.values():
  1801. inp.links.sort(key=lambda a : -a.multi_input_sort_id)
  1802. oob = self.evaluate_input("OoB Behaviour")
  1803. index = self.evaluate_input("Index")
  1804. from .utilities import cap, wrap
  1805. # we must assume that the array has sent the correct number of links
  1806. if oob == 'WRAP':
  1807. index = wrap(0, len(self.inputs['Array'].links), index)
  1808. if oob == 'HOLD':
  1809. index = cap(index, len(self.inputs['Array'].links)-1)
  1810. array_choose_relink(self, [index], "Array", "Output")
  1811. self.prepared, self.executed = True, True
  1812. class UtilityArrayLength(MantisNode):
  1813. def __init__(self, signature, base_tree):
  1814. super().__init__(signature, base_tree)
  1815. inputs = [
  1816. "Array" ,
  1817. ]
  1818. outputs = [
  1819. "Length" ,
  1820. ]
  1821. self.inputs.init_sockets(inputs)
  1822. self.outputs.init_sockets(outputs)
  1823. self.init_parameters()
  1824. self.node_type = "UTILITY"
  1825. def bPrepare(self, bContext = None,):
  1826. self.parameters["Length"] = len(self.inputs["Array"].links)
  1827. self.prepared, self.executed = True, True
  1828. class UtilitySetBoneMatrixTail(MantisNode):
  1829. def __init__(self, signature, base_tree):
  1830. super().__init__(signature, base_tree)
  1831. inputs = {
  1832. "Matrix" ,
  1833. "Tail Location" ,
  1834. }
  1835. outputs = [
  1836. "Result" ,
  1837. ]
  1838. self.inputs.init_sockets(inputs)
  1839. self.outputs.init_sockets(outputs)
  1840. self.init_parameters()
  1841. self.node_type = "UTILITY"
  1842. def bPrepare(self, bContext = None,):
  1843. from mathutils import Matrix
  1844. matrix = self.evaluate_input("Matrix")
  1845. if matrix is None: matrix = Matrix.Identity(4)
  1846. #just do this for now lol
  1847. self.parameters["Result"] = matrix_from_head_tail(matrix.translation, self.evaluate_input("Tail Location"))
  1848. self.prepared = True; self.executed = True
  1849. class UtilityPrint(MantisNode):
  1850. def __init__(self, signature, base_tree):
  1851. super().__init__(signature, base_tree)
  1852. inputs = [
  1853. "Input" ,
  1854. ]
  1855. self.inputs.init_sockets(inputs)
  1856. self.init_parameters()
  1857. self.node_type = "UTILITY"
  1858. def bPrepare(self, bContext = None,):
  1859. if my_input := self.evaluate_input("Input"):
  1860. print("Preparation phase: ", wrapWhite(self), wrapGreen(my_input))
  1861. self.prepared = True
  1862. def bTransformPass(self, bContext = None,):
  1863. if my_input := self.evaluate_input("Input"):
  1864. print("Execution phase: ", wrapWhite(self), wrapGreen(my_input))
  1865. self.executed = True
  1866. class UtilityCompare(MantisNode):
  1867. def __init__(self, signature, base_tree):
  1868. super().__init__(signature, base_tree, CompareSockets)
  1869. self.init_parameters()
  1870. self.node_type = "UTILITY"
  1871. def bPrepare(self, bContext = None,):
  1872. operation=self.evaluate_input("Comparison")
  1873. a = self.evaluate_input("A")
  1874. b = self.evaluate_input("B")
  1875. if a is None:
  1876. raise GraphError(f"Invalid first input for {self}")
  1877. if b is None:
  1878. raise GraphError(f"Invalid second input for {self}")
  1879. if isinstance(a, str) and isinstance(b, str) and \
  1880. operation not in ['EQUAL', 'NOT_EQUAL']:
  1881. raise GraphError("Strings do not have numerical value to"
  1882. " compute greater than or less than.")
  1883. match operation:
  1884. case "EQUAL":
  1885. self.parameters["Result"] = a == b
  1886. case "NOT_EQUAL":
  1887. self.parameters["Result"] = a != b
  1888. case "GREATER_THAN":
  1889. self.parameters["Result"] = a > b
  1890. case "GREATER_THAN_EQUAL":
  1891. self.parameters["Result"] = a >= b
  1892. case "LESS_THAN":
  1893. self.parameters["Result"] = a < b
  1894. case "LESS_THAN_EQUAL":
  1895. self.parameters["Result"] = a <= b
  1896. self.prepared = True; self.executed = True
  1897. class UtilityChoose(MantisNode):
  1898. def __init__(self, signature, base_tree):
  1899. super().__init__(signature, base_tree)
  1900. inputs = [
  1901. "Condition" ,
  1902. "A" ,
  1903. "B" ,
  1904. ]
  1905. outputs = [
  1906. "Result" ,
  1907. ]
  1908. self.inputs.init_sockets(inputs)
  1909. self.outputs.init_sockets(outputs)
  1910. self.init_parameters()
  1911. self.node_type = "UTILITY"
  1912. def reset_execution(self):
  1913. prepared=self.prepared
  1914. super().reset_execution()
  1915. # prevent this node from attempting to prepare again.
  1916. self.prepared, self.executed = prepared, prepared
  1917. def bPrepare(self, bContext = None,):
  1918. if self.outputs['Result'].links: # otherwise this doesn't matter as it is not connected.
  1919. prGreen(f"Executing Choose Node {self}")
  1920. condition = self.evaluate_input("Condition")
  1921. if self.evaluate_input('A') is not None and self.evaluate_input('B') is not None:
  1922. self.parameters['Result'] = self.evaluate_input('B') if condition else self.evaluate_input('A')
  1923. elif self.evaluate_input('A') is None and self.evaluate_input('B') is None:
  1924. if condition: link = self.inputs['B'].links[0]
  1925. else: link = self.inputs['A'].links[0]
  1926. from_node = link.from_node; from_socket = link.from_socket
  1927. for link in self.outputs['Result'].links:
  1928. from_node.outputs[from_socket].connect(link.to_node, link.to_socket)
  1929. link.die()
  1930. self.flush_links()
  1931. # attempting to init the connections seems more error prone than leaving them be.
  1932. else:
  1933. raise GraphError(f"Choose Node {self} has incorrect types.")
  1934. self.prepared = True; self.executed = True