misc_nodes_ui.py 40 KB

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