xForm_nodes_ui.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. import bpy
  2. from .base_definitions import xFormNode
  3. from bpy.types import Node
  4. from .utilities import (prRed, prGreen, prPurple, prWhite,
  5. prOrange,
  6. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  7. wrapOrange,)
  8. from .base_definitions import get_signature_from_edited_tree
  9. from .xForm_socket_templates import *
  10. def TellClasses():
  11. return [
  12. # xFormNullNode,
  13. xFormBoneNode,
  14. xFormArmatureNode,
  15. xFormGeometryObjectNode,
  16. xFormObjectInstance,
  17. xFormCurvePin,
  18. xFormGetBone,
  19. ]
  20. def default_traverse(self, socket):
  21. if (socket == self.outputs["xForm Out"]):
  22. return self.inputs["Relationship"]
  23. if (socket == self.inputs["Relationship"]):
  24. return self.outputs["xForm Out"]
  25. return None
  26. # Representing an Empty or non-armature-Object
  27. # class xFormNullNode(Node, xFormNode):
  28. # '''A node representing a Null node'''
  29. # bl_idname = 'xFormNullNode'
  30. # bl_label = "Null"
  31. # bl_icon = 'EMPTY_AXIS'
  32. # # === Optional Functions ===
  33. # def init(self, context):
  34. # self.inputs.new('StringSocket', "Name")
  35. # self.inputs.new('RelationshipSocket', "Relationship")
  36. # self.inputs.new('RotationOrderSocket', "Rotation Order")
  37. # self.inputs.new('MatrixSocket', "Matrix")
  38. # self.outputs.new('xFormSocket', "xForm Out")
  39. def check_if_connected(start, end, line):
  40. started=False
  41. for path_mantis_node in line:
  42. prWhite(" ", path_mantis_node.signature)
  43. if path_mantis_node.signature == start.signature:
  44. started = True
  45. elif path_mantis_node.signature == end.signature:
  46. break
  47. if started:
  48. if path_mantis_node.inputs.get("Connected"):
  49. if path_mantis_node.evaluate_input("Connected") == False:
  50. return False
  51. else:
  52. return False
  53. return True
  54. def main_draw_label(self): # this will prefer a user-set label, or return the evaluated name
  55. if self.label:
  56. return self.label
  57. if self.inputs['Name'].display_text:
  58. return self.inputs['Name'].display_text
  59. return self.name
  60. def custom_prop_display_update(self, mantis_node = None):
  61. custom_props = {}
  62. type_map = {
  63. 'BOOL' : 'ParameterBoolSocket',
  64. 'INT' : 'ParameterIntSocket',
  65. 'FLOAT' : 'ParameterFloatSocket',
  66. 'VECTOR' : 'ParameterVectorSocket',
  67. 'STRING' : 'ParameterStringSocket',
  68. }
  69. cant_read=0
  70. if not mantis_node:
  71. links = [link for link in self.inputs['Custom Properties'].links]
  72. links.sort(key=lambda a : -a.multi_input_sort_id)
  73. for link in links:
  74. if link.from_node.bl_idname == 'UtilityCustomProperty':
  75. if (link.from_node.inputs['Name'].is_linked or
  76. link.from_node.inputs['Type'].is_linked):
  77. cant_read+=1 # we can't eval this without the Mantis data
  78. continue # but we need to count it
  79. else:
  80. custom_props[link.from_node.inputs['Name'].default_value] = \
  81. type_map[link.from_node.inputs['Type'].default_value]
  82. else:
  83. cant_read+=1
  84. for name, type in custom_props.items():
  85. if self.outputs.get(name): continue # already there
  86. else:
  87. self.outputs.new(type, name)
  88. else:
  89. from .misc_nodes import UtilityCustomProperty
  90. from .mantis_dataclasses import custom_prop_template
  91. mantis_node.inputs['Custom Properties'].flush_links() # clean/sort the links
  92. if len(mantis_node.inputs['Custom Properties'].links) != len(self.inputs['Custom Properties'].links):
  93. return # there is a problem somewhere
  94. for i, link in enumerate(mantis_node.inputs['Custom Properties'].links):
  95. from .node_common import trace_single_line
  96. node_line, _last_socket = trace_single_line(mantis_node, 'Custom Properties', i)
  97. if not node_line[-1].prepared:
  98. return
  99. custom_prop_data = mantis_node.evaluate_input('Custom Property', i)
  100. if isinstance(node_line[-1], UtilityCustomProperty) and \
  101. not isinstance(custom_prop_data, custom_prop_template):
  102. return
  103. elif not isinstance(custom_prop_data, custom_prop_template):
  104. continue # wrong connection
  105. name = custom_prop_data.name
  106. type = custom_prop_data.prop_type
  107. custom_props[name] = type_map[type]
  108. from .utilities import get_socket_maps, do_relink
  109. _socket_map_in, socket_map_out = get_socket_maps(self)
  110. for name, type in custom_props.items():
  111. if socket_map_out.get(name) is None:
  112. socket_map_out[name] = None # initialize the new ones
  113. # remove and replace sockets
  114. if cant_read == 0:
  115. remove_me = self.outputs[1:]
  116. while remove_me:
  117. remove_socket = remove_me.pop()
  118. self.outputs.remove(remove_socket)
  119. for name, type in custom_props.items():
  120. socket = self.outputs.new(type, name)
  121. do_relink(self, socket, socket_map_out, in_out='OUTPUT')
  122. # I had chat gpt flip these so they may be a little innacurate
  123. # always visible
  124. main_names = {
  125. "Name":'StringSocket',
  126. "Rotation Order":'RotationOrderSocket',
  127. "Relationship":'RelationshipSocket',
  128. "Matrix":'MatrixSocket',}
  129. # IK SETTINGS
  130. ik_names = {
  131. "IK Stretch":'FloatFactorSocket',
  132. "Lock IK":'BooleanThreeTupleSocket',
  133. "IK Stiffness":'NodeSocketVector',
  134. "Limit IK":'BooleanThreeTupleSocket',
  135. "X Min":'NodeSocketFloatAngle',
  136. "X Max":'NodeSocketFloatAngle',
  137. "Y Min":'NodeSocketFloatAngle',
  138. "Y Max":'NodeSocketFloatAngle',
  139. "Z Min":'NodeSocketFloatAngle',
  140. "Z Max":'NodeSocketFloatAngle',
  141. }
  142. #display settings
  143. display_names = {
  144. "Bone Collection":'BoneCollectionSocket',
  145. "Custom Object":'xFormSocket',
  146. "Custom Object xForm Override":'xFormSocket',
  147. "Custom Object Scale to Bone Length":'BooleanSocket',
  148. "Custom Object Wireframe":'BooleanSocket',
  149. "Custom Object Scale":'VectorScaleSocket',
  150. "Custom Object Translation":'VectorSocket',
  151. "Custom Object Rotation":'VectorEulerSocket',
  152. "Color":'ColorSetSocket',
  153. "Inherit Color":'BooleanSocket',
  154. }
  155. # deform_names
  156. deform_names = {
  157. "Deform":'BooleanSocket',
  158. "Envelope Distance":'FloatPositiveSocket',
  159. "Envelope Weight":'FloatFactorSocket',
  160. "Envelope Multiply":'BooleanSocket',
  161. "Envelope Head Radius":'FloatPositiveSocket',
  162. "Envelope Tail Radius":'FloatPositiveSocket',
  163. }
  164. bbone_names = {
  165. "BBone Segments":"IntSocket", # BONE
  166. "BBone X Size":"FloatSocket", # BONE
  167. "BBone Z Size":"FloatSocket", # BONE
  168. # "bbone_mapping_mode":"StringSocket", <== BONE
  169. "BBone HQ Deformation":"BooleanSocket", # BONE bbone_mapping_mode
  170. "BBone X Curve-In":"FloatSocket", # BONE AND POSE
  171. "BBone Z Curve-In":"FloatSocket", # BONE AND POSE
  172. "BBone X Curve-Out":"FloatSocket", # BONE AND POSE
  173. "BBone Z Curve-Out":"FloatSocket", # BONE AND POSE
  174. "BBone Roll-In":"FloatSocket", # BONE AND POSE
  175. "BBone Roll-Out":"FloatSocket", # BONE AND POSE
  176. "BBone Inherit End Roll":"BooleanSocket", # BONE
  177. "BBone Scale-In":"VectorSocket", # BONE AND POSE
  178. "BBone Scale-Out":"VectorSocket", # BONE AND POSE
  179. "BBone Ease-In":"FloatSocket", # BONE AND POSE
  180. "BBone Ease-Out":"FloatSocket", # BONE AND POSE
  181. "BBone Easing":"BooleanSocket", # BONE
  182. "BBone Start Handle Type":"EnumBBoneHandleType", # BONE
  183. "BBone Custom Start Handle":"StringSocket", # BONE
  184. "BBone Start Handle Scale":"BooleanThreeTupleSocket", # BONE
  185. "BBone Start Handle Ease":"BooleanSocket", # BONE
  186. "BBone End Handle Type":"EnumBBoneHandleType", # BONE
  187. "BBone Custom End Handle":"StringSocket", # BONE
  188. "BBone End Handle Scale":"BooleanThreeTupleSocket", # BONE
  189. "BBone End Handle Ease":"BooleanSocket", # BONE
  190. }
  191. other_names = {
  192. "Lock Location":'BooleanThreeTupleSocket',
  193. "Lock Rotation":'BooleanThreeTupleSocket',
  194. "Lock Scale":'BooleanThreeTupleSocket',
  195. "Hide":'HideSocket',
  196. }
  197. from mathutils import Color
  198. xFormColor = Color((0.093172, 0.047735, 0.028036)).from_scene_linear_to_srgb()
  199. class xFormBoneNode(Node, xFormNode):
  200. '''A node representing a Bone'''
  201. bl_idname = 'xFormBoneNode'
  202. bl_label = "Bone"
  203. bl_icon = 'BONE_DATA'
  204. display_ik_settings : bpy.props.BoolProperty(default=False)
  205. display_vp_settings : bpy.props.BoolProperty(default=False)
  206. display_def_settings : bpy.props.BoolProperty(default=False)
  207. display_bb_settings : bpy.props.BoolProperty(default=False)
  208. socket_count : bpy.props.IntProperty()
  209. initialized : bpy.props.BoolProperty(default = False)
  210. mantis_node_class_name=bl_idname[:-4]
  211. def init(self, context):
  212. for name, sock_type in main_names.items():
  213. self.inputs.new(sock_type, name)
  214. for name, sock_type in ik_names.items():
  215. s = self.inputs.new(sock_type, name)
  216. s.hide = True
  217. for name, sock_type in display_names.items():
  218. if name == 'Bone Collection': # HACK because I am not using Socket Templates yet
  219. s = self.inputs.new(sock_type, name, use_multi_input=True)
  220. else:
  221. s = self.inputs.new(sock_type, name)
  222. if s.name in ['Custom Object', 'Bone Collection']:
  223. continue
  224. s.hide = True
  225. for name, sock_type in deform_names.items():
  226. s = self.inputs.new(sock_type, name)
  227. if s.name == 'Deform':
  228. continue
  229. s.hide = True
  230. for name, sock_type in bbone_names.items():
  231. s = self.inputs.new(sock_type, name)
  232. if s.name == "BBone Segments":
  233. continue
  234. s.hide = True
  235. for name, sock_type in other_names.items():
  236. self.inputs.new(sock_type, name)
  237. # could probably simplify this further with iter_tools.chain() but meh
  238. self.inputs.new("CustomPropSocket", "Custom Properties", use_multi_input=True)
  239. self.socket_count = len(self.inputs)
  240. #
  241. self.outputs.new('xFormSocket', "xForm Out")
  242. # set up some defaults...
  243. self.inputs['Rotation Order'].default_value = "XYZ"
  244. self.inputs['Lock Location'].default_value[0] = True
  245. self.inputs['Lock Location'].default_value[1] = True
  246. self.inputs['Lock Location'].default_value[2] = True
  247. self.inputs['Lock Rotation'].default_value[0] = True
  248. self.inputs['Lock Rotation'].default_value[1] = True
  249. self.inputs['Lock Rotation'].default_value[2] = True
  250. self.inputs['Lock Scale'].default_value[0] = True
  251. self.inputs['Lock Scale'].default_value[1] = True
  252. self.inputs['Lock Scale'].default_value[2] = True
  253. self.inputs['Inherit Color'].default_value = True
  254. # color
  255. self.use_custom_color = True
  256. self.color = xFormColor
  257. #
  258. self.initialized=True
  259. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  260. return main_draw_label(self)
  261. def display_update(self, parsed_tree, context):
  262. # let's setup the outputs for the custom properties:
  263. # we'll start by trying to do it without the special mantis data
  264. if self.id_data.mantis_version[1] < 13: return #or the old custom properties will get wiped.
  265. custom_prop_display_update(self)
  266. if context.space_data:
  267. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  268. if mantis_node and mantis_node.prepared:
  269. custom_prop_display_update(self, mantis_node)
  270. self.display_ik_settings = False
  271. if mantis_node and (pb := mantis_node.bGetObject(mode='POSE')):
  272. self.display_ik_settings = pb.is_in_ik_chain
  273. self.inputs['Name'].display_text = ""
  274. if mantis_node and mantis_node.prepared:
  275. try:
  276. self.inputs['Name'].display_text = mantis_node.evaluate_input("Name")
  277. self.display_vp_settings = mantis_node.inputs["Custom Object"].is_connected
  278. self.display_def_settings = mantis_node.evaluate_input("Deform")
  279. self.display_bb_settings = mantis_node.evaluate_input("BBone Segments") > 1
  280. except KeyError:
  281. return # the tree isn't ready yet.
  282. for name in ik_names.keys():
  283. self.inputs[name].hide = not self.display_ik_settings
  284. for name in display_names.keys():
  285. if name in ['Custom Object', 'Bone Collection']: continue
  286. self.inputs[name].hide = not self.display_vp_settings
  287. for name in deform_names.keys():
  288. if name in ['Deform']: continue
  289. self.inputs[name].hide = not self.display_def_settings
  290. for name in bbone_names.keys():
  291. if name in ['BBone Segments']: continue
  292. self.inputs[name].hide = not self.display_bb_settings
  293. def object_displaty_update(self, parsed_tree, context):
  294. custom_prop_display_update(self)
  295. if context.space_data:
  296. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  297. self.inputs['Name'].display_text = ""
  298. if mantis_node and mantis_node.prepared:
  299. self.inputs['Name'].display_text = mantis_node.evaluate_input("Name")
  300. custom_prop_display_update(self, mantis_node)
  301. class xFormArmatureNode(Node, xFormNode):
  302. '''A node representing an Armature object node'''
  303. bl_idname = 'xFormArmatureNode'
  304. bl_label = "Armature"
  305. bl_icon = 'OUTLINER_OB_ARMATURE'
  306. initialized : bpy.props.BoolProperty(default = False)
  307. mantis_node_class_name=bl_idname[:-4]
  308. def init(self, context):
  309. self.init_sockets(xFormArmatureSockets)
  310. self.use_custom_color = True
  311. self.color = xFormColor
  312. self.initialized=True
  313. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  314. return main_draw_label(self)
  315. def display_update(self, parsed_tree, context):
  316. object_displaty_update(self, parsed_tree, context)
  317. class xFormGeometryObjectNode(Node, xFormNode):
  318. """Represents a curve or mesh object."""
  319. bl_idname = "xFormGeometryObject"
  320. bl_label = "Geometry Object"
  321. bl_icon = "EMPTY_AXIS"
  322. initialized : bpy.props.BoolProperty(default = False)
  323. mantis_node_class_name=bl_idname
  324. def init(self, context):
  325. self.init_sockets(xFormGeometryObjectSockets)
  326. self.use_custom_color = True
  327. self.color = xFormColor
  328. self.initialized=True
  329. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  330. return main_draw_label(self)
  331. def display_update(self, parsed_tree, context):
  332. object_displaty_update(self, parsed_tree, context)
  333. class xFormObjectInstance(Node, xFormNode):
  334. """Represents an instance of an existing geometry object."""
  335. bl_idname = "xFormObjectInstance"
  336. bl_label = "Object Instance"
  337. bl_icon = "EMPTY_AXIS"
  338. initialized : bpy.props.BoolProperty(default = False)
  339. mantis_node_class_name=bl_idname
  340. def init(self, context):
  341. self.init_sockets(xFormGeometryObjectInstanceSockets)
  342. self.use_custom_color = True
  343. self.color = xFormColor
  344. self.initialized=True
  345. def draw_label(self): # this will prefer a user-set label, or return the evaluated name
  346. return main_draw_label(self)
  347. def display_update(self, parsed_tree, context):
  348. object_displaty_update(self, parsed_tree, context)
  349. from .xForm_nodes import xFormCurvePinSockets
  350. class xFormCurvePin(Node, xFormNode):
  351. """"A node representing a curve pin"""
  352. bl_idname = "xFormCurvePin"
  353. bl_label = "Curve Pin"
  354. bl_icon = "FORCE_CURVE"
  355. initialized : bpy.props.BoolProperty(default = False)
  356. mantis_node_class_name=bl_idname
  357. def init(self, context):
  358. self.init_sockets(xFormCurvePinSockets)
  359. self.use_custom_color = True
  360. self.color = xFormColor
  361. self.initialized = True
  362. class xFormGetBone(Node, xFormNode):
  363. """"A node representing an existing bone"""
  364. bl_idname = "xFormGetBone"
  365. bl_label = "Existing Bone"
  366. bl_icon = "BONE_DATA"
  367. initialized : bpy.props.BoolProperty(default = False)
  368. armature : bpy.props.StringProperty(default="")
  369. mantis_node_class_name=bl_idname
  370. def init(self, context):
  371. self.init_sockets(xFormGetBoneSockets)
  372. self.use_custom_color = True
  373. self.color = xFormColor
  374. self.initialized = True
  375. def display_update(self, parsed_tree, context):
  376. if context.space_data:
  377. mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
  378. if mantis_node:
  379. # need this parent armature to use the bone picker.
  380. self.inputs["Bone"].armature = mantis_node.bGetParentArmature()
  381. for cls in TellClasses():
  382. cls.set_mantis_class()