misc_nodes_ui.py 36 KB

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