misc_nodes_ui.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. import bpy
  2. from bpy.types import Node
  3. from .base_definitions import MantisMiscNode, get_signature_from_edited_tree
  4. from .misc_nodes_socket_templates import *
  5. from .utilities.utilities import (prRed, prGreen, prPurple, prWhite,
  6. prOrange,
  7. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  8. wrapOrange,)
  9. def TellClasses():
  10. return [ InputFloatNode,
  11. InputIntNode,
  12. InputVectorNode,
  13. InputBooleanNode,
  14. InputBooleanThreeTupleNode,
  15. InputRotationOrderNode,
  16. InputTransformSpaceNode,
  17. InputStringNode,
  18. InputMatrixNode,
  19. InputThemeBoneColorSets,
  20. InputColorSetPallete,
  21. # InputGeometryNode,
  22. InputWidget,
  23. InputExistingGeometryObjectNode,
  24. InputExistingGeometryDataNode,
  25. UtilityCustomProperty,
  26. UtilityDeclareCollections,
  27. UtilityCollectionJoin,
  28. UtilityCollectionHierarchy,
  29. UtilityGeometryOfXForm,
  30. UtilityNameOfXForm,
  31. # ComposeMatrixNode,
  32. UtilityPointFromCurve,
  33. UtilityMatrixFromCurve,
  34. UtilityMatricesFromCurve,
  35. UtilityNumberOfCurveSegments,
  36. UtilityNumberOfSplines,
  37. UtilityMatrixFromCurveSegment,
  38. UtilityGetCurvePoint,
  39. UtilityGetNearestFactorOnCurve,
  40. UtilityKDChoosePoint,
  41. UtilityKDChooseXForm,
  42. UtilityMetaRigNode,
  43. UtilityBonePropertiesNode,
  44. UtilityDriverVariableNode,
  45. UtilityFCurveNode,
  46. UtilityDriverNode,
  47. UtilitySwitchNode,
  48. UtilityKeyframe,
  49. UtilityCombineThreeBoolNode,
  50. UtilityCombineVectorNode,
  51. UtilitySeparateVector,
  52. UtilityCatStringsNode,
  53. UtilityGetBoneLength,
  54. UtilityPointFromBoneMatrix,
  55. UtilitySetBoneLength,
  56. UtilityMatrixSetLocation,
  57. UtilityMatrixGetLocation,
  58. UtilityMatrixFromXForm,
  59. UtilityAxesFromMatrix,
  60. UtilityBoneMatrixHeadTailFlip,
  61. UtilityMatrixTransform,
  62. UtilityMatrixInvert,
  63. UtilityMatrixCompose,
  64. UtilityMatrixAlignRoll,
  65. UtilityTransformationMatrix,
  66. UtilitySetBoneMatrixTail,
  67. UtilityIntToString,
  68. UtilityArrayGet,
  69. UtilityArrayLength,
  70. #
  71. UtilityCompare,
  72. UtilityChoose,
  73. # for testing
  74. UtilityPrint,
  75. ]
  76. def default_traverse(self,socket):
  77. return None
  78. def socket_data_from_collection_paths(root_data, root_name, path, socket_data):
  79. # so we need to 'push' the socket names and their paths in order
  80. # socket_data is a list of tuples of ( name, path, )
  81. for key, value in root_data.items():
  82. path.append(key)
  83. socket_data.append( (key, path))
  84. if hasattr(value , 'items'):
  85. socket_data = socket_data_from_collection_paths(value, key, path.copy(), socket_data)
  86. path.pop()
  87. return socket_data
  88. #################################################
  89. # Base Classes for the Misc Nodes
  90. #################################################
  91. class SimpleInputNode(MantisMiscNode):
  92. def init(self, context):
  93. super().init(context)
  94. for s in self.outputs:
  95. s.input=True # give it the property that makes it draw correctly
  96. class InputFloatNode(Node, SimpleInputNode):
  97. '''A node representing inheritance'''
  98. bl_idname = 'InputFloatNode'
  99. bl_label = "Float"
  100. mantis_node_class_name=bl_idname[:-4]
  101. socket_templates=InputFloatSockets
  102. class InputIntNode(Node, SimpleInputNode):
  103. '''A node representing inheritance'''
  104. bl_idname = 'InputIntNode'
  105. bl_label = "Integer"
  106. mantis_node_class_name=bl_idname
  107. socket_templates=InputIntSockets
  108. class InputVectorNode(Node, SimpleInputNode):
  109. '''A node representing inheritance'''
  110. bl_idname = 'InputVectorNode'
  111. bl_label = "Vector"
  112. mantis_node_class_name=bl_idname[:-4]
  113. socket_templates=InputVectorSockets
  114. class InputBooleanNode(Node, SimpleInputNode):
  115. '''A node representing inheritance'''
  116. bl_idname = 'InputBooleanNode'
  117. bl_label = "Boolean"
  118. mantis_node_class_name=bl_idname[:-4]
  119. socket_templates=InputBooleanSockets
  120. class InputBooleanThreeTupleNode(Node, SimpleInputNode):
  121. '''A node representing inheritance'''
  122. bl_idname = 'InputBooleanThreeTupleNode'
  123. bl_label = "Boolean Vector"
  124. mantis_node_class_name=bl_idname[:-4]
  125. socket_templates=InputBooleanThreeTupleSockets
  126. class InputRotationOrderNode(Node, SimpleInputNode):
  127. '''A node representing inheritance'''
  128. bl_idname = 'InputRotationOrderNode'
  129. bl_label = "Rotation Order"
  130. mantis_node_class_name=bl_idname[:-4]
  131. socket_templates=InputRotationOrderSockets
  132. class InputTransformSpaceNode(Node, SimpleInputNode):
  133. '''A node representing inheritance'''
  134. bl_idname = 'InputTransformSpaceNode'
  135. bl_label = "Transform Space"
  136. mantis_node_class_name=bl_idname[:-4]
  137. socket_templates=InputTransformSpaceSockets
  138. class InputStringNode(Node, SimpleInputNode):
  139. '''A node representing inheritance'''
  140. bl_idname = 'InputStringNode'
  141. bl_label = "String"
  142. mantis_node_class_name=bl_idname[:-4]
  143. socket_templates=InputStringSockets
  144. class InputMatrixNode(Node, SimpleInputNode):
  145. '''A node representing inheritance'''
  146. bl_idname = 'InputMatrixNode'
  147. bl_label = "Matrix"
  148. first_row : bpy.props.FloatVectorProperty(name="", size=4, default = (1.0, 0.0, 0.0, 0.0,))
  149. second_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 1.0, 0.0, 0.0,))
  150. third_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 0.0, 1.0, 0.0,))
  151. fourth_row : bpy.props.FloatVectorProperty(name="", size=4, default = (0.0, 0.0, 0.0, 1.0,))
  152. mantis_node_class_name=bl_idname[:-4]
  153. def init(self, context):
  154. self.init_sockets(InputMatrixSockets)
  155. self.initialized = True
  156. def set_matrix(self):
  157. return (self.first_row[ 0], self.first_row[ 1], self.first_row[ 2], self.first_row[ 3],
  158. self.second_row[0], self.second_row[1], self.second_row[2], self.second_row[3],
  159. self.third_row[ 0], self.third_row[ 1], self.third_row[ 2], self.third_row[ 3],
  160. self.fourth_row[0], self.fourth_row[1], self.fourth_row[2], self.fourth_row[3],)
  161. def draw_buttons(self, context, layout):
  162. # return
  163. layout.prop(self, "first_row")
  164. layout.prop(self, "second_row")
  165. layout.prop(self, "third_row")
  166. layout.prop(self, "fourth_row")
  167. def update(self):
  168. mat_sock = self.outputs[0]
  169. mat_sock.default_value = self.set_matrix()
  170. class InputThemeBoneColorSets(Node, MantisMiscNode):
  171. """Displays the theme's colors."""
  172. bl_idname = "InputThemeBoneColorSets"
  173. bl_label = "Theme Colors"
  174. bl_width_min=280
  175. mantis_node_class_name=bl_idname
  176. def init(self, context):
  177. for i in range(20):
  178. s = self.outputs.new("ColorSetDisplaySocket", f"Color {str(i).zfill(2)}")
  179. s.color_index=i
  180. self.initialized = True
  181. class InputColorSetPallete(Node, MantisMiscNode):
  182. """Displays the theme's colors."""
  183. bl_idname = "InputColorSetPallete"
  184. bl_label = "Color Pallete"
  185. bl_width_min=280
  186. mantis_node_class_name=bl_idname
  187. def init(self, context):
  188. self.initialized = True
  189. def draw_buttons(self, context, layout):
  190. ops_props = layout.operator('mantis.color_pallete_socket_add')
  191. ops_props.tree_invoked = self.id_data.name
  192. ops_props.node_invoked = self.name
  193. class UtilityMatrixFromCurve(Node, MantisMiscNode):
  194. """Gets a matrix from a curve."""
  195. bl_idname = "UtilityMatrixFromCurve"
  196. bl_label = "Matrix from Curve"
  197. mantis_node_class_name=bl_idname
  198. socket_templates=MatrixFromCurveSockets
  199. def display_update(self, parsed_tree, context):
  200. # first we'll checc if the mode is connected to a noodle, should be rare
  201. mode = 'DIVISIONS'
  202. if self.inputs['Mode'].is_linked:
  203. if mantis_node := parsed_tree.get(get_signature_from_edited_tree(self, context)):
  204. mode = mantis_node.evaluate_input("Mode")
  205. else: # We can use it directly, this is a little faster.
  206. mode = self.inputs['Mode'].default_value
  207. # We just hide/show based on the selected mode.
  208. for template in self.__class__.socket_templates:
  209. if mode == 'DIVISIONS':
  210. if template.category == 'factor':
  211. self.inputs[template.name].hide=True
  212. elif template.category == 'divisions':
  213. self.inputs[template.name].hide=False
  214. elif template.category == 'factor_tail':
  215. self.inputs[template.name].hide=True
  216. elif mode == 'FACTOR':
  217. if template.category == 'factor':
  218. self.inputs[template.name].hide=False
  219. elif template.category == 'divisions':
  220. self.inputs[template.name].hide=True
  221. elif template.category == 'factor_tail':
  222. self.inputs[template.name].hide=True
  223. elif mode == 'FACTOR_WITH_TAIL':
  224. if template.category == 'factor':
  225. self.inputs[template.name].hide=False
  226. elif template.category == 'divisions':
  227. self.inputs[template.name].hide=True
  228. elif template.category == 'factor_tail':
  229. self.inputs[template.name].hide=False
  230. class UtilityPointFromCurve(Node, MantisMiscNode):
  231. """Gets a point from a curve."""
  232. bl_idname = "UtilityPointFromCurve"
  233. bl_label = "Point from Curve"
  234. mantis_node_class_name=bl_idname
  235. socket_templates=PointFromCurveSockets
  236. class UtilityNumberOfCurveSegments(Node, MantisMiscNode):
  237. """Tells the number of segments in a curve."""
  238. bl_idname = "UtilityNumberOfCurveSegments"
  239. bl_label = "Number of Curve Segments"
  240. mantis_node_class_name=bl_idname
  241. socket_templates=NumberOfCurveSegmentsSockets
  242. class UtilityNumberOfSplines(Node, MantisMiscNode):
  243. """Tells the number of splines in a curve."""
  244. bl_idname = "UtilityNumberOfSplines"
  245. bl_label = "Number of Splines"
  246. mantis_node_class_name=bl_idname
  247. socket_templates=NumberOfSplinesSockets
  248. class UtilityMatrixFromCurveSegment(Node, MantisMiscNode):
  249. """Gets a matrix from a curve segment."""
  250. bl_idname = "UtilityMatrixFromCurveSegment"
  251. bl_label = "Matrix from Curve Segment"
  252. mantis_node_class_name=bl_idname
  253. socket_templates=MatrixFromCurveSegmentSockets
  254. class UtilityGetCurvePoint(Node, MantisMiscNode):
  255. bl_idname = 'UtilityGetCurvePoint'
  256. bl_label = "Control Point from Curve"
  257. mantis_node_class_name=bl_idname
  258. socket_templates=GetCurvePointSockets
  259. def display_update(self, parsed_tree, context):
  260. self.outputs["Point"].hide=False
  261. self.outputs["Left Handle"].hide=True
  262. self.outputs["Right Handle"].hide=True
  263. spline_index = self.inputs['Spline Index'].default_value
  264. index = self.inputs['Index'].default_value
  265. curve = self.inputs['Curve'].default_value
  266. if self.inputs['Spline Index'].is_linked or self.inputs['Index'].is_linked \
  267. or self.inputs['Curve'].is_linked:
  268. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  269. spline_index = mantis_node.evaluate_input("Spline Index")
  270. index = mantis_node.evaluate_input("Index")
  271. curve = mantis_node.evaluate_input("Curve")
  272. if curve := bpy.data.objects.get(curve):
  273. if curve.type != "CURVE":
  274. self.outputs["Point"].hide=True
  275. spline = curve.data.splines[spline_index]
  276. if spline.type == 'BEZIER':
  277. self.outputs["Left Handle"].hide=False
  278. self.outputs["Right Handle"].hide=False
  279. class UtilityMatricesFromCurve(Node, MantisMiscNode):
  280. """Gets a matrix from a curve."""
  281. bl_idname = "UtilityMatricesFromCurve"
  282. bl_label = "Matrices from Curve"
  283. mantis_node_class_name=bl_idname
  284. socket_templates=MatricesFromCurveSockets
  285. def display_update_choose_nearest(self, parsed_tree, context):
  286. number_of_points = self.inputs['Number to Find'].default_value
  287. if self.inputs["Number to Find"].is_linked:
  288. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  289. number_of_points = mantis_node.evaluate_input("Number to Find")
  290. elif number_of_points == 0:
  291. self.inputs['Number to Find'].default_value=1
  292. if number_of_points > 1:
  293. # then we need to make it an array out
  294. self.outputs.display_shape = 'SQUARE_DOT'
  295. else:
  296. self.outputs.display_shape = 'CIRCLE'
  297. class UtilityGetNearestFactorOnCurve(Node, MantisMiscNode):
  298. bl_idname = 'UtilityGetNearestFactorOnCurve'
  299. bl_label = "Get Factor on Curve at Point"
  300. mantis_node_class_name=bl_idname
  301. socket_templates=GetNearestFactorOnCurveSockets
  302. class UtilityKDChoosePoint(Node, MantisMiscNode):
  303. """Chooses the nearest point with a KD Tree."""
  304. bl_idname = "UtilityKDChoosePoint"
  305. bl_label = "Choose Nearest Point"
  306. mantis_node_class_name=bl_idname
  307. socket_templates=KDChoosePointSockets
  308. def display_update(self, parsed_tree, context):
  309. display_update_choose_nearest(self, parsed_tree, context)
  310. class UtilityKDChooseXForm(Node, MantisMiscNode):
  311. """Chooses the nearest xForm with a KD Tree."""
  312. bl_idname = "UtilityKDChooseXForm"
  313. bl_label = "Choose Nearest xForm"
  314. mantis_node_class_name=bl_idname
  315. socket_templates=KDChooseXFormSockets
  316. def display_update(self, parsed_tree, context):
  317. display_update_choose_nearest(self, parsed_tree, context)
  318. class UtilityMetaRigNode(Node, MantisMiscNode):
  319. """Gets a matrix from a meta-rig bone."""
  320. bl_idname = "UtilityMetaRig"
  321. bl_label = "Meta-Rig"
  322. armature:bpy.props.StringProperty()
  323. pose_bone:bpy.props.StringProperty()
  324. mantis_node_class_name=bl_idname
  325. socket_templates=MetaRigSockets
  326. def display_update(self, parsed_tree, context):
  327. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  328. using_overlay = self.inputs['Meta-Armature'].use_overlay
  329. if mantis_node:
  330. self.armature= mantis_node.evaluate_input("Meta-Armature")
  331. self.pose_bone= mantis_node.evaluate_input("Meta-Bone")
  332. if not self.armature and not using_overlay:
  333. self.inputs["Meta-Bone"].hide=True
  334. else:
  335. self.inputs["Meta-Bone"].hide=False
  336. if self.inputs["Meta-Armature"].is_linked:
  337. self.inputs["Meta-Armature"].search_prop = None
  338. if self.inputs["Meta-Bone"].is_linked:
  339. self.inputs["Meta-Bone"].search_prop = None
  340. class UtilityBonePropertiesNode(Node, MantisMiscNode):
  341. """Provides as sockets strings identifying bone transform properties."""
  342. bl_idname = "UtilityBoneProperties"
  343. bl_label = "Bone Properties"
  344. mantis_node_class_name=bl_idname
  345. socket_templates=BonePropertiesSockets
  346. class UtilityDriverVariableNode(Node, MantisMiscNode):
  347. """Creates a variable for use in a driver."""
  348. bl_idname = "UtilityDriverVariable"
  349. bl_label = "Driver Variable"
  350. mantis_node_class_name=bl_idname
  351. socket_templates=DriverVariableSockets
  352. def display_update(self, parsed_tree, context):
  353. if self.inputs["Variable Type"].is_linked:
  354. if context.space_data:
  355. node_tree = context.space_data.path[0].node_tree
  356. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  357. if mantis_node:
  358. driver_type = mantis_node.evaluate_input("Variable Type")
  359. else:
  360. driver_type = self.inputs[0].default_value
  361. if driver_type == 'SINGLE_PROP':
  362. self.inputs[1].hide = False
  363. self.inputs[2].hide = False
  364. self.inputs[3].hide = False
  365. self.inputs[4].hide = False
  366. self.inputs[5].hide = False
  367. self.inputs[6].hide = True
  368. elif driver_type == 'LOC_DIFF':
  369. self.inputs[1].hide = True
  370. self.inputs[2].hide = True
  371. self.inputs[3].hide = True
  372. self.inputs[4].hide = True
  373. self.inputs[5].hide = False
  374. self.inputs[6].hide = False
  375. elif driver_type == 'ROTATION_DIFF':
  376. self.inputs[1].hide = True
  377. self.inputs[2].hide = True
  378. self.inputs[3].hide = True
  379. self.inputs[4].hide = False
  380. self.inputs[5].hide = False
  381. self.inputs[6].hide = False
  382. elif driver_type == 'TRANSFORMS':
  383. self.inputs[1].hide = True
  384. self.inputs[2].hide = True
  385. self.inputs[3].hide = False
  386. self.inputs[4].hide = False
  387. self.inputs[5].hide = False
  388. self.inputs[6].hide = True
  389. # TODO: make a way to edit the fCurve directly.
  390. # I had a working version of this in the past, but it required doing sinful things like
  391. # keeping track of the RAM address of the window.
  392. class UtilityFCurveNode(Node, MantisMiscNode):
  393. """Creates an fCurve for use with a driver."""
  394. bl_idname = "UtilityFCurve"
  395. bl_label = "fCurve"
  396. mantis_node_class_name=bl_idname
  397. socket_templates=FCurveSockets
  398. use_kf_nodes : bpy.props.BoolProperty(default=True)
  399. class UtilityDriverNode(Node, MantisMiscNode):
  400. """Represents a Driver relationship"""
  401. bl_idname = "UtilityDriver"
  402. bl_label = "Driver"
  403. mantis_node_class_name=bl_idname
  404. socket_templates=DriverSockets
  405. def display_update(self, parsed_tree, context):
  406. if not self.inputs["Driver Type"].is_linked:
  407. dType = self.inputs["Driver Type"].default_value
  408. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  409. if mantis_node:
  410. dType = mantis_node.evaluate_input("Driver Type")
  411. if dType == 'SCRIPTED':
  412. self.inputs["Expression"].hide = False
  413. else:
  414. self.inputs["Expression"].hide = True
  415. def draw_buttons(self, context, layout):
  416. # return
  417. layout.operator( 'mantis.driver_node_add_variable' )
  418. if (len(self.inputs) > 3):
  419. layout.operator( 'mantis.driver_node_remove_variable' )
  420. class UtilitySwitchNode(Node, MantisMiscNode):
  421. """Represents a switch relationship between one driver property and one or more driven properties."""
  422. bl_idname = "UtilitySwitch"
  423. bl_label = "Switch"
  424. mantis_node_class_name=bl_idname
  425. socket_templates=SwitchSockets
  426. class UtilityCombineThreeBoolNode(Node, MantisMiscNode):
  427. """Combines three booleans into a three-bool."""
  428. bl_idname = "UtilityCombineThreeBool"
  429. bl_label = "CombineThreeBool"
  430. mantis_node_class_name=bl_idname
  431. socket_templates=CombineThreeBoolSockets
  432. class UtilityCombineVectorNode(Node, MantisMiscNode):
  433. """Combines three floats into a vector."""
  434. bl_idname = "UtilityCombineVector"
  435. bl_label = "CombineVector"
  436. mantis_node_class_name=bl_idname
  437. socket_templates=CombineVectorSockets
  438. class UtilitySeparateVector(Node, MantisMiscNode):
  439. """Separates a vector into three floats"""
  440. bl_idname = "UtilitySeparateVector"
  441. bl_label = "Separate Vector"
  442. mantis_node_class_name=bl_idname
  443. socket_templates=SeparateVectorSockets
  444. class UtilityCatStringsNode(Node, MantisMiscNode):
  445. """Combines multiple strings into one."""
  446. bl_idname = "UtilityCatStrings"
  447. bl_label = "Join Strings"
  448. mantis_node_class_name=bl_idname
  449. socket_templates=CatStringsSockets
  450. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  451. if self.label:
  452. return self.label
  453. if self.outputs['OutputString'].display_text:
  454. return self.outputs['OutputString'].display_text
  455. return self.name
  456. def display_update(self, parsed_tree, context):
  457. if context.space_data:
  458. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  459. self.inputs['String_1'].display_text = ""
  460. self.inputs['String_2'].display_text = ""
  461. self.outputs['OutputString'].display_text = ""
  462. if mantis_node:
  463. try:
  464. self.inputs['String_1'].display_text = a = mantis_node.evaluate_input("String_1")
  465. self.inputs['String_2'].display_text = b = mantis_node.evaluate_input("String_2")
  466. # cat the strings here, since the node may not have run yet.
  467. self.outputs['OutputString'].display_text = a+b
  468. except KeyError:
  469. return # the tree isn't ready yet.
  470. class InputWidget(Node, MantisMiscNode):
  471. """Fetches a Widget from the Widget Library."""
  472. bl_idname = "InputWidget"
  473. bl_label = "Widget"
  474. mantis_node_class_name=bl_idname
  475. socket_templates=InputWidgetSockets
  476. class InputExistingGeometryObjectNode(Node, MantisMiscNode):
  477. """Represents an existing geometry object from within the scene."""
  478. bl_idname = "InputExistingGeometryObject"
  479. bl_label = "Existing Object"
  480. mantis_node_class_name=bl_idname
  481. # We want Mantis to import widgets and stuff, so we hold a reference to the object
  482. object_reference : bpy.props.PointerProperty(type=bpy.types.Object,)
  483. socket_templates=InputExistingGeometryObjectSockets
  484. def display_update(self, parsed_tree, context):
  485. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  486. if mantis_node: # this is done here so I don't have to define yet another custom socket.
  487. self.object_reference = bpy.data.objects.get(mantis_node.evaluate_input("Name"))
  488. # TODO: maybe I should hold a data reference here, too.
  489. # but it is complicated by the fact that Mantis does not distinguish b/tw geo types
  490. class InputExistingGeometryDataNode(Node, MantisMiscNode):
  491. """Represents a mesh or curve datablock from the scene."""
  492. bl_idname = "InputExistingGeometryData"
  493. bl_label = "Existing Geometry"
  494. mantis_node_class_name=bl_idname
  495. socket_templates=InputExistingGeometrySockets
  496. class UtilityCustomProperty(Node, MantisMiscNode):
  497. """A declaration of a custom property."""
  498. bl_idname = "UtilityCustomProperty"
  499. bl_label = "Custom Property"
  500. bl_icon = "NODE"
  501. mantis_node_class_name=bl_idname
  502. socket_templates=UtilityCustomPropertySockets
  503. def display_update(self, parsed_tree, context):
  504. prop_type = self.inputs['Type'].default_value
  505. if context: # I need to be able to call this without context in versioing.
  506. if mantis_node := parsed_tree.get(get_signature_from_edited_tree(self, context)):
  507. # this is done here so I don't have to define yet another custom socket.
  508. prop_type = mantis_node.evaluate_input("Type")
  509. visible_categories = ['general']
  510. match prop_type:
  511. case 'STRING': visible_categories.append('string')
  512. case 'BOOL': visible_categories.append('bool')
  513. case 'INT': visible_categories.append('int')
  514. case 'FLOAT': visible_categories.append('float')
  515. case 'VECTOR': visible_categories.append('vector')
  516. for i, socket_template in enumerate(UtilityCustomPropertySockets):
  517. if socket_template.is_input == False: continue
  518. if socket_template.category in visible_categories:
  519. self.inputs[i].hide = False
  520. else:
  521. self.inputs[i].hide = True
  522. if prop_type == "VECTOR":
  523. for i in range (4): # keep the min/max open
  524. self.inputs[i+3].hide=False
  525. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  526. if self.label:
  527. return self.label
  528. if self.inputs['Name'].display_text:
  529. return self.inputs['Name'].display_text
  530. return self.name
  531. class UtilityDeclareCollections(Node, MantisMiscNode):
  532. """A utility used to declare bone collections."""
  533. bl_idname = "UtilityDeclareCollections"
  534. bl_label = "Collections"
  535. bl_icon = "NODE"
  536. bl_width_min = 320
  537. mantis_node_class_name=bl_idname
  538. # Here is the layout of the data:
  539. # nested dicts of key:dict ( key = name, dict = children)
  540. # the 'leaf nodes' are empty dicts
  541. # we'll store it as a JSON string in order to make it a bpy.props
  542. # and still have the ability to use it as a dict and save it
  543. # TODO: check and see if these strings have a character limit
  544. collection_declarations : bpy.props.StringProperty(default="")
  545. def update_interface(self):
  546. from .utilities.utilities import get_socket_maps, do_relink
  547. # we need to do dynamic stuff here like with interfaces
  548. socket_map_out = get_socket_maps(self)[1]
  549. self.outputs.clear()
  550. current_data = self.read_declarations_from_json()
  551. socket_data = socket_data_from_collection_paths(current_data, self.name, [], [])
  552. for item in socket_data:
  553. full_path_name = '>'.join(item[1]+[item[0]])
  554. s = self.outputs.new('CollectionDeclarationSocket', name=item[0],identifier=full_path_name )
  555. s.collection_path = full_path_name
  556. for s in self.outputs:
  557. if s.identifier in socket_map_out.keys():
  558. do_relink(self, s, socket_map_out, 'OUTPUT')
  559. def init(self, context):
  560. self.initialized = True
  561. if self.collection_declarations == "":
  562. self.push_declarations_to_json({})
  563. def push_declarations_to_json(self, dict):
  564. import json
  565. j_str = json.dumps(dict)
  566. self.collection_declarations = j_str
  567. def read_declarations_from_json(self):
  568. import json
  569. j_data = json.loads(self.collection_declarations)
  570. return j_data
  571. def draw_buttons(self, context, layout):
  572. op_props = layout.operator('mantis.collection_add_new')
  573. op_props.socket_invoked = '' # this isn't reset between invocations
  574. # so we have to make sure to unset it when running it from the node
  575. class UtilityCollectionJoin(Node, MantisMiscNode):
  576. """A utility used to join bone collection declarations."""
  577. bl_idname = "UtilityCollectionJoin"
  578. bl_label = "Join Collection Declarations"
  579. bl_icon = "NODE"
  580. mantis_node_class_name=bl_idname
  581. socket_templates=CollectionJoinSockets
  582. class UtilityCollectionHierarchy(Node, MantisMiscNode):
  583. """A utility used to declare bone collection hierarchies."""
  584. bl_idname = "UtilityCollectionHierarchy"
  585. bl_label = "Collection Child Declaration"
  586. bl_icon = "NODE"
  587. mantis_node_class_name=bl_idname
  588. socket_templates=CollectionHierarchySockets
  589. class UtilityGeometryOfXForm(Node, MantisMiscNode):
  590. """Retrieves a mesh or curve datablock from an xForm."""
  591. bl_idname = "UtilityGeometryOfXForm"
  592. bl_label = "Geometry of xForm"
  593. mantis_node_class_name=bl_idname
  594. socket_templates=GeometryOfXFormSockets
  595. class UtilityNameOfXForm(Node, MantisMiscNode):
  596. """Retrieves the name of a xForm."""
  597. bl_idname = "UtilityNameOfXForm"
  598. bl_label = "Name of xForm"
  599. mantis_node_class_name=bl_idname
  600. socket_templates=NameOfXFormSockets
  601. class UtilityGetBoneLength(Node, MantisMiscNode):
  602. """Returns the length of the bone from its matrix."""
  603. bl_idname = "UtilityGetBoneLength"
  604. bl_label = "Get Bone Length"
  605. mantis_node_class_name=bl_idname
  606. socket_templates=GetBoneLengthSockets
  607. # TODO: make it work with BBones!
  608. class UtilityPointFromBoneMatrix(Node, MantisMiscNode):
  609. """Returns a point representing the location along a bone, given a matrix representing that bone's shape."""
  610. bl_idname = "UtilityPointFromBoneMatrix"
  611. bl_label = "Point from Bone Matrix"
  612. mantis_node_class_name=bl_idname
  613. socket_templates=PointFromBoneMatrixSockets
  614. class UtilitySetBoneLength(Node, MantisMiscNode):
  615. """Sets the length of a bone matrix."""
  616. bl_idname = "UtilitySetBoneLength"
  617. bl_label = "Set Bone Matrix Length"
  618. mantis_node_class_name=bl_idname
  619. socket_templates=SetBoneLengthSockets
  620. # TODO: more keyframe types should be supported in the future.
  621. # Some of the code that can do this is commented out here until I can implement it properly.
  622. class UtilityKeyframe(Node, MantisMiscNode):
  623. """A keyframe for a FCurve"""
  624. bl_idname = "UtilityKeyframe"
  625. bl_label = "KeyFrame"
  626. mantis_node_class_name=bl_idname
  627. socket_templates=KeyframeSockets
  628. # TODO: I'll implement this when I finish adding the extra features to the Keyframe node
  629. # def display_update(self, parsed_tree, context):
  630. # if context.space_data:
  631. # mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  632. # if mantis_node.evaluate_input("Interpolation") in ["CONSTANT", "LINEAR"]:
  633. # for inp in self.inputs[1:6]:
  634. # inp.hide = True
  635. # else:
  636. # if mantis_node.evaluate_input("Left Handle Type") in ["FREE", "ALIGNED"]:
  637. # for inp in self.inputs[1:6]:
  638. # inp.hide = False
  639. class UtilityBoneMatrixHeadTailFlip(Node, MantisMiscNode):
  640. """Flips a bone matrix so that the head is where the tail was and visa versa."""
  641. bl_idname = "UtilityBoneMatrixHeadTailFlip"
  642. bl_label = "Flip Head/Tail"
  643. mantis_node_class_name=bl_idname
  644. socket_templates=BoneMatrixHeadTailFlipSockets
  645. class UtilityMatrixTransform(Node, MantisMiscNode):
  646. """Transforms a matrix by another."""
  647. bl_idname = "UtilityMatrixTransform"
  648. bl_label = "Multiply Matrices"
  649. mantis_node_class_name=bl_idname
  650. socket_templates=MatrixTransformSockets
  651. class UtilityMatrixInvert(Node, MantisMiscNode):
  652. """Inverts an invertable matrix, otherwise throws an error."""
  653. bl_idname = "UtilityMatrixInvert"
  654. bl_label = "Invert Matrix"
  655. mantis_node_class_name=bl_idname
  656. socket_templates=MatrixInvertSockets
  657. class UtilityMatrixCompose(Node, MantisMiscNode):
  658. """Inverts an invertable matrix, otherwise throws an error."""
  659. bl_idname = "UtilityMatrixCompose"
  660. bl_label = "Compose Bone Matrix"
  661. mantis_node_class_name=bl_idname
  662. socket_templates=MatrixComposeSockets
  663. def display_update(self, parsed_tree, context):
  664. # first we'll checc if the mode is connected to a noodle, should be rare
  665. mode = 'BASIS'
  666. if self.inputs['Mode'].is_linked:
  667. if mantis_node := parsed_tree.get(get_signature_from_edited_tree(self, context)):
  668. mode = mantis_node.evaluate_input("Mode")
  669. else: # We can use it directly, this is a little faster.
  670. mode = self.inputs['Mode'].default_value
  671. # We just hide/show based on the selected mode.
  672. for template in self.__class__.socket_templates:
  673. if mode == 'BASIS':
  674. if template.category == 'transform':
  675. self.inputs[template.name].hide=True
  676. elif template.category == 'axes':
  677. self.inputs[template.name].hide=False
  678. elif mode == 'CHANNELS':
  679. if template.category == 'transform':
  680. self.inputs[template.name].hide=False
  681. elif template.category == 'axes':
  682. self.inputs[template.name].hide=True
  683. if template.category == 'always_show':
  684. self.inputs[template.name].hide=False
  685. class UtilityMatrixAlignRoll(Node, MantisMiscNode):
  686. """Aligns the Z axis of a matrix to point in a direction."""
  687. bl_idname = "UtilityMatrixAlignRoll"
  688. bl_label = "Align Matrix Roll"
  689. mantis_node_class_name=bl_idname
  690. socket_templates=MatrixAlignRollSockets
  691. class UtilityMatrixSetLocation(Node, MantisMiscNode):
  692. """Sets a matrix's location."""
  693. bl_idname = "UtilityMatrixSetLocation"
  694. bl_label = "Set Matrix Location"
  695. mantis_node_class_name=bl_idname
  696. socket_templates=MatrixSetLocationSockets
  697. class UtilityMatrixGetLocation(Node, MantisMiscNode):
  698. """Gets a matrix's location."""
  699. bl_idname = "UtilityMatrixGetLocation"
  700. bl_label = "Get Matrix Location"
  701. mantis_node_class_name=bl_idname
  702. socket_templates=MatrixGetLocationSockets
  703. class UtilityTransformationMatrix(Node, MantisMiscNode):
  704. """Constructs a matrix representing a transformation"""
  705. bl_idname = "UtilityTransformationMatrix"
  706. bl_label = "Transformation Matrix"
  707. mantis_node_class_name=bl_idname
  708. socket_templates=TransformationMatrixSockets
  709. def display_update(self, parsed_tree, context):
  710. operation = self.inputs['Operation'].default_value
  711. if self.inputs['Operation'].is_linked:
  712. if context.space_data:
  713. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  714. operation = mantis_node.evaluate_input("Operation")
  715. if operation in ["ROTATE_AXIS_ANGLE", "SCALE"]:
  716. self.inputs["Vector"].hide = False
  717. self.inputs["W"].hide = False
  718. if operation in ["TRANSLATE"]:
  719. self.inputs["Vector"].hide = False
  720. self.inputs["W"].hide = True
  721. # Blender calculates bone roll this way...
  722. # https://projects.blender.org/blender/blender/src/commit/dd209221675ac7b62ce47b7ea42f15cbe34a6035/source/blender/editors/armature/armature_edit.cc#L281
  723. # 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
  724. # in the edit bone.
  725. # So instead, we need to avoid calculating the roll for now.
  726. # but I want to make that its own node and add roll-recalc to this node, too.
  727. class UtilitySetBoneMatrixTail(Node, MantisMiscNode):
  728. """Constructs a matrix representing a transformation"""
  729. bl_idname = "UtilitySetBoneMatrixTail"
  730. bl_label = "Set Bone Matrix Tail"
  731. mantis_node_class_name=bl_idname
  732. socket_templates=SetBoneMatrixTailSockets
  733. class UtilityMatrixFromXForm(Node, MantisMiscNode):
  734. """Returns the matrix of the given xForm node."""
  735. bl_idname = "UtilityMatrixFromXForm"
  736. bl_label = "Matrix of xForm"
  737. mantis_node_class_name=bl_idname
  738. socket_templates=MatrixFromXFormSockets
  739. class UtilityAxesFromMatrix(Node, MantisMiscNode):
  740. """Returns the axes of the matrix."""
  741. bl_idname = "UtilityAxesFromMatrix"
  742. bl_label = "Axes of Matrix"
  743. mantis_node_class_name=bl_idname
  744. socket_templates=AxesFromMatrixSockets
  745. class UtilityIntToString(Node, MantisMiscNode):
  746. """Converts a number to a string"""
  747. bl_idname = "UtilityIntToString"
  748. bl_label = "Number String"
  749. mantis_node_class_name=bl_idname
  750. socket_templates=IntToStringSockets
  751. class UtilityArrayGet(Node, MantisMiscNode):
  752. """Gets a value from an array at a specified index."""
  753. bl_idname = "UtilityArrayGet"
  754. bl_label = "Array Get"
  755. bl_icon = "NODE"
  756. mantis_node_class_name=bl_idname
  757. socket_templates=ArrayGetSockets
  758. def update(self):
  759. wildcard_color = (0.0,0.0,0.0,0.0)
  760. if self.inputs['Array'].is_linked == False:
  761. self.inputs['Array'].color = wildcard_color
  762. self.outputs['Output'].color = wildcard_color
  763. def insert_link(self, link):
  764. super().insert_link(link)
  765. if link.to_socket.identifier == self.inputs['Array'].identifier:
  766. from_socket = link.from_socket
  767. if hasattr(from_socket, "color"):
  768. self.inputs['Array'].color = from_socket.color
  769. self.outputs['Output'].color = from_socket.color
  770. class UtilityArrayLength(Node, MantisMiscNode):
  771. """Gets The length of an array."""
  772. bl_idname = "UtilityArrayLength"
  773. bl_label = "Array Length"
  774. bl_icon = "NODE"
  775. mantis_node_class_name=bl_idname
  776. socket_templates=UtilityArrayLengthSockets
  777. def update(self):
  778. wildcard_color = (0.0,0.0,0.0,0.0)
  779. if self.inputs['Array'].is_linked == False:
  780. self.inputs['Array'].color = wildcard_color
  781. def insert_link(self, link):
  782. super().insert_link(link)
  783. if link.to_socket.identifier == self.inputs['Array'].identifier:
  784. from_socket = link.from_socket
  785. if hasattr(from_socket, "color"):
  786. self.inputs['Array'].color = from_socket.color
  787. class UtilityCompare(Node, MantisMiscNode):
  788. """Compares two inputs and produces a boolean output"""
  789. bl_idname = "UtilityCompare"
  790. bl_label = "Compare"
  791. mantis_node_class_name=bl_idname
  792. socket_templates=CompareSockets
  793. def update(self):
  794. wildcard_color = (0.0,0.0,0.0,0.0)
  795. if self.inputs['A'].is_linked == False:
  796. self.inputs['A'].color = wildcard_color
  797. if self.inputs['B'].is_linked == False:
  798. self.inputs['B'].color = wildcard_color
  799. def insert_link(self, link):
  800. super().insert_link(link)
  801. if link.to_socket.identifier == self.inputs['A'].identifier:
  802. self.inputs['A'].color = link.from_socket.color_simple
  803. if hasattr(link.from_socket, "color"):
  804. self.inputs['A'].color = link.from_socket.color
  805. if link.to_socket.identifier == self.inputs['B'].identifier:
  806. self.inputs['B'].color = link.from_socket.color_simple
  807. if hasattr(link.from_socket, "color"):
  808. self.inputs['B'].color = link.from_socket.color
  809. class UtilityChoose(Node, MantisMiscNode):
  810. """Chooses an output"""
  811. bl_idname = "UtilityChoose"
  812. bl_label = "Choose"
  813. mantis_node_class_name=bl_idname
  814. socket_templates=ChooseSockets
  815. def display_update(self, parsed_tree, context):
  816. wildcard_color = (0.0,0.0,0.0,0.0)
  817. if not self.inputs['A'].is_linked:
  818. self.inputs['A'].color = wildcard_color
  819. if not self.inputs['B'].is_linked:
  820. self.inputs['B'].color = wildcard_color
  821. self.outputs['Result'].color = wildcard_color
  822. # if both inputs are the same color, then use that color for the result
  823. if self.inputs['Condition'].is_linked:
  824. from .base_definitions import get_signature_from_edited_tree
  825. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  826. if mantis_node:
  827. condition = mantis_node.evaluate_input('Condition')
  828. else:
  829. condition = self.inputs['Condition'].default_value
  830. if condition == True:
  831. self.outputs['Result'].color = self.inputs['B'].color
  832. else:
  833. self.outputs['Result'].color = self.inputs['A'].color
  834. def insert_link(self, link):
  835. super().insert_link(link)
  836. if link.to_socket.identifier == self.inputs['A'].identifier:
  837. self.inputs['A'].color = link.from_socket.color_simple
  838. if hasattr(link.from_socket, "color"):
  839. self.inputs['A'].color = link.from_socket.color
  840. if link.to_socket.identifier == self.inputs['B'].identifier:
  841. self.inputs['B'].color = link.from_socket.color_simple
  842. if hasattr(link.from_socket, "color"):
  843. self.inputs['B'].color = link.from_socket.color
  844. class UtilityPrint(Node, MantisMiscNode):
  845. """A utility used to print arbitrary values."""
  846. bl_idname = "UtilityPrint"
  847. bl_label = "Print"
  848. bl_icon = "NODE"
  849. mantis_node_class_name=bl_idname
  850. socket_templates=PrintSockets