misc_nodes_ui.py 36 KB

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