misc_nodes_ui.py 36 KB

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