nodes_generic.py 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. import bpy
  2. from bpy.types import Node
  3. from .base_definitions import MantisNode
  4. from .utilities import (prRed, prGreen, prPurple, prWhite,
  5. prOrange,
  6. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  7. wrapOrange,)
  8. def TellClasses():
  9. return [ InputFloatNode,
  10. InputIntNode,
  11. InputVectorNode,
  12. InputBooleanNode,
  13. InputBooleanThreeTupleNode,
  14. InputRotationOrderNode,
  15. InputTransformSpaceNode,
  16. InputStringNode,
  17. InputQuaternionNode,
  18. InputQuaternionNodeAA,
  19. InputMatrixNode,
  20. InputLayerMaskNode,
  21. # InputGeometryNode,
  22. InputExistingGeometryObjectNode,
  23. InputExistingGeometryDataNode,
  24. # ComposeMatrixNode,
  25. MetaRigMatrixNode,
  26. UtilityMatrixFromCurve,
  27. UtilityPointFromCurve,
  28. UtilityMatricesFromCurve,
  29. # ScaleBoneLengthNode,
  30. UtilityMetaRigNode,
  31. UtilityBonePropertiesNode,
  32. UtilityDriverVariableNode,
  33. UtilityFCurveNode,
  34. UtilityDriverNode,
  35. UtilitySwitchNode,
  36. UtilityKeyframe,
  37. UtilityCombineThreeBoolNode,
  38. UtilityCombineVectorNode,
  39. UtilityCatStringsNode,
  40. UtilityGetBoneLength,
  41. UtilityPointFromBoneMatrix,
  42. UtilitySetBoneLength,
  43. UtilityMatrixSetLocation,
  44. UtilityMatrixGetLocation,
  45. UtilityMatrixFromXForm,
  46. UtilityAxesFromMatrix,
  47. UtilityBoneMatrixHeadTailFlip,
  48. UtilityMatrixTransform,
  49. UtilityTransformationMatrix,
  50. UtilitySetBoneMatrixTail,
  51. UtilityIntToString,
  52. UtilityArrayGet,
  53. #
  54. UtilityCompare,
  55. UtilityChoose,
  56. # for testing
  57. UtilityPrint,
  58. ]
  59. def default_traverse(self,socket):
  60. return None
  61. class InputFloatNode(Node, MantisNode):
  62. '''A node representing inheritance'''
  63. bl_idname = 'InputFloatNode'
  64. bl_label = "Float"
  65. bl_icon = 'NODE'
  66. initialized : bpy.props.BoolProperty(default = False)
  67. def init(self, context):
  68. self.outputs.new('FloatSocket', "Float Input").input = True
  69. self.initialized = True
  70. class InputIntNode(Node, MantisNode):
  71. '''A node representing inheritance'''
  72. bl_idname = 'InputIntNode'
  73. bl_label = "Integer"
  74. bl_icon = 'NODE'
  75. initialized : bpy.props.BoolProperty(default = False)
  76. def init(self, context):
  77. self.outputs.new('IntSocket', "Integer").input = True
  78. self.initialized = True
  79. class InputVectorNode(Node, MantisNode):
  80. '''A node representing inheritance'''
  81. bl_idname = 'InputVectorNode'
  82. bl_label = "Vector"
  83. bl_icon = 'NODE'
  84. initialized : bpy.props.BoolProperty(default = False)
  85. def init(self, context):
  86. self.outputs.new('VectorSocket', "").input = True
  87. self.initialized = True
  88. class InputBooleanNode(Node, MantisNode):
  89. '''A node representing inheritance'''
  90. bl_idname = 'InputBooleanNode'
  91. bl_label = "Boolean"
  92. bl_icon = 'NODE'
  93. initialized : bpy.props.BoolProperty(default = False)
  94. def init(self, context):
  95. self.outputs.new('BooleanSocket', "").input = True
  96. self.initialized = True
  97. class InputBooleanThreeTupleNode(Node, MantisNode):
  98. '''A node representing inheritance'''
  99. bl_idname = 'InputBooleanThreeTupleNode'
  100. bl_label = "Boolean Vector"
  101. bl_icon = 'NODE'
  102. initialized : bpy.props.BoolProperty(default = False)
  103. def init(self, context):
  104. self.outputs.new('BooleanThreeTupleSocket', "")
  105. self.initialized = True
  106. class InputRotationOrderNode(Node, MantisNode):
  107. '''A node representing inheritance'''
  108. bl_idname = 'InputRotationOrderNode'
  109. bl_label = "Rotation Order"
  110. bl_icon = 'NODE'
  111. initialized : bpy.props.BoolProperty(default = False)
  112. def init(self, context):
  113. self.outputs.new('RotationOrderSocket', "").input = True
  114. self.initialized = True
  115. class InputTransformSpaceNode(Node, MantisNode):
  116. '''A node representing inheritance'''
  117. bl_idname = 'InputTransformSpaceNode'
  118. bl_label = "Transform Space"
  119. bl_icon = 'NODE'
  120. initialized : bpy.props.BoolProperty(default = False)
  121. def init(self, context):
  122. self.outputs.new('TransformSpaceSocket', "").input = True
  123. self.initialized = True
  124. class InputStringNode(Node, MantisNode):
  125. '''A node representing inheritance'''
  126. bl_idname = 'InputStringNode'
  127. bl_label = "String"
  128. bl_icon = 'NODE'
  129. initialized : bpy.props.BoolProperty(default = False)
  130. def init(self, context):
  131. self.outputs.new('StringSocket', "").input = True
  132. self.initialized = True
  133. class InputQuaternionNode(Node, MantisNode):
  134. '''A node representing inheritance'''
  135. bl_idname = 'InputQuaternionNode'
  136. bl_label = "Quaternion"
  137. bl_icon = 'NODE'
  138. initialized : bpy.props.BoolProperty(default = False)
  139. def init(self, context):
  140. self.outputs.new('QuaternionSocket', "").input = True
  141. self.initialized = True
  142. class InputQuaternionNodeAA(Node, MantisNode):
  143. '''A node representing inheritance'''
  144. bl_idname = 'InputQuaternionNodeAA'
  145. bl_label = "Axis Angle"
  146. bl_icon = 'NODE'
  147. initialized : bpy.props.BoolProperty(default = False)
  148. def init(self, context):
  149. self.outputs.new('QuaternionSocketAA', "").input = True
  150. self.initialized = True
  151. class InputMatrixNode(Node, MantisNode):
  152. '''A node representing inheritance'''
  153. bl_idname = 'InputMatrixNode'
  154. bl_label = "Matrix"
  155. bl_icon = 'NODE'
  156. first_row : bpy.props.FloatVectorProperty(name="", size=4, default = (1.0, 0.0, 0.0, 0.0,))
  157. second_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 1.0, 0.0, 0.0,))
  158. third_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 0.0, 1.0, 0.0,))
  159. fourth_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 0.0, 0.0, 1.0,))
  160. initialized : bpy.props.BoolProperty(default = False)
  161. def set_matrix(self):
  162. return (self.first_row[ 0], self.first_row[ 1], self.first_row[ 2], self.first_row[ 3],
  163. self.second_row[0], self.second_row[1], self.second_row[2], self.second_row[3],
  164. self.third_row[ 0], self.third_row[ 1], self.third_row[ 2], self.third_row[ 3],
  165. self.fourth_row[0], self.fourth_row[1], self.fourth_row[2], self.fourth_row[3],)
  166. def init(self, context):
  167. self.outputs.new('MatrixSocket', "Matrix")
  168. self.initialized = True
  169. def update_node(self, context):
  170. self.outputs["Matrix"].default_value = self.set_matrix()
  171. def draw_buttons(self, context, layout):
  172. # return
  173. layout.prop(self, "first_row")
  174. layout.prop(self, "second_row")
  175. layout.prop(self, "third_row")
  176. layout.prop(self, "fourth_row")
  177. def update(self):
  178. mat_sock = self.outputs[0]
  179. mat_sock.default_value = self.set_matrix()
  180. # TODO: reimplement the nodes beneath here
  181. # from .utilities import QuerySocket, to_mathutils_value
  182. # class ComposeMatrixNode(Node, MantisNode):
  183. # '''A utility node for composing a matrix'''
  184. # bl_idname = 'ComposeMatrixNode'
  185. # bl_label = "Compose Matrix"
  186. # bl_icon = 'NODE'
  187. # def init(self, context):
  188. # self.inputs.new('VectorTranslationSocket', "Translation")
  189. # self.inputs.new('GenericRotationSocket', "Rotation")
  190. # self.inputs.new('VectorScaleSocket', "Scale")
  191. # self.outputs.new('MatrixSocket', "Matrix")
  192. # def update_node(self, context = None):
  193. # from mathutils import Matrix, Euler, Quaternion, Vector
  194. # mat_sock = self.outputs[0]
  195. # rotation = Matrix.Identity(4)
  196. # scale = Matrix.Identity(4)
  197. # translation = Matrix.Identity(4)
  198. # sock = QuerySocket(self.inputs["Rotation"])[0]
  199. # val = to_mathutils_value(sock)
  200. # if (val):
  201. # if (isinstance(val, Vector)):
  202. # val = Euler((val[0], val[1], val[2]), 'XYZ')
  203. # rotation = val.to_matrix().to_4x4()
  204. # sock = QuerySocket(self.inputs["Scale"])[0]
  205. # val = to_mathutils_value(sock)
  206. # if (val):
  207. # if (isinstance(val, Vector)):
  208. # scale = Matrix.Scale(val[0],4,(1.0,0.0,0.0)) @ Matrix.Scale(val[1],4,(0.0,1.0,0.0)) @ Matrix.Scale(val[2],4,(0.0,0.0,1.0))
  209. # sock = QuerySocket(self.inputs["Translation"])[0]
  210. # val = to_mathutils_value(sock)
  211. # if (val):
  212. # if (isinstance(val, Vector)):
  213. # translation = Matrix.Translation((val))
  214. # mat = translation @ rotation @ scale
  215. # mat_sock.default_value = ( mat[0][0], mat[0][1], mat[0][2], mat[0][3],
  216. # mat[1][0], mat[1][1], mat[1][2], mat[1][3],
  217. # mat[2][0], mat[2][1], mat[2][2], mat[2][3],
  218. # mat[3][0], mat[3][1], mat[3][2], mat[3][3], )
  219. class ScaleBoneLengthNode(Node, MantisNode):
  220. '''Scale Bone Length'''
  221. bl_idname = 'ScaleBoneLength'
  222. bl_label = "Scale Bone Length"
  223. bl_icon = 'NODE'
  224. initialized : bpy.props.BoolProperty(default = False)
  225. # === Optional Functions ===
  226. def init(self, context):
  227. self.inputs.new('MatrixSocket', "In Matrix")
  228. self.inputs.new('FloatSocket', "Factor")
  229. self.outputs.new('MatrixSocket', "Out Matrix")
  230. self.initialized = True
  231. class MetaRigMatrixNode(Node, MantisNode):
  232. # Identical to the above, except
  233. '''A node representing a bone's matrix'''
  234. bl_idname = 'MetaRigMatrixNode'
  235. bl_label = "Matrix"
  236. bl_icon = 'NODE'
  237. first_row : bpy.props.FloatVectorProperty(name="", size=4, default = (1.0, 0.0, 0.0, 0.0,))
  238. second_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 1.0, 0.0, 0.0,))
  239. third_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 0.0, 1.0, 0.0,))
  240. fourth_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 0.0, 0.0, 1.0,))
  241. initialized : bpy.props.BoolProperty(default = False)
  242. def set_matrix(self):
  243. return (self.first_row[ 0], self.first_row[ 1], self.first_row[ 2], self.first_row[ 3],
  244. self.second_row[0], self.second_row[1], self.second_row[2], self.second_row[3],
  245. self.third_row[ 0], self.third_row[ 1], self.third_row[ 2], self.third_row[ 3],
  246. self.fourth_row[0], self.fourth_row[1], self.fourth_row[2], self.fourth_row[3],)
  247. def init(self, context):
  248. self.outputs.new('MatrixSocket', "Matrix")
  249. self.initialized = True
  250. def traverse(self, context):
  251. from mathutils import Matrix
  252. v = self.outputs[0].default_value
  253. # print( Matrix( ( ( v[ 0], v[ 1], v[ 2], v[ 3],),
  254. # ( v[ 4], v[ 5], v[ 6], v[ 7],),
  255. # ( v[ 8], v[ 9], v[10], v[11],),
  256. # ( v[12], v[13], v[14], v[15],), ) ) )
  257. return None
  258. def update_node(self, context):
  259. self.outputs["Matrix"].default_value = self.set_matrix()
  260. def update(self):
  261. mat_sock = self.outputs[0]
  262. mat_sock.default_value = self.set_matrix()
  263. class UtilityMatrixFromCurve(Node, MantisNode):
  264. """Gets a matrix from a curve."""
  265. bl_idname = "UtilityMatrixFromCurve"
  266. bl_label = "Matrix from Curve"
  267. bl_icon = "NODE"
  268. initialized : bpy.props.BoolProperty(default = False)
  269. def init(self, context):
  270. curv = self.inputs.new("EnumCurveSocket", "Curve")
  271. curv.icon = "OUTLINER_OB_CURVE"
  272. self.inputs.new('IntSocket', 'Total Divisions')
  273. self.outputs.new("MatrixSocket", "Matrix")
  274. self.initialized = True
  275. class UtilityPointFromCurve(Node, MantisNode):
  276. """Gets a point from a curve."""
  277. bl_idname = "UtilityPointFromCurve"
  278. bl_label = "Point from Curve"
  279. bl_icon = "NODE"
  280. initialized : bpy.props.BoolProperty(default = False)
  281. def init(self, context):
  282. curv = self.inputs.new("EnumCurveSocket", "Curve")
  283. curv.icon = "OUTLINER_OB_CURVE"
  284. self.inputs.new('FloatFactorSocket', 'Factor')
  285. self.outputs.new("VectorSocket", "Point")
  286. self.initialized = True
  287. class UtilityMatricesFromCurve(Node, MantisNode):
  288. """Gets a matrix from a curve."""
  289. bl_idname = "UtilityMatricesFromCurve"
  290. bl_label = "Matrices from Curve"
  291. bl_icon = "NODE"
  292. initialized : bpy.props.BoolProperty(default = False)
  293. def init(self, context):
  294. curv = self.inputs.new("EnumCurveSocket", "Curve")
  295. curv.icon = "OUTLINER_OB_CURVE"
  296. self.inputs.new('IntSocket', 'Total Divisions')
  297. o = self.outputs.new("MatrixSocket", "Matrices")
  298. o.display_shape = 'SQUARE_DOT'
  299. self.initialized = True
  300. class UtilityMetaRigNode(Node, MantisNode):
  301. """Gets a matrix from a meta-rig bone."""
  302. bl_idname = "UtilityMetaRig"
  303. bl_label = "Meta-Rig"
  304. bl_icon = "NODE"
  305. armature:bpy.props.StringProperty()
  306. pose_bone:bpy.props.StringProperty()
  307. initialized : bpy.props.BoolProperty(default = False)
  308. def init(self, context):
  309. armt = self.inputs.new("EnumMetaRigSocket", "Meta-Armature")
  310. bone = self.inputs.new("EnumMetaBoneSocket", "Meta-Bone")
  311. armt.icon = "OUTLINER_OB_ARMATURE"
  312. bone.icon = "BONE_DATA"
  313. bone.hide=True
  314. self.outputs.new("MatrixSocket", "Matrix")
  315. self.initialized = True
  316. def display_update(self, parsed_tree, context):
  317. from .base_definitions import get_signature_from_edited_tree
  318. nc = parsed_tree.get(get_signature_from_edited_tree(self, context))
  319. if nc:
  320. self.armature= nc.evaluate_input("Meta-Armature")
  321. self.pose_bone= nc.evaluate_input("Meta-Bone")
  322. if not self.armature:
  323. self.inputs["Meta-Bone"].hide=True
  324. else:
  325. self.inputs["Meta-Bone"].hide=False
  326. if self.inputs["Meta-Armature"].is_connected:
  327. self.inputs["Meta-Armature"].search_prop = None
  328. if self.inputs["Meta-Bone"].is_connected:
  329. self.inputs["Meta-Bone"].search_prop = None
  330. class UtilityBonePropertiesNode(Node, MantisNode):
  331. """Provides as sockets strings identifying bone transform properties."""
  332. bl_idname = "UtilityBoneProperties"
  333. bl_label = "Bone Properties"
  334. bl_icon = "NODE"
  335. #bl_width_default = 250
  336. initialized : bpy.props.BoolProperty(default = False)
  337. def init(self, context):
  338. self.outputs.new("ParameterStringSocket", "matrix")
  339. self.outputs.new("ParameterStringSocket", "matrix_local")
  340. self.outputs.new("ParameterStringSocket", "matrix_basis")
  341. self.outputs.new("ParameterStringSocket", "head")
  342. self.outputs.new("ParameterStringSocket", "tail")
  343. self.outputs.new("ParameterStringSocket", "length")
  344. self.outputs.new("ParameterStringSocket", "rotation")
  345. self.outputs.new("ParameterStringSocket", "location")
  346. self.outputs.new("ParameterStringSocket", "scale")
  347. self.initialized = True
  348. for o in self.outputs:
  349. o.text_only = True
  350. class UtilityDriverVariableNode(Node, MantisNode):
  351. """Creates a variable for use in a driver."""
  352. bl_idname = "UtilityDriverVariable"
  353. bl_label = "Driver Variable"
  354. bl_icon = "NODE"
  355. initialized : bpy.props.BoolProperty(default = False)
  356. def init(self, context):
  357. self.inputs.new("EnumDriverVariableType", "Variable Type") # 0
  358. self.inputs.new("ParameterStringSocket", "Property") # 1
  359. self.inputs.new("IntSocket", "Property Index") # 2
  360. self.inputs.new("EnumDriverVariableTransformChannel", "Transform Channel") # 3
  361. self.inputs.new("EnumDriverVariableEvaluationSpace", "Evaluation Space") # 4
  362. self.inputs.new("EnumDriverRotationMode", "Rotation Mode") # 5
  363. self.inputs.new("xFormSocket", "xForm 1") # 6
  364. self.inputs.new("xFormSocket", "xForm 2") # 7
  365. self.outputs.new("DriverVariableSocket", "Driver Variable")
  366. self.inputs[3].hide = True
  367. self.initialized = True
  368. # def update_on_socket_change(self, context):
  369. # self.update()
  370. def display_update(self, parsed_tree, context):
  371. from .base_definitions import get_signature_from_edited_tree
  372. if self.inputs["Variable Type"].is_linked:
  373. if context.space_data:
  374. node_tree = context.space_data.path[0].node_tree
  375. nc = parsed_tree.get(get_signature_from_edited_tree(self, context))
  376. if nc:
  377. driver_type = nc.evaluate_input("Variable Type")
  378. else:
  379. driver_type = self.inputs[0].default_value
  380. if driver_type == 'SINGLE_PROP':
  381. self.inputs[1].hide = False
  382. self.inputs[2].hide = False
  383. self.inputs[3].hide = True
  384. self.inputs[4].hide = False
  385. self.inputs[5].hide = False
  386. self.inputs[6].hide = False
  387. self.inputs[7].hide = True
  388. elif driver_type == 'LOC_DIFF':
  389. self.inputs[1].hide = True
  390. self.inputs[2].hide = True
  391. self.inputs[3].hide = True
  392. self.inputs[4].hide = True
  393. self.inputs[5].hide = True
  394. self.inputs[6].hide = False
  395. self.inputs[7].hide = False
  396. elif driver_type == 'ROTATION_DIFF':
  397. self.inputs[1].hide = True
  398. self.inputs[2].hide = True
  399. self.inputs[3].hide = True
  400. self.inputs[4].hide = True
  401. self.inputs[5].hide = False
  402. self.inputs[6].hide = False
  403. self.inputs[7].hide = False
  404. elif driver_type == 'TRANSFORMS':
  405. self.inputs[1].hide = True
  406. self.inputs[2].hide = True
  407. self.inputs[3].hide = False
  408. self.inputs[4].hide = False
  409. self.inputs[5].hide = False
  410. self.inputs[6].hide = False
  411. self.inputs[7].hide = True
  412. self.inputs[3].hide = True
  413. class UtilityFCurveNode(Node, MantisNode):
  414. """Creates an fCurve for use with a driver."""
  415. bl_idname = "UtilityFCurve"
  416. bl_label = "fCurve"
  417. bl_icon = "NODE"
  418. use_kf_nodes : bpy.props.BoolProperty(default=True)
  419. # fake_fcurve_ob : bpy.props.PointerProperty(type=bpy.types.Object)
  420. initialized : bpy.props.BoolProperty(default = False)
  421. def init(self, context):
  422. self.outputs.new("FCurveSocket", "fCurve")
  423. # if not self.fake_fcurve_ob:
  424. # ob = bpy.data.objects.new("fake_ob_"+self.name, None)
  425. # self.fake_fcurve_ob = ob
  426. # ob.animation_data_create()
  427. # ob.animation_data.action = bpy.data.actions.new('fake_action_'+self.name)
  428. # fc = ob.animation_data.action.fcurves.new('location', index=0, action_group='location')
  429. # fc.keyframe_points.add(2)
  430. # kf0 = fc.keyframe_points[0]; kf0.co_ui = (0, 0)
  431. # kf1 = fc.keyframe_points[1]; kf1.co_ui = (1, 1)
  432. # #
  433. # kf0.interpolation = 'BEZIER'
  434. # kf0.handle_left_type = 'AUTO_CLAMPED'
  435. # kf0.handle_right_type = 'AUTO_CLAMPED'
  436. # kf1.interpolation = 'BEZIER'
  437. # kf1.handle_left_type = 'AUTO_CLAMPED'
  438. # kf1.handle_right_type = 'AUTO_CLAMPED'
  439. #
  440. self.initialized = True
  441. def draw_buttons(self, context, layout):
  442. # return
  443. # if self.use_kf_nodes:
  444. # layout.prop(self, "use_kf_nodes", text="[ Use fCurve data ]", toggle=True, invert_checkbox=True)
  445. layout.operator( 'mantis.fcurve_node_add_kf' )
  446. if (len(self.inputs) > 0):
  447. layout.operator( 'mantis.fcurve_node_remove_kf' )
  448. # else:
  449. # layout.prop(self, "use_kf_nodes", text="[ Use Keyframe Nodes ]", toggle=True)
  450. # layout.operator('mantis.edit_fcurve_node')
  451. # THE DIFFICULT part is getting it to show up in the graph editor
  452. # TRY:
  453. # a modal operator that opens the Graph Editor
  454. # and then finishes when it is closed
  455. # it would reveal the object holding the fCurve before
  456. # showing the Graph Editor
  457. # And hide it after closing it.
  458. #
  459. class UtilityDriverNode(Node, MantisNode):
  460. """Represents a Driver relationship"""
  461. bl_idname = "UtilityDriver"
  462. bl_label = "Driver"
  463. bl_icon = "NODE"
  464. initialized : bpy.props.BoolProperty(default = False)
  465. def init(self, context):
  466. self.inputs.new("EnumDriverType", "Driver Type")
  467. self.inputs.new("FCurveSocket", "fCurve")
  468. self.inputs.new("StringSocket", "Expression")
  469. self.outputs.new("DriverSocket", "Driver")
  470. self.initialized = True
  471. def update(self):
  472. return
  473. context = bpy.context
  474. try:
  475. tree = context.space_data.path[0].node_tree
  476. proceed = True
  477. except AttributeError:
  478. proceed = False
  479. if proceed:
  480. from .f_nodegraph import (GetDownstreamXFormNodes, get_node_container)
  481. if (node_container := get_node_container(self, context)[0]):
  482. dType = node_container.evaluate_input("Driver Type")
  483. else:
  484. dType = self.inputs[0].default_value
  485. if dType == 'SCRIPTED':
  486. self.inputs["Expression"].hide = False
  487. else:
  488. self.inputs["Expression"].hide = True
  489. def draw_buttons(self, context, layout):
  490. # return
  491. layout.operator( 'mantis.driver_node_add_variable' )
  492. if (len(self.inputs) > 3):
  493. layout.operator( 'mantis.driver_node_remove_variable' )
  494. class UtilitySwitchNode(Node, MantisNode):
  495. """Represents a switch relationship between one driver property and one or more driven properties."""
  496. bl_idname = "UtilitySwitch"
  497. bl_label = "Switch"
  498. bl_icon = "NODE"
  499. initialized : bpy.props.BoolProperty(default = False)
  500. def init(self, context):
  501. # self.inputs.new("xFormSocket", "xForm")
  502. self.inputs.new("ParameterStringSocket", "Parameter")
  503. self.inputs.new("IntSocket", "Parameter Index")
  504. self.inputs.new("BooleanSocket", "Invert Switch")
  505. self.outputs.new("DriverSocket", "Driver")
  506. self.initialized = True
  507. class UtilityCombineThreeBoolNode(Node, MantisNode):
  508. """Combines three booleans into a three-bool."""
  509. bl_idname = "UtilityCombineThreeBool"
  510. bl_label = "CombineThreeBool"
  511. bl_icon = "NODE"
  512. initialized : bpy.props.BoolProperty(default = False)
  513. def init(self, context):
  514. self.inputs.new("BooleanSocket", "X")
  515. self.inputs.new("BooleanSocket", "Y")
  516. self.inputs.new("BooleanSocket", "Z")
  517. self.outputs.new("BooleanThreeTupleSocket", "Three-Bool")
  518. # this node should eventually just be a Combine Boolean Three-Tuple node
  519. # and the "Driver" output will need to be figured out some other way
  520. self.initialized = True
  521. class UtilityCombineVectorNode(Node, MantisNode):
  522. """Combines three floats into a vector."""
  523. bl_idname = "UtilityCombineVector"
  524. bl_label = "CombineVector"
  525. bl_icon = "NODE"
  526. initialized : bpy.props.BoolProperty(default = False)
  527. def init(self, context):
  528. self.inputs.new("FloatSocket", "X")
  529. self.inputs.new("FloatSocket", "Y")
  530. self.inputs.new("FloatSocket", "Z")
  531. self.outputs.new("VectorSocket", "Vector")
  532. # this node should eventually just be a Combine Boolean Three-Tuple node
  533. # and the "Driver" output will need to be figured out some other way
  534. self.initialized = True
  535. class UtilityCatStringsNode(Node, MantisNode):
  536. """Adds a suffix to a string"""
  537. bl_idname = "UtilityCatStrings"
  538. bl_label = "Concatenate Strings"
  539. bl_icon = "NODE"
  540. initialized : bpy.props.BoolProperty(default = False)
  541. def init(self, context):
  542. self.inputs.new("StringSocket", "String_1")
  543. self.inputs.new("StringSocket", "String_2")
  544. self.outputs.new("StringSocket", "OutputString")
  545. self.initialized = True
  546. class InputLayerMaskNode(Node, MantisNode):
  547. """Represents a layer mask for a bone."""
  548. bl_idname = "InputLayerMaskNode"
  549. bl_label = "Layer Mask"
  550. bl_icon = "NODE"
  551. initialized : bpy.props.BoolProperty(default = False)
  552. def init(self, context):
  553. self.outputs.new("LayerMaskInputSocket", "Layer Mask")
  554. self.initialized = True
  555. class InputExistingGeometryObjectNode(Node, MantisNode):
  556. """Represents an existing geometry object from within the scene."""
  557. bl_idname = "InputExistingGeometryObject"
  558. bl_label = "Existing Object"
  559. bl_icon = "NODE"
  560. initialized : bpy.props.BoolProperty(default = False)
  561. # We want Mantis to import widgets and stuff, so we hold a reference to the object
  562. object_reference : bpy.props.PointerProperty(type=bpy.types.Object,)
  563. def init(self, context):
  564. self.inputs.new("StringSocket", "Name")
  565. self.outputs.new("xFormSocket", "Object")
  566. self.initialized = True
  567. def display_update(self, parsed_tree, context):
  568. from .base_definitions import get_signature_from_edited_tree
  569. nc = parsed_tree.get(get_signature_from_edited_tree(self, context))
  570. if nc: # this is done here so I don't have to define yet another custom socket.
  571. self.object_reference = bpy.data.objects.get(nc.evaluate_input("Name"))
  572. # TODO: maybe I should hold a data reference here, too.
  573. # but it is complicated by the fact that Mantis does not distinguish b/tw geo types
  574. class InputExistingGeometryDataNode(Node, MantisNode):
  575. """Represents a mesh or curve datablock from the scene."""
  576. bl_idname = "InputExistingGeometryData"
  577. bl_label = "Existing Geometry"
  578. bl_icon = "NODE"
  579. initialized : bpy.props.BoolProperty(default = False)
  580. def init(self, context):
  581. self.inputs.new("StringSocket", "Name")
  582. self.outputs.new("GeometrySocket", "Geometry")
  583. self.initialized = True
  584. class UtilityGetBoneLength(Node, MantisNode):
  585. """Returns the length of the bone from its matrix."""
  586. bl_idname = "UtilityGetBoneLength"
  587. bl_label = "Get Bone Length"
  588. bl_icon = "NODE"
  589. initialized : bpy.props.BoolProperty(default = False)
  590. def init(self, context):
  591. self.inputs.new("MatrixSocket", "Bone Matrix")
  592. self.outputs.new("FloatSocket", "Bone Length")
  593. self.initialized = True
  594. # TODO: make it work with BBones!
  595. class UtilityPointFromBoneMatrix(Node, MantisNode):
  596. """Returns a point representing the location along a bone, given a matrix representing that bone's shape."""
  597. bl_idname = "UtilityPointFromBoneMatrix"
  598. bl_label = "Point from Bone Matrix"
  599. bl_icon = "NODE"
  600. initialized : bpy.props.BoolProperty(default = False)
  601. def init(self, context):
  602. self.inputs.new("MatrixSocket", "Bone Matrix")
  603. self.inputs.new("FloatFactorSocket", "Head/Tail")
  604. self.outputs.new("VectorSocket", "Point")
  605. self.initialized = True
  606. class UtilitySetBoneLength(Node, MantisNode):
  607. """Sets the length of a bone matrix."""
  608. bl_idname = "UtilitySetBoneLength"
  609. bl_label = "Set Bone Matrix Length"
  610. bl_icon = "NODE"
  611. initialized : bpy.props.BoolProperty(default = False)
  612. def init(self, context):
  613. self.inputs.new("MatrixSocket", "Bone Matrix")
  614. self.inputs.new("FloatSocket", "Length")
  615. self.outputs.new("MatrixSocket", "Bone Matrix")
  616. self.initialized = True
  617. class UtilityKeyframe(Node, MantisNode):
  618. """A keyframe for a FCurve"""
  619. bl_idname = "UtilityKeyframe"
  620. bl_label = "KeyFrame"
  621. bl_icon = "NODE"
  622. initialized : bpy.props.BoolProperty(default = False)
  623. def init(self, context):
  624. # x and y
  625. # output is keyframe
  626. # self.inputs.new("EnumKeyframeInterpolationTypeSocket", "Interpolation")
  627. # self.inputs.new("EnumKeyframeBezierHandleType", "Left Handle Type")
  628. # self.inputs.new("EnumKeyframeBezierHandleType", "Right Handle Type")
  629. # self.inputs.new("FloatSocket", "Left Handle Distance")
  630. # self.inputs.new("FloatSocket", "Left Handle Value")
  631. # self.inputs.new("FloatSocket", "Right Handle Frame")
  632. # self.inputs.new("FloatSocket", "Right Handle Value")
  633. self.inputs.new("FloatSocket", "Frame")
  634. self.inputs.new("FloatSocket", "Value")
  635. self.outputs.new("KeyframeSocket", "Keyframe")
  636. # there will eventually be inputs for e.g. key type, key handles, etc.
  637. # right now I am gonna hardcode LINEAR keyframes so I don't have to deal with anything else
  638. # TODO TODO TODO
  639. # def display_update(self, parsed_tree, context):
  640. # if context.space_data:
  641. # nc = parsed_tree.get(get_signature_from_edited_tree(self, context))
  642. # if nc.evaluate_input("Interpolation") in ["CONSTANT", "LINEAR"]:
  643. # for inp in self.inputs[1:6]:
  644. # inp.hide = True
  645. # else:
  646. # if nc.evaluate_input("Left Handle Type") in ["FREE", "ALIGNED"]:
  647. # for inp in self.inputs[1:6]:
  648. # inp.hide = False
  649. self.initialized = True
  650. class UtilityBoneMatrixHeadTailFlip(Node, MantisNode):
  651. """Flips a bone matrix so that the head is where the tail was and visa versa."""
  652. bl_idname = "UtilityBoneMatrixHeadTailFlip"
  653. bl_label = "Flip Head/Tail"
  654. bl_icon = "NODE"
  655. initialized : bpy.props.BoolProperty(default = False)
  656. def init(self, context):
  657. self.inputs.new("MatrixSocket", "Bone Matrix")
  658. self.outputs.new("MatrixSocket", "Bone Matrix")
  659. self.initialized = True
  660. class UtilityMatrixTransform(Node, MantisNode):
  661. """Transforms a matrix by another."""
  662. bl_idname = "UtilityMatrixTransform"
  663. bl_label = "Matrix Transform"
  664. bl_icon = "NODE"
  665. initialized : bpy.props.BoolProperty(default = False)
  666. def init(self, context):
  667. self.inputs.new("MatrixSocket", "Matrix 1")
  668. self.inputs.new("MatrixSocket", "Matrix 2")
  669. self.outputs.new("MatrixSocket", "Out Matrix")
  670. self.initialized = True
  671. class UtilityMatrixSetLocation(Node, MantisNode):
  672. """Sets a matrix's location."""
  673. bl_idname = "UtilityMatrixSetLocation"
  674. bl_label = "Set Matrix Location"
  675. bl_icon = "NODE"
  676. initialized : bpy.props.BoolProperty(default = False)
  677. def init(self, context):
  678. self.inputs.new("MatrixSocket", "Matrix")
  679. self.inputs.new("VectorSocket", "Location")
  680. self.outputs.new("MatrixSocket", "Matrix")
  681. self.initialized = True
  682. class UtilityMatrixGetLocation(Node, MantisNode):
  683. """Gets a matrix's location."""
  684. bl_idname = "UtilityMatrixGetLocation"
  685. bl_label = "Get Matrix Location"
  686. bl_icon = "NODE"
  687. initialized : bpy.props.BoolProperty(default = False)
  688. def init(self, context):
  689. self.inputs.new("MatrixSocket", "Matrix")
  690. self.outputs.new("VectorSocket", "Location")
  691. self.initialized = True
  692. class UtilityTransformationMatrix(Node, MantisNode):
  693. """Constructs a matrix representing a transformation"""
  694. bl_idname = "UtilityTransformationMatrix"
  695. bl_label = "Transformation Matrix"
  696. bl_icon = "NODE"
  697. initialized : bpy.props.BoolProperty(default = False)
  698. def init(self, context):
  699. # first input is a transformation type - translation, rotation, or scale
  700. # rotation is an especially annoying feature because it can take multiple types
  701. # so Euler, axis/angle, quaternion, matrix...
  702. # for now I am only going to implement axis-angle
  703. # it should get an axis and a magnitude
  704. # self.inputs.new("MatrixSocket", "Bone Matrix")
  705. self.inputs.new("MatrixTransformOperation", "Operation")
  706. self.inputs.new("VectorSocket", "Vector")
  707. self.inputs.new("FloatSocket", "W")
  708. self.outputs.new("MatrixSocket", "Matrix")
  709. self.initialized = True
  710. # Blender calculates bone roll this way...
  711. # https://projects.blender.org/blender/blender/src/commit/dd209221675ac7b62ce47b7ea42f15cbe34a6035/source/blender/editors/armature/armature_edit.cc#L281
  712. # but this looks like it will be harder to re-implement than to re-use. Unfortunately, it doesn't apply directly to a matrix so I have to call a method
  713. # in the edit bone. Thus, this node currently does nothing and the xForm node has to handle it by reading back through the tree....
  714. # this will lead to bugs.
  715. # So instead, we need to avoid calculating the roll for now.
  716. # but I want to make that its own node and add roll-recalc to this node, too.
  717. class UtilitySetBoneMatrixTail(Node, MantisNode):
  718. """Constructs a matrix representing a transformation"""
  719. bl_idname = "UtilitySetBoneMatrixTail"
  720. bl_label = "Set Bone Matrix Tail"
  721. bl_icon = "NODE"
  722. initialized : bpy.props.BoolProperty(default = False)
  723. def init(self, context):
  724. self.inputs.new("MatrixSocket", "Matrix")
  725. self.inputs.new("VectorSocket", "Tail Location")
  726. self.outputs.new("MatrixSocket", "Result")
  727. self.initialized = True
  728. class UtilityMatrixFromXForm(Node, MantisNode):
  729. """Returns the matrix of the given xForm node."""
  730. bl_idname = "UtilityMatrixFromXForm"
  731. bl_label = "Matrix of xForm"
  732. bl_icon = "NODE"
  733. initialized : bpy.props.BoolProperty(default = False)
  734. def init(self, context):
  735. self.inputs.new("xFormSocket", "xForm")
  736. self.outputs.new("MatrixSocket", "Matrix")
  737. self.initialized = True
  738. class UtilityAxesFromMatrix(Node, MantisNode):
  739. """Returns the axes of the matrix."""
  740. bl_idname = "UtilityAxesFromMatrix"
  741. bl_label = "Axes of Matrix"
  742. bl_icon = "NODE"
  743. initialized : bpy.props.BoolProperty(default = False)
  744. def init(self, context):
  745. self.inputs.new("MatrixSocket", "Matrix")
  746. self.outputs.new("VectorSocket", "X Axis")
  747. self.outputs.new("VectorSocket", "Y Axis")
  748. self.outputs.new("VectorSocket", "Z Axis")
  749. self.initialized = True
  750. class UtilityIntToString(Node, MantisNode):
  751. """Converts a number to a string"""
  752. bl_idname = "UtilityIntToString"
  753. bl_label = "Number String"
  754. bl_icon = "NODE"
  755. initialized : bpy.props.BoolProperty(default = False)
  756. def init(self, context):
  757. self.inputs.new("IntSocket", "Number")
  758. self.inputs.new("IntSocket", "Zero Padding")
  759. self.outputs.new("StringSocket", "String")
  760. self.initialized = True
  761. class UtilityArrayGet(Node, MantisNode):
  762. """Gets a value from an array at a specified index."""
  763. bl_idname = "UtilityArrayGet"
  764. bl_label = "Array Get"
  765. bl_icon = "NODE"
  766. initialized : bpy.props.BoolProperty(default = False)
  767. def init(self, context):
  768. self.inputs.new('EnumArrayGetOptions', 'OoB Behaviour')
  769. self.inputs.new("IntSocket", "Index")
  770. s = self.inputs.new("WildcardSocket", "Array", use_multi_input=True)
  771. s.display_shape = 'SQUARE_DOT'
  772. self.outputs.new("WildcardSocket", "Output")
  773. self.initialized = True
  774. def update(self):
  775. wildcard_color = (0.0,0.0,0.0,0.0)
  776. if self.inputs['Array'].is_linked == False:
  777. self.inputs['Array'].color = wildcard_color
  778. self.outputs['Output'].color = wildcard_color
  779. def insert_link(self, link):
  780. prGreen(link.from_node.name, link.from_socket.identifier, link.to_node.name, link.to_socket.identifier)
  781. if link.to_socket.identifier == self.inputs['Array'].identifier:
  782. from_socket = link.from_socket
  783. print (from_socket.color)
  784. if hasattr(from_socket, "color"):
  785. self.inputs['Array'].color = from_socket.color
  786. self.outputs['Output'].color = from_socket.color
  787. class UtilityCompare(Node, MantisNode):
  788. """Compares two inputs and produces a boolean output"""
  789. bl_idname = "UtilityCompare"
  790. bl_label = "Compare"
  791. bl_icon = "NODE"
  792. initialized : bpy.props.BoolProperty(default = False)
  793. def init(self, context):
  794. self.inputs.new("WildcardSocket", "A")
  795. self.inputs.new("WildcardSocket", "B")
  796. self.outputs.new("BooleanSocket", "Result")
  797. self.initialized = True
  798. def update(self):
  799. wildcard_color = (0.0,0.0,0.0,0.0)
  800. if self.inputs['A'].is_linked == False:
  801. self.inputs['A'].color = wildcard_color
  802. if self.inputs['B'].is_linked == False:
  803. self.inputs['B'].color = wildcard_color
  804. def insert_link(self, link):
  805. if link.to_socket.identifier == self.inputs['A'].identifier:
  806. self.inputs['A'].color = from_socket.color_simple
  807. if hasattr(from_socket, "color"):
  808. self.inputs['A'].color = from_socket.color
  809. if link.to_socket.identifier == self.inputs['B'].identifier:
  810. self.inputs['B'].color = from_socket.color_simple
  811. if hasattr(from_socket, "color"):
  812. self.inputs['B'].color = from_socket.color
  813. class UtilityChoose(Node, MantisNode):
  814. """Chooses an output"""
  815. bl_idname = "UtilityChoose"
  816. bl_label = "Choose"
  817. bl_icon = "NODE"
  818. initialized : bpy.props.BoolProperty(default = False)
  819. def init(self, context):
  820. self.inputs.new("BooleanSocket", "Condition")
  821. self.inputs.new("WildcardSocket", "A")
  822. self.inputs.new("WildcardSocket", "B")
  823. self.outputs.new("WildcardSocket", "Result")
  824. self.initialized = True
  825. def update(self):
  826. wildcard_color = (0.0,0.0,0.0,0.0)
  827. if self.inputs['A'].is_linked == False:
  828. self.inputs['A'].color = wildcard_color
  829. self.outputs['Result'].color = (1.0,0.0,0.0,0.0) # red for Error
  830. if self.inputs['B'].is_linked == False:
  831. self.inputs['B'].color = wildcard_color
  832. self.outputs['Result'].color = (1.0,0.0,0.0,0.0)
  833. # if both inputs are the same color, then use that color for the result
  834. if self.inputs['A'].is_linked and self.inputs['A'].color == self.inputs['B'].color:
  835. self.outputs['Result'].color = self.inputs['A'].color
  836. #
  837. if ((self.inputs['A'].is_linked and self.inputs['B'].is_linked) and
  838. (self.inputs['A'].links[0].from_socket.bl_idname != self.inputs['B'].links[0].from_socket.bl_idname)):
  839. self.inputs['A'].color = (1.0,0.0,0.0,0.0)
  840. self.inputs['B'].color = (1.0,0.0,0.0,0.0)
  841. self.outputs['Result'].color = (1.0,0.0,0.0,0.0)
  842. def insert_link(self, link):
  843. if link.to_socket.identifier == self.inputs['A'].identifier:
  844. self.inputs['A'].color = from_socket.color_simple
  845. if hasattr(from_socket, "color"):
  846. self.inputs['A'].color = from_socket.color
  847. if link.to_socket.identifier == self.inputs['B'].identifier:
  848. self.inputs['B'].color = from_socket.color_simple
  849. if hasattr(from_socket, "color"):
  850. self.inputs['B'].color = from_socket.color
  851. class UtilityPrint(Node, MantisNode):
  852. """A utility used to print arbitrary values."""
  853. bl_idname = "UtilityPrint"
  854. bl_label = "Print"
  855. bl_icon = "NODE"
  856. initialized : bpy.props.BoolProperty(default = False)
  857. def init(self, context):
  858. self.inputs.new("WildcardSocket", "Input")
  859. self.initialized = True