misc_nodes_ui.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. if mantis_node:
  329. self.armature= mantis_node.evaluate_input("Meta-Armature")
  330. self.pose_bone= mantis_node.evaluate_input("Meta-Bone")
  331. if not self.armature:
  332. self.inputs["Meta-Bone"].hide=True
  333. else:
  334. self.inputs["Meta-Bone"].hide=False
  335. if self.inputs["Meta-Armature"].is_linked:
  336. self.inputs["Meta-Armature"].search_prop = None
  337. if self.inputs["Meta-Bone"].is_linked:
  338. self.inputs["Meta-Bone"].search_prop = None
  339. class UtilityBonePropertiesNode(Node, MantisMiscNode):
  340. """Provides as sockets strings identifying bone transform properties."""
  341. bl_idname = "UtilityBoneProperties"
  342. bl_label = "Bone Properties"
  343. mantis_node_class_name=bl_idname
  344. socket_templates=BonePropertiesSockets
  345. class UtilityDriverVariableNode(Node, MantisMiscNode):
  346. """Creates a variable for use in a driver."""
  347. bl_idname = "UtilityDriverVariable"
  348. bl_label = "Driver Variable"
  349. mantis_node_class_name=bl_idname
  350. socket_templates=DriverVariableSockets
  351. def display_update(self, parsed_tree, context):
  352. if self.inputs["Variable Type"].is_linked:
  353. if context.space_data:
  354. node_tree = context.space_data.path[0].node_tree
  355. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  356. if mantis_node:
  357. driver_type = mantis_node.evaluate_input("Variable Type")
  358. else:
  359. driver_type = self.inputs[0].default_value
  360. if driver_type == 'SINGLE_PROP':
  361. self.inputs[1].hide = False
  362. self.inputs[2].hide = False
  363. self.inputs[3].hide = False
  364. self.inputs[4].hide = False
  365. self.inputs[5].hide = False
  366. self.inputs[6].hide = True
  367. elif driver_type == 'LOC_DIFF':
  368. self.inputs[1].hide = True
  369. self.inputs[2].hide = True
  370. self.inputs[3].hide = True
  371. self.inputs[4].hide = True
  372. self.inputs[5].hide = False
  373. self.inputs[6].hide = False
  374. elif driver_type == 'ROTATION_DIFF':
  375. self.inputs[1].hide = True
  376. self.inputs[2].hide = True
  377. self.inputs[3].hide = True
  378. self.inputs[4].hide = False
  379. self.inputs[5].hide = False
  380. self.inputs[6].hide = False
  381. elif driver_type == 'TRANSFORMS':
  382. self.inputs[1].hide = True
  383. self.inputs[2].hide = True
  384. self.inputs[3].hide = False
  385. self.inputs[4].hide = False
  386. self.inputs[5].hide = False
  387. self.inputs[6].hide = True
  388. # TODO: make a way to edit the fCurve directly.
  389. # I had a working version of this in the past, but it required doing sinful things like
  390. # keeping track of the RAM address of the window.
  391. class UtilityFCurveNode(Node, MantisMiscNode):
  392. """Creates an fCurve for use with a driver."""
  393. bl_idname = "UtilityFCurve"
  394. bl_label = "fCurve"
  395. mantis_node_class_name=bl_idname
  396. socket_templates=FCurveSockets
  397. use_kf_nodes : bpy.props.BoolProperty(default=True)
  398. class UtilityDriverNode(Node, MantisMiscNode):
  399. """Represents a Driver relationship"""
  400. bl_idname = "UtilityDriver"
  401. bl_label = "Driver"
  402. mantis_node_class_name=bl_idname
  403. socket_templates=DriverSockets
  404. def display_update(self, parsed_tree, context):
  405. if not self.inputs["Driver Type"].is_linked:
  406. dType = self.inputs["Driver Type"].default_value
  407. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  408. if mantis_node:
  409. dType = mantis_node.evaluate_input("Driver Type")
  410. if dType == 'SCRIPTED':
  411. self.inputs["Expression"].hide = False
  412. else:
  413. self.inputs["Expression"].hide = True
  414. def draw_buttons(self, context, layout):
  415. # return
  416. layout.operator( 'mantis.driver_node_add_variable' )
  417. if (len(self.inputs) > 3):
  418. layout.operator( 'mantis.driver_node_remove_variable' )
  419. class UtilitySwitchNode(Node, MantisMiscNode):
  420. """Represents a switch relationship between one driver property and one or more driven properties."""
  421. bl_idname = "UtilitySwitch"
  422. bl_label = "Switch"
  423. mantis_node_class_name=bl_idname
  424. socket_templates=SwitchSockets
  425. class UtilityCombineThreeBoolNode(Node, MantisMiscNode):
  426. """Combines three booleans into a three-bool."""
  427. bl_idname = "UtilityCombineThreeBool"
  428. bl_label = "CombineThreeBool"
  429. mantis_node_class_name=bl_idname
  430. socket_templates=CombineThreeBoolSockets
  431. class UtilityCombineVectorNode(Node, MantisMiscNode):
  432. """Combines three floats into a vector."""
  433. bl_idname = "UtilityCombineVector"
  434. bl_label = "CombineVector"
  435. mantis_node_class_name=bl_idname
  436. socket_templates=CombineVectorSockets
  437. class UtilitySeparateVector(Node, MantisMiscNode):
  438. """Separates a vector into three floats"""
  439. bl_idname = "UtilitySeparateVector"
  440. bl_label = "Separate Vector"
  441. mantis_node_class_name=bl_idname
  442. socket_templates=SeparateVectorSockets
  443. class UtilityCatStringsNode(Node, MantisMiscNode):
  444. """Combines multiple strings into one."""
  445. bl_idname = "UtilityCatStrings"
  446. bl_label = "Join Strings"
  447. mantis_node_class_name=bl_idname
  448. socket_templates=CatStringsSockets
  449. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  450. if self.label:
  451. return self.label
  452. if self.outputs['OutputString'].display_text:
  453. return self.outputs['OutputString'].display_text
  454. return self.name
  455. def display_update(self, parsed_tree, context):
  456. if context.space_data:
  457. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  458. self.inputs['String_1'].display_text = ""
  459. self.inputs['String_2'].display_text = ""
  460. self.outputs['OutputString'].display_text = ""
  461. if mantis_node:
  462. try:
  463. self.inputs['String_1'].display_text = a = mantis_node.evaluate_input("String_1")
  464. self.inputs['String_2'].display_text = b = mantis_node.evaluate_input("String_2")
  465. # cat the strings here, since the node may not have run yet.
  466. self.outputs['OutputString'].display_text = a+b
  467. except KeyError:
  468. return # the tree isn't ready yet.
  469. class InputWidget(Node, MantisMiscNode):
  470. """Fetches a Widget from the Widget Library."""
  471. bl_idname = "InputWidget"
  472. bl_label = "Widget"
  473. mantis_node_class_name=bl_idname
  474. socket_templates=InputWidgetSockets
  475. class InputExistingGeometryObjectNode(Node, MantisMiscNode):
  476. """Represents an existing geometry object from within the scene."""
  477. bl_idname = "InputExistingGeometryObject"
  478. bl_label = "Existing Object"
  479. mantis_node_class_name=bl_idname
  480. # We want Mantis to import widgets and stuff, so we hold a reference to the object
  481. object_reference : bpy.props.PointerProperty(type=bpy.types.Object,)
  482. socket_templates=InputExistingGeometryObjectSockets
  483. def display_update(self, parsed_tree, context):
  484. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  485. if mantis_node: # this is done here so I don't have to define yet another custom socket.
  486. self.object_reference = bpy.data.objects.get(mantis_node.evaluate_input("Name"))
  487. # TODO: maybe I should hold a data reference here, too.
  488. # but it is complicated by the fact that Mantis does not distinguish b/tw geo types
  489. class InputExistingGeometryDataNode(Node, MantisMiscNode):
  490. """Represents a mesh or curve datablock from the scene."""
  491. bl_idname = "InputExistingGeometryData"
  492. bl_label = "Existing Geometry"
  493. mantis_node_class_name=bl_idname
  494. socket_templates=InputExistingGeometrySockets
  495. class UtilityCustomProperty(Node, MantisMiscNode):
  496. """A declaration of a custom property."""
  497. bl_idname = "UtilityCustomProperty"
  498. bl_label = "Custom Property"
  499. bl_icon = "NODE"
  500. mantis_node_class_name=bl_idname
  501. socket_templates=UtilityCustomPropertySockets
  502. def display_update(self, parsed_tree, context):
  503. prop_type = self.inputs['Type'].default_value
  504. if context: # I need to be able to call this without context in versioing.
  505. if mantis_node := parsed_tree.get(get_signature_from_edited_tree(self, context)):
  506. # this is done here so I don't have to define yet another custom socket.
  507. prop_type = mantis_node.evaluate_input("Type")
  508. visible_categories = ['general']
  509. match prop_type:
  510. case 'STRING': visible_categories.append('string')
  511. case 'BOOL': visible_categories.append('bool')
  512. case 'INT': visible_categories.append('int')
  513. case 'FLOAT': visible_categories.append('float')
  514. case 'VECTOR': visible_categories.append('vector')
  515. for i, socket_template in enumerate(UtilityCustomPropertySockets):
  516. if socket_template.is_input == False: continue
  517. if socket_template.category in visible_categories:
  518. self.inputs[i].hide = False
  519. else:
  520. self.inputs[i].hide = True
  521. if prop_type == "VECTOR":
  522. for i in range (4): # keep the min/max open
  523. self.inputs[i+3].hide=False
  524. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  525. if self.label:
  526. return self.label
  527. if self.inputs['Name'].display_text:
  528. return self.inputs['Name'].display_text
  529. return self.name
  530. class UtilityDeclareCollections(Node, MantisMiscNode):
  531. """A utility used to declare bone collections."""
  532. bl_idname = "UtilityDeclareCollections"
  533. bl_label = "Collections"
  534. bl_icon = "NODE"
  535. bl_width_min = 320
  536. mantis_node_class_name=bl_idname
  537. # Here is the layout of the data:
  538. # nested dicts of key:dict ( key = name, dict = children)
  539. # the 'leaf nodes' are empty dicts
  540. # we'll store it as a JSON string in order to make it a bpy.props
  541. # and still have the ability to use it as a dict and save it
  542. # TODO: check and see if these strings have a character limit
  543. collection_declarations : bpy.props.StringProperty(default="")
  544. def update_interface(self):
  545. from .utilities.utilities import get_socket_maps, do_relink
  546. # we need to do dynamic stuff here like with interfaces
  547. socket_map_out = get_socket_maps(self)[1]
  548. self.outputs.clear()
  549. current_data = self.read_declarations_from_json()
  550. socket_data = socket_data_from_collection_paths(current_data, self.name, [], [])
  551. for item in socket_data:
  552. full_path_name = '>'.join(item[1]+[item[0]])
  553. s = self.outputs.new('CollectionDeclarationSocket', name=item[0],identifier=full_path_name )
  554. s.collection_path = full_path_name
  555. for s in self.outputs:
  556. if s.identifier in socket_map_out.keys():
  557. do_relink(self, s, socket_map_out, 'OUTPUT')
  558. def init(self, context):
  559. self.initialized = True
  560. if self.collection_declarations == "":
  561. self.push_declarations_to_json({})
  562. def push_declarations_to_json(self, dict):
  563. import json
  564. j_str = json.dumps(dict)
  565. self.collection_declarations = j_str
  566. def read_declarations_from_json(self):
  567. import json
  568. j_data = json.loads(self.collection_declarations)
  569. return j_data
  570. def draw_buttons(self, context, layout):
  571. op_props = layout.operator('mantis.collection_add_new')
  572. op_props.socket_invoked = '' # this isn't reset between invocations
  573. # so we have to make sure to unset it when running it from the node
  574. class UtilityCollectionJoin(Node, MantisMiscNode):
  575. """A utility used to join bone collection declarations."""
  576. bl_idname = "UtilityCollectionJoin"
  577. bl_label = "Join Collection Declarations"
  578. bl_icon = "NODE"
  579. mantis_node_class_name=bl_idname
  580. socket_templates=CollectionJoinSockets
  581. class UtilityCollectionHierarchy(Node, MantisMiscNode):
  582. """A utility used to declare bone collection hierarchies."""
  583. bl_idname = "UtilityCollectionHierarchy"
  584. bl_label = "Collection Child Declaration"
  585. bl_icon = "NODE"
  586. mantis_node_class_name=bl_idname
  587. socket_templates=CollectionHierarchySockets
  588. class UtilityGeometryOfXForm(Node, MantisMiscNode):
  589. """Retrieves a mesh or curve datablock from an xForm."""
  590. bl_idname = "UtilityGeometryOfXForm"
  591. bl_label = "Geometry of xForm"
  592. mantis_node_class_name=bl_idname
  593. socket_templates=GeometryOfXFormSockets
  594. class UtilityNameOfXForm(Node, MantisMiscNode):
  595. """Retrieves the name of a xForm."""
  596. bl_idname = "UtilityNameOfXForm"
  597. bl_label = "Name of xForm"
  598. mantis_node_class_name=bl_idname
  599. socket_templates=NameOfXFormSockets
  600. class UtilityGetBoneLength(Node, MantisMiscNode):
  601. """Returns the length of the bone from its matrix."""
  602. bl_idname = "UtilityGetBoneLength"
  603. bl_label = "Get Bone Length"
  604. mantis_node_class_name=bl_idname
  605. socket_templates=GetBoneLengthSockets
  606. # TODO: make it work with BBones!
  607. class UtilityPointFromBoneMatrix(Node, MantisMiscNode):
  608. """Returns a point representing the location along a bone, given a matrix representing that bone's shape."""
  609. bl_idname = "UtilityPointFromBoneMatrix"
  610. bl_label = "Point from Bone Matrix"
  611. mantis_node_class_name=bl_idname
  612. socket_templates=PointFromBoneMatrixSockets
  613. class UtilitySetBoneLength(Node, MantisMiscNode):
  614. """Sets the length of a bone matrix."""
  615. bl_idname = "UtilitySetBoneLength"
  616. bl_label = "Set Bone Matrix Length"
  617. mantis_node_class_name=bl_idname
  618. socket_templates=SetBoneLengthSockets
  619. # TODO: more keyframe types should be supported in the future.
  620. # Some of the code that can do this is commented out here until I can implement it properly.
  621. class UtilityKeyframe(Node, MantisMiscNode):
  622. """A keyframe for a FCurve"""
  623. bl_idname = "UtilityKeyframe"
  624. bl_label = "KeyFrame"
  625. mantis_node_class_name=bl_idname
  626. socket_templates=KeyframeSockets
  627. # TODO: I'll implement this when I finish adding the extra features to the Keyframe node
  628. # def display_update(self, parsed_tree, context):
  629. # if context.space_data:
  630. # mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  631. # if mantis_node.evaluate_input("Interpolation") in ["CONSTANT", "LINEAR"]:
  632. # for inp in self.inputs[1:6]:
  633. # inp.hide = True
  634. # else:
  635. # if mantis_node.evaluate_input("Left Handle Type") in ["FREE", "ALIGNED"]:
  636. # for inp in self.inputs[1:6]:
  637. # inp.hide = False
  638. class UtilityBoneMatrixHeadTailFlip(Node, MantisMiscNode):
  639. """Flips a bone matrix so that the head is where the tail was and visa versa."""
  640. bl_idname = "UtilityBoneMatrixHeadTailFlip"
  641. bl_label = "Flip Head/Tail"
  642. mantis_node_class_name=bl_idname
  643. socket_templates=BoneMatrixHeadTailFlipSockets
  644. class UtilityMatrixTransform(Node, MantisMiscNode):
  645. """Transforms a matrix by another."""
  646. bl_idname = "UtilityMatrixTransform"
  647. bl_label = "Multiply Matrices"
  648. mantis_node_class_name=bl_idname
  649. socket_templates=MatrixTransformSockets
  650. class UtilityMatrixInvert(Node, MantisMiscNode):
  651. """Inverts an invertable matrix, otherwise throws an error."""
  652. bl_idname = "UtilityMatrixInvert"
  653. bl_label = "Invert Matrix"
  654. mantis_node_class_name=bl_idname
  655. socket_templates=MatrixInvertSockets
  656. class UtilityMatrixCompose(Node, MantisMiscNode):
  657. """Inverts an invertable matrix, otherwise throws an error."""
  658. bl_idname = "UtilityMatrixCompose"
  659. bl_label = "Compose Bone Matrix"
  660. mantis_node_class_name=bl_idname
  661. socket_templates=MatrixComposeSockets
  662. def display_update(self, parsed_tree, context):
  663. # first we'll checc if the mode is connected to a noodle, should be rare
  664. mode = 'BASIS'
  665. if self.inputs['Mode'].is_linked:
  666. if mantis_node := parsed_tree.get(get_signature_from_edited_tree(self, context)):
  667. mode = mantis_node.evaluate_input("Mode")
  668. else: # We can use it directly, this is a little faster.
  669. mode = self.inputs['Mode'].default_value
  670. # We just hide/show based on the selected mode.
  671. for template in self.__class__.socket_templates:
  672. if mode == 'BASIS':
  673. if template.category == 'transform':
  674. self.inputs[template.name].hide=True
  675. elif template.category == 'axes':
  676. self.inputs[template.name].hide=False
  677. elif mode == 'CHANNELS':
  678. if template.category == 'transform':
  679. self.inputs[template.name].hide=False
  680. elif template.category == 'axes':
  681. self.inputs[template.name].hide=True
  682. if template.category == 'always_show':
  683. self.inputs[template.name].hide=False
  684. class UtilityMatrixAlignRoll(Node, MantisMiscNode):
  685. """Aligns the Z axis of a matrix to point in a direction."""
  686. bl_idname = "UtilityMatrixAlignRoll"
  687. bl_label = "Align Matrix Roll"
  688. mantis_node_class_name=bl_idname
  689. socket_templates=MatrixAlignRollSockets
  690. class UtilityMatrixSetLocation(Node, MantisMiscNode):
  691. """Sets a matrix's location."""
  692. bl_idname = "UtilityMatrixSetLocation"
  693. bl_label = "Set Matrix Location"
  694. mantis_node_class_name=bl_idname
  695. socket_templates=MatrixSetLocationSockets
  696. class UtilityMatrixGetLocation(Node, MantisMiscNode):
  697. """Gets a matrix's location."""
  698. bl_idname = "UtilityMatrixGetLocation"
  699. bl_label = "Get Matrix Location"
  700. mantis_node_class_name=bl_idname
  701. socket_templates=MatrixGetLocationSockets
  702. class UtilityTransformationMatrix(Node, MantisMiscNode):
  703. """Constructs a matrix representing a transformation"""
  704. bl_idname = "UtilityTransformationMatrix"
  705. bl_label = "Transformation Matrix"
  706. mantis_node_class_name=bl_idname
  707. socket_templates=TransformationMatrixSockets
  708. def display_update(self, parsed_tree, context):
  709. operation = self.inputs['Operation'].default_value
  710. if self.inputs['Operation'].is_linked:
  711. if context.space_data:
  712. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  713. operation = mantis_node.evaluate_input("Operation")
  714. if operation in ["ROTATE_AXIS_ANGLE", "SCALE"]:
  715. self.inputs["Vector"].hide = False
  716. self.inputs["W"].hide = False
  717. if operation in ["TRANSLATE"]:
  718. self.inputs["Vector"].hide = False
  719. self.inputs["W"].hide = True
  720. # Blender calculates bone roll this way...
  721. # https://projects.blender.org/blender/blender/src/commit/dd209221675ac7b62ce47b7ea42f15cbe34a6035/source/blender/editors/armature/armature_edit.cc#L281
  722. # 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
  723. # in the edit bone.
  724. # So instead, we need to avoid calculating the roll for now.
  725. # but I want to make that its own node and add roll-recalc to this node, too.
  726. class UtilitySetBoneMatrixTail(Node, MantisMiscNode):
  727. """Constructs a matrix representing a transformation"""
  728. bl_idname = "UtilitySetBoneMatrixTail"
  729. bl_label = "Set Bone Matrix Tail"
  730. mantis_node_class_name=bl_idname
  731. socket_templates=SetBoneMatrixTailSockets
  732. class UtilityMatrixFromXForm(Node, MantisMiscNode):
  733. """Returns the matrix of the given xForm node."""
  734. bl_idname = "UtilityMatrixFromXForm"
  735. bl_label = "Matrix of xForm"
  736. mantis_node_class_name=bl_idname
  737. socket_templates=MatrixFromXFormSockets
  738. class UtilityAxesFromMatrix(Node, MantisMiscNode):
  739. """Returns the axes of the matrix."""
  740. bl_idname = "UtilityAxesFromMatrix"
  741. bl_label = "Axes of Matrix"
  742. mantis_node_class_name=bl_idname
  743. socket_templates=AxesFromMatrixSockets
  744. class UtilityIntToString(Node, MantisMiscNode):
  745. """Converts a number to a string"""
  746. bl_idname = "UtilityIntToString"
  747. bl_label = "Number String"
  748. mantis_node_class_name=bl_idname
  749. socket_templates=IntToStringSockets
  750. class UtilityArrayGet(Node, MantisMiscNode):
  751. """Gets a value from an array at a specified index."""
  752. bl_idname = "UtilityArrayGet"
  753. bl_label = "Array Get"
  754. bl_icon = "NODE"
  755. mantis_node_class_name=bl_idname
  756. socket_templates=ArrayGetSockets
  757. def update(self):
  758. wildcard_color = (0.0,0.0,0.0,0.0)
  759. if self.inputs['Array'].is_linked == False:
  760. self.inputs['Array'].color = wildcard_color
  761. self.outputs['Output'].color = wildcard_color
  762. def insert_link(self, link):
  763. super().insert_link(link)
  764. if link.to_socket.identifier == self.inputs['Array'].identifier:
  765. from_socket = link.from_socket
  766. if hasattr(from_socket, "color"):
  767. self.inputs['Array'].color = from_socket.color
  768. self.outputs['Output'].color = from_socket.color
  769. class UtilityArrayLength(Node, MantisMiscNode):
  770. """Gets The length of an array."""
  771. bl_idname = "UtilityArrayLength"
  772. bl_label = "Array Length"
  773. bl_icon = "NODE"
  774. mantis_node_class_name=bl_idname
  775. socket_templates=UtilityArrayLengthSockets
  776. def update(self):
  777. wildcard_color = (0.0,0.0,0.0,0.0)
  778. if self.inputs['Array'].is_linked == False:
  779. self.inputs['Array'].color = wildcard_color
  780. def insert_link(self, link):
  781. super().insert_link(link)
  782. if link.to_socket.identifier == self.inputs['Array'].identifier:
  783. from_socket = link.from_socket
  784. if hasattr(from_socket, "color"):
  785. self.inputs['Array'].color = from_socket.color
  786. class UtilityCompare(Node, MantisMiscNode):
  787. """Compares two inputs and produces a boolean output"""
  788. bl_idname = "UtilityCompare"
  789. bl_label = "Compare"
  790. mantis_node_class_name=bl_idname
  791. socket_templates=CompareSockets
  792. def update(self):
  793. wildcard_color = (0.0,0.0,0.0,0.0)
  794. if self.inputs['A'].is_linked == False:
  795. self.inputs['A'].color = wildcard_color
  796. if self.inputs['B'].is_linked == False:
  797. self.inputs['B'].color = wildcard_color
  798. def insert_link(self, link):
  799. super().insert_link(link)
  800. if link.to_socket.identifier == self.inputs['A'].identifier:
  801. self.inputs['A'].color = link.from_socket.color_simple
  802. if hasattr(link.from_socket, "color"):
  803. self.inputs['A'].color = link.from_socket.color
  804. if link.to_socket.identifier == self.inputs['B'].identifier:
  805. self.inputs['B'].color = link.from_socket.color_simple
  806. if hasattr(link.from_socket, "color"):
  807. self.inputs['B'].color = link.from_socket.color
  808. class UtilityChoose(Node, MantisMiscNode):
  809. """Chooses an output"""
  810. bl_idname = "UtilityChoose"
  811. bl_label = "Choose"
  812. mantis_node_class_name=bl_idname
  813. socket_templates=ChooseSockets
  814. def display_update(self, parsed_tree, context):
  815. wildcard_color = (0.0,0.0,0.0,0.0)
  816. if not self.inputs['A'].is_linked:
  817. self.inputs['A'].color = wildcard_color
  818. if not self.inputs['B'].is_linked:
  819. self.inputs['B'].color = wildcard_color
  820. self.outputs['Result'].color = wildcard_color
  821. # if both inputs are the same color, then use that color for the result
  822. if self.inputs['Condition'].is_linked:
  823. from .base_definitions import get_signature_from_edited_tree
  824. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  825. if mantis_node:
  826. condition = mantis_node.evaluate_input('Condition')
  827. else:
  828. condition = self.inputs['Condition'].default_value
  829. if condition == True:
  830. self.outputs['Result'].color = self.inputs['B'].color
  831. else:
  832. self.outputs['Result'].color = self.inputs['A'].color
  833. def insert_link(self, link):
  834. super().insert_link(link)
  835. if link.to_socket.identifier == self.inputs['A'].identifier:
  836. self.inputs['A'].color = link.from_socket.color_simple
  837. if hasattr(link.from_socket, "color"):
  838. self.inputs['A'].color = link.from_socket.color
  839. if link.to_socket.identifier == self.inputs['B'].identifier:
  840. self.inputs['B'].color = link.from_socket.color_simple
  841. if hasattr(link.from_socket, "color"):
  842. self.inputs['B'].color = link.from_socket.color
  843. class UtilityPrint(Node, MantisMiscNode):
  844. """A utility used to print arbitrary values."""
  845. bl_idname = "UtilityPrint"
  846. bl_label = "Print"
  847. bl_icon = "NODE"
  848. mantis_node_class_name=bl_idname
  849. socket_templates=PrintSockets