misc_nodes_ui.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  473. prop_type = self.inputs['Type'].default_value
  474. if mantis_node: # this is done here so I don't have to define yet another custom socket.
  475. prop_type = mantis_node.evaluate_input("Type")
  476. visible_categories = ['general']
  477. match prop_type:
  478. case 'STRING': visible_categories.append('string')
  479. case 'BOOL': visible_categories.append('bool')
  480. case 'INT': visible_categories.append('int')
  481. case 'FLOAT': visible_categories.append('float')
  482. case 'VECTOR': visible_categories.append('vector')
  483. for i, socket_template in enumerate(UtilityCustomPropertySockets):
  484. if socket_template.is_input == False: continue
  485. if socket_template.category in visible_categories:
  486. self.inputs[i].hide = False
  487. else:
  488. self.inputs[i].hide = True
  489. if prop_type == "VECTOR":
  490. for i in range (4): # keep the min/max open
  491. self.inputs[i+3].hide=False
  492. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  493. if self.label:
  494. return self.label
  495. if self.inputs['Name'].display_text:
  496. return self.inputs['Name'].display_text
  497. return self.name
  498. class UtilityDeclareCollections(Node, MantisMiscNode):
  499. """A utility used to declare bone collections."""
  500. bl_idname = "UtilityDeclareCollections"
  501. bl_label = "Collections"
  502. bl_icon = "NODE"
  503. bl_width_min = 320
  504. mantis_node_class_name=bl_idname
  505. # Here is the layout of the data:
  506. # nested dicts of key:dict ( key = name, dict = children)
  507. # the 'leaf nodes' are empty dicts
  508. # we'll store it as a JSON string in order to make it a bpy.props
  509. # and still have the ability to use it as a dict and save it
  510. # TODO: check and see if these strings have a character limit
  511. collection_declarations : bpy.props.StringProperty(default="")
  512. def update_interface(self):
  513. from .utilities.utilities import get_socket_maps, do_relink
  514. # we need to do dynamic stuff here like with interfaces
  515. socket_map_out = get_socket_maps(self)[1]
  516. self.outputs.clear()
  517. current_data = self.read_declarations_from_json()
  518. socket_data = socket_data_from_collection_paths(current_data, self.name, [], [])
  519. for item in socket_data:
  520. full_path_name = '>'.join(item[1]+[item[0]])
  521. s = self.outputs.new('CollectionDeclarationSocket', name=item[0],identifier=full_path_name )
  522. s.collection_path = full_path_name
  523. for s in self.outputs:
  524. if s.identifier in socket_map_out.keys():
  525. do_relink(self, s, socket_map_out, 'OUTPUT')
  526. def init(self, context):
  527. self.initialized = True
  528. if self.collection_declarations == "":
  529. self.push_declarations_to_json({})
  530. def push_declarations_to_json(self, dict):
  531. import json
  532. j_str = json.dumps(dict)
  533. self.collection_declarations = j_str
  534. def read_declarations_from_json(self):
  535. import json
  536. j_data = json.loads(self.collection_declarations)
  537. return j_data
  538. def draw_buttons(self, context, layout):
  539. op_props = layout.operator('mantis.collection_add_new')
  540. op_props.socket_invoked = '' # this isn't reset between invocations
  541. # so we have to make sure to unset it when running it from the node
  542. class UtilityCollectionJoin(Node, MantisMiscNode):
  543. """A utility used to join bone collection declarations."""
  544. bl_idname = "UtilityCollectionJoin"
  545. bl_label = "Join Collection Declarations"
  546. bl_icon = "NODE"
  547. mantis_node_class_name=bl_idname
  548. socket_templates=CollectionJoinSockets
  549. class UtilityCollectionHierarchy(Node, MantisMiscNode):
  550. """A utility used to declare bone collection hierarchies."""
  551. bl_idname = "UtilityCollectionHierarchy"
  552. bl_label = "Collection Child Declaration"
  553. bl_icon = "NODE"
  554. mantis_node_class_name=bl_idname
  555. socket_templates=CollectionHierarchySockets
  556. class UtilityGeometryOfXForm(Node, MantisMiscNode):
  557. """Retrieves a mesh or curve datablock from an xForm."""
  558. bl_idname = "UtilityGeometryOfXForm"
  559. bl_label = "Geometry of xForm"
  560. mantis_node_class_name=bl_idname
  561. socket_templates=GeometryOfXFormSockets
  562. class UtilityNameOfXForm(Node, MantisMiscNode):
  563. """Retrieves the name of a xForm."""
  564. bl_idname = "UtilityNameOfXForm"
  565. bl_label = "Name of xForm"
  566. mantis_node_class_name=bl_idname
  567. socket_templates=NameOfXFormSockets
  568. class UtilityGetBoneLength(Node, MantisMiscNode):
  569. """Returns the length of the bone from its matrix."""
  570. bl_idname = "UtilityGetBoneLength"
  571. bl_label = "Get Bone Length"
  572. mantis_node_class_name=bl_idname
  573. socket_templates=GetBoneLengthSockets
  574. # TODO: make it work with BBones!
  575. class UtilityPointFromBoneMatrix(Node, MantisMiscNode):
  576. """Returns a point representing the location along a bone, given a matrix representing that bone's shape."""
  577. bl_idname = "UtilityPointFromBoneMatrix"
  578. bl_label = "Point from Bone Matrix"
  579. mantis_node_class_name=bl_idname
  580. socket_templates=PointFromBoneMatrixSockets
  581. class UtilitySetBoneLength(Node, MantisMiscNode):
  582. """Sets the length of a bone matrix."""
  583. bl_idname = "UtilitySetBoneLength"
  584. bl_label = "Set Bone Matrix Length"
  585. mantis_node_class_name=bl_idname
  586. socket_templates=SetBoneLengthSockets
  587. # TODO: more keyframe types should be supported in the future.
  588. # Some of the code that can do this is commented out here until I can implement it properly.
  589. class UtilityKeyframe(Node, MantisMiscNode):
  590. """A keyframe for a FCurve"""
  591. bl_idname = "UtilityKeyframe"
  592. bl_label = "KeyFrame"
  593. mantis_node_class_name=bl_idname
  594. socket_templates=KeyframeSockets
  595. # TODO: I'll implement this when I finish adding the extra features to the Keyframe node
  596. # def display_update(self, parsed_tree, context):
  597. # if context.space_data:
  598. # mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  599. # if mantis_node.evaluate_input("Interpolation") in ["CONSTANT", "LINEAR"]:
  600. # for inp in self.inputs[1:6]:
  601. # inp.hide = True
  602. # else:
  603. # if mantis_node.evaluate_input("Left Handle Type") in ["FREE", "ALIGNED"]:
  604. # for inp in self.inputs[1:6]:
  605. # inp.hide = False
  606. class UtilityBoneMatrixHeadTailFlip(Node, MantisMiscNode):
  607. """Flips a bone matrix so that the head is where the tail was and visa versa."""
  608. bl_idname = "UtilityBoneMatrixHeadTailFlip"
  609. bl_label = "Flip Head/Tail"
  610. mantis_node_class_name=bl_idname
  611. socket_templates=BoneMatrixHeadTailFlipSockets
  612. class UtilityMatrixTransform(Node, MantisMiscNode):
  613. """Transforms a matrix by another."""
  614. bl_idname = "UtilityMatrixTransform"
  615. bl_label = "Multiply Matrices"
  616. mantis_node_class_name=bl_idname
  617. socket_templates=MatrixTransformSockets
  618. class UtilityMatrixInvert(Node, MantisMiscNode):
  619. """Inverts an invertable matrix, otherwise throws an error."""
  620. bl_idname = "UtilityMatrixInvert"
  621. bl_label = "Invert Matrix"
  622. mantis_node_class_name=bl_idname
  623. socket_templates=MatrixInvertSockets
  624. class UtilityMatrixCompose(Node, MantisMiscNode):
  625. """Inverts an invertable matrix, otherwise throws an error."""
  626. bl_idname = "UtilityMatrixCompose"
  627. bl_label = "Compose Matrix"
  628. mantis_node_class_name=bl_idname
  629. socket_templates=MatrixComposeSockets
  630. class UtilityMatrixAlignRoll(Node, MantisMiscNode):
  631. """Aligns the Z axis of a matrix to point in a direction."""
  632. bl_idname = "UtilityMatrixAlignRoll"
  633. bl_label = "Align Matrix Roll"
  634. mantis_node_class_name=bl_idname
  635. socket_templates=MatrixAlignRollSockets
  636. class UtilityMatrixSetLocation(Node, MantisMiscNode):
  637. """Sets a matrix's location."""
  638. bl_idname = "UtilityMatrixSetLocation"
  639. bl_label = "Set Matrix Location"
  640. mantis_node_class_name=bl_idname
  641. socket_templates=MatrixSetLocationSockets
  642. class UtilityMatrixGetLocation(Node, MantisMiscNode):
  643. """Gets a matrix's location."""
  644. bl_idname = "UtilityMatrixGetLocation"
  645. bl_label = "Get Matrix Location"
  646. mantis_node_class_name=bl_idname
  647. socket_templates=MatrixGetLocationSockets
  648. class UtilityTransformationMatrix(Node, MantisMiscNode):
  649. """Constructs a matrix representing a transformation"""
  650. bl_idname = "UtilityTransformationMatrix"
  651. bl_label = "Transformation Matrix"
  652. mantis_node_class_name=bl_idname
  653. socket_templates=TransformationMatrixSockets
  654. def display_update(self, parsed_tree, context):
  655. operation = self.inputs['Operation'].default_value
  656. if self.inputs['Operation'].is_linked:
  657. if context.space_data:
  658. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  659. operation = mantis_node.evaluate_input("Operation")
  660. if operation in ["ROTATE_AXIS_ANGLE", "SCALE"]:
  661. self.inputs["Vector"].hide = False
  662. self.inputs["W"].hide = False
  663. if operation in ["TRANSLATE"]:
  664. self.inputs["Vector"].hide = False
  665. self.inputs["W"].hide = True
  666. # Blender calculates bone roll this way...
  667. # https://projects.blender.org/blender/blender/src/commit/dd209221675ac7b62ce47b7ea42f15cbe34a6035/source/blender/editors/armature/armature_edit.cc#L281
  668. # 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
  669. # in the edit bone.
  670. # So instead, we need to avoid calculating the roll for now.
  671. # but I want to make that its own node and add roll-recalc to this node, too.
  672. class UtilitySetBoneMatrixTail(Node, MantisMiscNode):
  673. """Constructs a matrix representing a transformation"""
  674. bl_idname = "UtilitySetBoneMatrixTail"
  675. bl_label = "Set Bone Matrix Tail"
  676. mantis_node_class_name=bl_idname
  677. socket_templates=SetBoneMatrixTailSockets
  678. class UtilityMatrixFromXForm(Node, MantisMiscNode):
  679. """Returns the matrix of the given xForm node."""
  680. bl_idname = "UtilityMatrixFromXForm"
  681. bl_label = "Matrix of xForm"
  682. mantis_node_class_name=bl_idname
  683. socket_templates=MatrixFromXFormSockets
  684. class UtilityAxesFromMatrix(Node, MantisMiscNode):
  685. """Returns the axes of the matrix."""
  686. bl_idname = "UtilityAxesFromMatrix"
  687. bl_label = "Axes of Matrix"
  688. mantis_node_class_name=bl_idname
  689. socket_templates=AxesFromMatrixSockets
  690. class UtilityIntToString(Node, MantisMiscNode):
  691. """Converts a number to a string"""
  692. bl_idname = "UtilityIntToString"
  693. bl_label = "Number String"
  694. mantis_node_class_name=bl_idname
  695. socket_templates=IntToStringSockets
  696. class UtilityArrayGet(Node, MantisMiscNode):
  697. """Gets a value from an array at a specified index."""
  698. bl_idname = "UtilityArrayGet"
  699. bl_label = "Array Get"
  700. bl_icon = "NODE"
  701. mantis_node_class_name=bl_idname
  702. socket_templates=ArrayGetSockets
  703. def update(self):
  704. wildcard_color = (0.0,0.0,0.0,0.0)
  705. if self.inputs['Array'].is_linked == False:
  706. self.inputs['Array'].color = wildcard_color
  707. self.outputs['Output'].color = wildcard_color
  708. def insert_link(self, link):
  709. super().insert_link(link)
  710. if link.to_socket.identifier == self.inputs['Array'].identifier:
  711. from_socket = link.from_socket
  712. if hasattr(from_socket, "color"):
  713. self.inputs['Array'].color = from_socket.color
  714. self.outputs['Output'].color = from_socket.color
  715. class UtilityArrayLength(Node, MantisMiscNode):
  716. """Gets The length of an array."""
  717. bl_idname = "UtilityArrayLength"
  718. bl_label = "Array Length"
  719. bl_icon = "NODE"
  720. mantis_node_class_name=bl_idname
  721. socket_templates=UtilityArrayLengthSockets
  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. def insert_link(self, link):
  727. super().insert_link(link)
  728. if link.to_socket.identifier == self.inputs['Array'].identifier:
  729. from_socket = link.from_socket
  730. if hasattr(from_socket, "color"):
  731. self.inputs['Array'].color = from_socket.color
  732. class UtilityCompare(Node, MantisMiscNode):
  733. """Compares two inputs and produces a boolean output"""
  734. bl_idname = "UtilityCompare"
  735. bl_label = "Compare"
  736. mantis_node_class_name=bl_idname
  737. socket_templates=CompareSockets
  738. def update(self):
  739. wildcard_color = (0.0,0.0,0.0,0.0)
  740. if self.inputs['A'].is_linked == False:
  741. self.inputs['A'].color = wildcard_color
  742. if self.inputs['B'].is_linked == False:
  743. self.inputs['B'].color = wildcard_color
  744. def insert_link(self, link):
  745. super().insert_link(link)
  746. if link.to_socket.identifier == self.inputs['A'].identifier:
  747. self.inputs['A'].color = link.from_socket.color_simple
  748. if hasattr(link.from_socket, "color"):
  749. self.inputs['A'].color = link.from_socket.color
  750. if link.to_socket.identifier == self.inputs['B'].identifier:
  751. self.inputs['B'].color = link.from_socket.color_simple
  752. if hasattr(link.from_socket, "color"):
  753. self.inputs['B'].color = link.from_socket.color
  754. class UtilityChoose(Node, MantisMiscNode):
  755. """Chooses an output"""
  756. bl_idname = "UtilityChoose"
  757. bl_label = "Choose"
  758. mantis_node_class_name=bl_idname
  759. socket_templates=ChooseSockets
  760. def display_update(self, parsed_tree, context):
  761. wildcard_color = (0.0,0.0,0.0,0.0)
  762. if not self.inputs['A'].is_linked:
  763. self.inputs['A'].color = wildcard_color
  764. if not self.inputs['B'].is_linked:
  765. self.inputs['B'].color = wildcard_color
  766. self.outputs['Result'].color = wildcard_color
  767. # if both inputs are the same color, then use that color for the result
  768. if self.inputs['Condition'].is_linked:
  769. from .base_definitions import get_signature_from_edited_tree
  770. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  771. if mantis_node:
  772. condition = mantis_node.evaluate_input('Condition')
  773. else:
  774. condition = self.inputs['Condition'].default_value
  775. if condition == True:
  776. self.outputs['Result'].color = self.inputs['B'].color
  777. else:
  778. self.outputs['Result'].color = self.inputs['A'].color
  779. def insert_link(self, link):
  780. super().insert_link(link)
  781. if link.to_socket.identifier == self.inputs['A'].identifier:
  782. self.inputs['A'].color = link.from_socket.color_simple
  783. if hasattr(link.from_socket, "color"):
  784. self.inputs['A'].color = link.from_socket.color
  785. if link.to_socket.identifier == self.inputs['B'].identifier:
  786. self.inputs['B'].color = link.from_socket.color_simple
  787. if hasattr(link.from_socket, "color"):
  788. self.inputs['B'].color = link.from_socket.color
  789. class UtilityPrint(Node, MantisMiscNode):
  790. """A utility used to print arbitrary values."""
  791. bl_idname = "UtilityPrint"
  792. bl_label = "Print"
  793. bl_icon = "NODE"
  794. mantis_node_class_name=bl_idname
  795. socket_templates=PrintSockets