misc_nodes_ui.py 37 KB

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