xForm_definitions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. def TellClasses():
  10. return [
  11. # xFormNullNode,
  12. xFormBoneNode,
  13. xFormRootNode,
  14. xFormArmatureNode,
  15. xFormGeometryObjectNode,
  16. ]
  17. def default_traverse(self, socket):
  18. if (socket == self.outputs["xForm Out"]):
  19. return self.inputs["Relationship"]
  20. if (socket == self.inputs["Relationship"]):
  21. return self.outputs["xForm Out"]
  22. return None
  23. # Representing an Empty or non-armature-Object
  24. # class xFormNullNode(Node, xFormNode):
  25. # '''A node representing a Null node'''
  26. # bl_idname = 'xFormNullNode'
  27. # bl_label = "Null"
  28. # bl_icon = 'EMPTY_AXIS'
  29. # # === Optional Functions ===
  30. # def init(self, context):
  31. # self.inputs.new('StringSocket', "Name")
  32. # self.inputs.new('RelationshipSocket', "Relationship")
  33. # self.inputs.new('RotationOrderSocket', "Rotation Order")
  34. # self.inputs.new('MatrixSocket', "Matrix")
  35. # self.outputs.new('xFormSocket', "xForm Out")
  36. def check_if_connected(start, end, line):
  37. started=False
  38. for path_nc in line:
  39. prWhite(" ", path_nc.signature)
  40. if path_nc.signature == start.signature:
  41. started = True
  42. elif path_nc.signature == end.signature:
  43. break
  44. if started:
  45. if path_nc.inputs.get("Connected"):
  46. if path_nc.evaluate_input("Connected") == False:
  47. return False
  48. else:
  49. return False
  50. return True
  51. class xFormRootNode(Node, xFormNode):
  52. '''A node representing the world node'''
  53. bl_idname = 'xFormRootNode'
  54. bl_label = "World Root"
  55. bl_icon = 'WORLD'
  56. initialized : bpy.props.BoolProperty(default = False)
  57. def init(self, context):
  58. self.outputs.new('RelationshipSocket', "World Out")
  59. self.initialized=True
  60. # I had chat gpt flip these so they may be a little innacurate
  61. # always visible
  62. main_names = {
  63. "Name":'StringSocket',
  64. "Rotation Order":'RotationOrderSocket',
  65. "Relationship":'RelationshipSocket',
  66. "Matrix":'MatrixSocket',}
  67. # IK SETTINGS
  68. ik_names = {
  69. "IK Stretch":'FloatFactorSocket',
  70. "Lock IK":'BooleanThreeTupleSocket',
  71. "IK Stiffness":'NodeSocketVector',
  72. "Limit IK":'BooleanThreeTupleSocket',
  73. "X Min":'NodeSocketFloatAngle',
  74. "X Max":'NodeSocketFloatAngle',
  75. "Y Min":'NodeSocketFloatAngle',
  76. "Y Max":'NodeSocketFloatAngle',
  77. "Z Min":'NodeSocketFloatAngle',
  78. "Z Max":'NodeSocketFloatAngle',
  79. }
  80. #display settings
  81. display_names = {
  82. "Bone Collection":'BoneCollectionSocket',
  83. "Custom Object":'xFormSocket',
  84. "Custom Object xForm Override":'xFormSocket',
  85. "Custom Object Scale to Bone Length":'BooleanSocket',
  86. "Custom Object Wireframe":'BooleanSocket',
  87. "Custom Object Scale":'VectorScaleSocket',
  88. "Custom Object Translation":'VectorSocket',
  89. "Custom Object Rotation":'VectorEulerSocket',
  90. }
  91. # deform_names
  92. deform_names = {
  93. "Deform":'BooleanSocket',
  94. "Envelope Distance":'FloatPositiveSocket',
  95. "Envelope Weight":'FloatFactorSocket',
  96. "Envelope Multiply":'BooleanSocket',
  97. "Envelope Head Radius":'FloatPositiveSocket',
  98. "Envelope Tail Radius":'FloatPositiveSocket',
  99. }
  100. bbone_names = {
  101. "BBone Segments":"IntSocket", # BONE
  102. "BBone X Size":"FloatSocket", # BONE
  103. "BBone Z Size":"FloatSocket", # BONE
  104. # "bbone_mapping_mode":"StringSocket", <== BONE
  105. "BBone HQ Deformation":"BooleanSocket", # BONE bbone_mapping_mode
  106. "BBone X Curve-In":"FloatSocket", # BONE AND POSE
  107. "BBone Z Curve-In":"FloatSocket", # BONE AND POSE
  108. "BBone X Curve-Out":"FloatSocket", # BONE AND POSE
  109. "BBone Z Curve-Out":"FloatSocket", # BONE AND POSE
  110. "BBone Roll-In":"FloatSocket", # BONE AND POSE
  111. "BBone Roll-Out":"FloatSocket", # BONE AND POSE
  112. "BBone Inherit End Roll":"BooleanSocket", # BONE
  113. "BBone Scale-In":"VectorSocket", # BONE AND POSE
  114. "BBone Scale-Out":"VectorSocket", # BONE AND POSE
  115. "BBone Ease-In":"FloatSocket", # BONE AND POSE
  116. "BBone Ease-Out":"FloatSocket", # BONE AND POSE
  117. "BBone Easing":"BooleanSocket", # BONE
  118. "BBone Start Handle Type":"EnumBBoneHandleType", # BONE
  119. "BBone Custom Start Handle":"StringSocket", # BONE
  120. "BBone Start Handle Scale":"BooleanThreeTupleSocket", # BONE
  121. "BBone Start Handle Ease":"BooleanSocket", # BONE
  122. "BBone End Handle Type":"EnumBBoneHandleType", # BONE
  123. "BBone Custom End Handle":"StringSocket", # BONE
  124. "BBone End Handle Scale":"BooleanThreeTupleSocket", # BONE
  125. "BBone End Handle Ease":"BooleanSocket", # BONE
  126. }
  127. other_names = {
  128. "Lock Location":'BooleanThreeTupleSocket',
  129. "Lock Rotation":'BooleanThreeTupleSocket',
  130. "Lock Scale":'BooleanThreeTupleSocket',
  131. "Hide":'HideSocket',
  132. }
  133. from mathutils import Color
  134. xFormColor = Color((0.093172, 0.047735, 0.028036)).from_scene_linear_to_srgb()
  135. class xFormBoneNode(Node, xFormNode):
  136. '''A node representing a Bone'''
  137. bl_idname = 'xFormBoneNode'
  138. bl_label = "Bone"
  139. bl_icon = 'BONE_DATA'
  140. display_ik_settings : bpy.props.BoolProperty(default=False)
  141. display_vp_settings : bpy.props.BoolProperty(default=False)
  142. display_def_settings : bpy.props.BoolProperty(default=False)
  143. display_bb_settings : bpy.props.BoolProperty(default=False)
  144. socket_count : bpy.props.IntProperty()
  145. initialized : bpy.props.BoolProperty(default = False)
  146. def init(self, context):
  147. for name, sock_type in main_names.items():
  148. self.inputs.new(sock_type, name)
  149. for name, sock_type in ik_names.items():
  150. s = self.inputs.new(sock_type, name)
  151. s.hide = True
  152. for name, sock_type in display_names.items():
  153. s = self.inputs.new(sock_type, name)
  154. if s.name in ['Custom Object', 'Bone Collection']:
  155. continue
  156. s.hide = True
  157. for name, sock_type in deform_names.items():
  158. s = self.inputs.new(sock_type, name)
  159. if s.name == 'Deform':
  160. continue
  161. s.hide = True
  162. for name, sock_type in bbone_names.items():
  163. s = self.inputs.new(sock_type, name)
  164. if s.name == "BBone Segments":
  165. continue
  166. s.hide = True
  167. for name, sock_type in other_names.items():
  168. self.inputs.new(sock_type, name)
  169. # could probably simplify this further with iter_tools.chain() but meh
  170. self.socket_count = len(self.inputs)
  171. #
  172. self.outputs.new('xFormSocket', "xForm Out")
  173. # set up some defaults...
  174. self.inputs['Rotation Order'].default_value = "XYZ"
  175. self.inputs['Lock Location'].default_value[0] = True
  176. self.inputs['Lock Location'].default_value[1] = True
  177. self.inputs['Lock Location'].default_value[2] = True
  178. self.inputs['Lock Rotation'].default_value[0] = True
  179. self.inputs['Lock Rotation'].default_value[1] = True
  180. self.inputs['Lock Rotation'].default_value[2] = True
  181. self.inputs['Lock Scale'].default_value[0] = True
  182. self.inputs['Lock Scale'].default_value[1] = True
  183. self.inputs['Lock Scale'].default_value[2] = True
  184. # color
  185. self.use_custom_color = True
  186. self.color = xFormColor
  187. #
  188. self.initialized=True
  189. def draw_buttons(self, context, layout):
  190. # return
  191. layout.operator("mantis.add_custom_property", text='+Add Custom Parameter')
  192. # layout.label(text="Edit Parameter ... not implemented")
  193. if (len(self.inputs) >= self.socket_count):
  194. layout.operator("mantis.remove_custom_property", text='-Remove Custom Parameter')
  195. else:
  196. layout.label(text="")
  197. def display_update(self, parsed_tree, context):
  198. if context.space_data:
  199. nc = parsed_tree.get(get_signature_from_edited_tree(self, context))
  200. other_nc = None
  201. if len(self.inputs.get("Relationship").links)>0:
  202. prev_node = self.inputs.get("Relationship").links[0].from_node
  203. if prev_node:
  204. other_nc = parsed_tree.get(get_signature_from_edited_tree(prev_node, context))
  205. if nc and other_nc:
  206. self.display_vp_settings = nc.inputs["Custom Object"].is_connected
  207. self.display_def_settings = nc.evaluate_input("Deform")
  208. self.display_bb_settings = nc.evaluate_input("BBone Segments") > 1
  209. self.display_ik_settings = False
  210. #
  211. from .node_container_common import ( trace_all_lines_up,
  212. trace_single_line)
  213. #TODO find a much faster way to do this
  214. if False and self.id_data.do_live_update: #TODO this is extremely freaking slow
  215. trace = trace_all_lines_up(nc, "xForm Out")
  216. for key in trace.keys():
  217. if (ik_nc:= parsed_tree.get(key)):
  218. if ik_nc.__class__.__name__ in ["LinkInverseKinematics"]:
  219. # if the tree is invalid? This shouldn't be necessary.
  220. if ik_nc.inputs["Input Relationship"].is_connected:
  221. chain_count = ik_nc.evaluate_input("Chain Length")
  222. # if chain_count == 0: # this is wrong
  223. # self.display_ik_settings = True
  224. # else:
  225. if ik_nc.evaluate_input("Use Tail") == False:
  226. chain_count+=1
  227. for line in trace[key]:
  228. # preprocess it to get rid of non-xForms:
  229. xForm_line=[]
  230. for path_nc in line:
  231. if path_nc == ik_nc:
  232. if ik_nc.inputs["Input Relationship"].links[0].from_node != prev_path_nc:
  233. break # not a constraint connection
  234. if path_nc.node_type == 'XFORM':
  235. xForm_line.append(path_nc)
  236. prev_path_nc = path_nc
  237. else: # if it can finish cleaning the line of non-xForms and still has a connection
  238. if (len(xForm_line) < chain_count) or (chain_count == 0):
  239. self.display_ik_settings = True
  240. inp = nc.inputs["Relationship"]
  241. link = None
  242. if inp.is_connected:
  243. link = inp.links[0]
  244. while(link):
  245. if link.from_node.__class__.__name__ in ["LinkInverseKinematics"]:
  246. self.display_ik_settings = link.from_node.evaluate_input("Use Tail")
  247. break
  248. inp = link.from_node.outputs[link.from_socket]
  249. inp = inp.traverse_target
  250. if not inp:
  251. break
  252. if inp.links:
  253. link = inp.links[0]
  254. else:
  255. link = None
  256. for name in ik_names.keys():
  257. self.inputs[name].hide = not self.display_ik_settings
  258. for name in display_names.keys():
  259. if name in ['Custom Object', 'Bone Collection']: continue
  260. self.inputs[name].hide = not self.display_vp_settings
  261. for name in deform_names.keys():
  262. if name in ['Deform']: continue
  263. self.inputs[name].hide = not self.display_def_settings
  264. for name in bbone_names.keys():
  265. if name in ['BBone Segments']: continue
  266. self.inputs[name].hide = not self.display_bb_settings
  267. # def update(self):
  268. # self.display_update()
  269. # def copy(ectype, archtype):
  270. # # TODO: automatically avoid duplicating names
  271. # ectype.inputs["Name"].default_value = ""
  272. class xFormArmatureNode(Node, xFormNode):
  273. '''A node representing an Armature object node'''
  274. bl_idname = 'xFormArmatureNode'
  275. bl_label = "Armature"
  276. bl_icon = 'OUTLINER_OB_ARMATURE'
  277. initialized : bpy.props.BoolProperty(default = False)
  278. def init(self, context):
  279. self.inputs.new('StringSocket', "Name")
  280. self.inputs.new('RelationshipSocket', "Relationship")
  281. self.inputs.new('RotationOrderSocket', "Rotation Order")
  282. self.inputs.new('MatrixSocket', "Matrix")
  283. self.outputs.new('xFormSocket', "xForm Out")
  284. # color
  285. self.use_custom_color = True
  286. self.color = xFormColor
  287. self.initialized=True
  288. class xFormGeometryObjectNode(Node, xFormNode):
  289. """Represents a curve or mesh object."""
  290. bl_idname = "xFormGeometryObject"
  291. bl_label = "Geometry Object"
  292. bl_icon = "EMPTY_AXIS"
  293. initialized : bpy.props.BoolProperty(default = False)
  294. def init(self, context):
  295. self.inputs.new('StringSocket', "Name")
  296. self.inputs.new('GeometrySocket', "Geometry")
  297. self.inputs.new('MatrixSocket', "Matrix")
  298. self.inputs.new('RelationshipSocket', "Relationship")
  299. self.inputs.new('DeformerSocket', "Deformer")
  300. self.inputs.new ("HideSocket", "Hide in Viewport")
  301. self.inputs.new ("BooleanSocket", "Hide in Render")
  302. self.outputs.new('xFormSocket', "xForm Out")
  303. # color
  304. self.use_custom_color = True
  305. self.color = xFormColor
  306. self.initialized=True