deformer_definitions.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import bpy
  2. from bpy.types import NodeTree, Node, NodeSocket
  3. from .base_definitions import MantisUINode, DeformerNode, get_signature_from_edited_tree
  4. from.deformer_socket_templates import *
  5. from .utilities import (prRed, prGreen, prPurple, prWhite, prOrange,
  6. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  7. wrapOrange,)
  8. def TellClasses():
  9. return [
  10. DeformerArmatureNode,
  11. DeformerHook,
  12. DeformerMorphTargetDeform,
  13. DeformerMorphTarget,
  14. DeformerSurfaceDeform,
  15. DeformerMeshDeform,
  16. ]
  17. def default_traverse(self, socket):
  18. if (socket == self.outputs["Deformer"]):
  19. return self.inputs["Deformer"]
  20. if (socket == self.inputs["Deformer"]):
  21. return self.outputs["Deformer"]
  22. return None
  23. class DeformerArmatureNode(Node, DeformerNode):
  24. '''A node representing an Armature Deformer'''
  25. bl_idname = 'DeformerArmature'
  26. bl_label = "Armature Deform"
  27. bl_icon = 'MOD_ARMATURE'
  28. initialized : bpy.props.BoolProperty(default = False)
  29. mantis_node_class_name=bl_idname
  30. def init(self, context):
  31. # self.inputs.new ("RelationshipSocket", "Input Relationship")
  32. self.inputs.new('xFormSocket', "Armature Object")
  33. self.inputs.new('StringSocket', "Blend Vertex Group")
  34. # self.inputs.new('StringSocket', "Preserve Volume Vertex Group") #TODO figure out the right UX for automatic dual-quat blending
  35. self.inputs.new('BooleanSocket', "Invert Vertex Group")
  36. self.inputs.new('BooleanSocket', "Preserve Volume")
  37. # TODO: make the above controlled by a vertex group instead.
  38. self.inputs.new('BooleanSocket', "Use Multi Modifier")# might just set this auto
  39. self.inputs.new('BooleanSocket', "Use Envelopes")
  40. self.inputs.new('BooleanSocket', "Use Vertex Groups")
  41. self.inputs.new("DeformerSocket", "Deformer")
  42. s = self.inputs.new("xFormSocket", "Copy Skin Weights From")
  43. s.hide = True
  44. self.inputs.new("EnumSkinning", "Skinning Method")
  45. self.outputs.new('DeformerSocket', "Deformer")
  46. self.initialized = True
  47. def display_update(self, parsed_tree, context):
  48. self.inputs["Copy Skin Weights From"].hide = True
  49. node_tree = context.space_data.path[0].node_tree
  50. nc = parsed_tree.get(get_signature_from_edited_tree(self, context))
  51. if nc:
  52. self.inputs["Copy Skin Weights From"].hide = not (nc.evaluate_input("Skinning Method") == "COPY_FROM_OBJECT")
  53. class DeformerHook(Node, DeformerNode):
  54. '''A node representing a Hook Deformer'''
  55. bl_idname = 'DeformerHook'
  56. bl_label = "Hook Deform"
  57. bl_icon = 'HOOK'
  58. initialized : bpy.props.BoolProperty(default = False)
  59. mantis_node_class_name=bl_idname
  60. def init(self, context):
  61. self.init_sockets(HookSockets)
  62. self.initialized = True
  63. def display_update(self, parsed_tree, context):
  64. curve_sockets = [
  65. self.inputs["Affect Curve Radius"],
  66. self.inputs['Auto-Bezier'],
  67. self.inputs['Spline Index'],
  68. ]
  69. is_curve_hook=True
  70. if self.outputs["Deformer"].is_linked:
  71. from bpy.types import Object
  72. if (mantis_node := parsed_tree.get(get_signature_from_edited_tree(self, context))):
  73. if (xforms := mantis_node.GetxForm()):
  74. for xF_node in xforms:
  75. if (ob := xF_node.bObject) and isinstance (xF_node, Object):
  76. if ob.type != 'CURVE': is_curve_hook=False
  77. for socket in curve_sockets:
  78. socket.hide=not is_curve_hook
  79. from .utilities import get_socket_maps, relink_socket_map
  80. # TODO this should probably not be in this file but intstead in Utilities or something
  81. def simple_do_relink(node, map, in_out='INPUT'):
  82. from bpy.types import NodeSocket
  83. for key, val in map.items():
  84. s = node.inputs.get(key) if in_out == "INPUT" else node.outputs.get(key)
  85. if s is None:
  86. if in_out == "INPUT":
  87. if node.num_targets > 0:
  88. s = node.inputs["Target."+str(node.num_targets-1).zfill(3)]
  89. else:
  90. continue
  91. if isinstance(val, list):
  92. for sub_val in val:
  93. if isinstance(sub_val, NodeSocket):
  94. if in_out =='INPUT':
  95. node.id_data.links.new(input=sub_val, output=s)
  96. else:
  97. node.id_data.links.new(input=s, output=sub_val)
  98. else:
  99. try:
  100. s.default_value = val
  101. except (AttributeError, ValueError, TypeError): # must be readonly or maybe it doesn't have a d.v.. TypeError if the d.v. is None at this point
  102. pass
  103. # Dynamic
  104. # - each Morph Target gets a MT input
  105. # - each Morph Target gets an influence input
  106. class DeformerMorphTargetDeform(Node, DeformerNode):
  107. '''A node representing a Morph Target Deformer'''
  108. bl_idname = 'DeformerMorphTargetDeform'
  109. bl_label = "Morph Deform"
  110. bl_icon = 'MOD_ARMATURE'
  111. initialized : bpy.props.BoolProperty(default = False)
  112. num_targets : bpy.props.IntProperty(default = 0)
  113. mantis_node_class_name=bl_idname
  114. def init(self, context):
  115. self.id_data.do_live_update = False
  116. self.inputs.new('DeformerSocket', 'Deformer', )
  117. self.inputs.new('BooleanSocket', 'Use Shape Key', )
  118. s = self.inputs.new('BooleanSocket', 'Use Offset', )
  119. s.default_value = True
  120. self.inputs.new('WildcardSocket', '', identifier='__extend__')
  121. self.outputs.new('DeformerSocket', "Deformer")
  122. self.update_morph_deformer()
  123. def update_morph_deformer(self, force=False):
  124. self.initialized = False
  125. # use_offset = self.inputs["Use Offset"].default_value
  126. socket_maps = get_socket_maps(self)
  127. if socket_maps is None:
  128. return
  129. input_map = socket_maps[0]
  130. # checc to see if targets have been removed... then modify the input map if necessary
  131. targets_deleted = 0 # this should usually be either 0 or 1
  132. for i in range(self.num_targets):
  133. name = "Target."+str(i).zfill(3); inf_name = "Value."+str(i).zfill(3)
  134. if self.inputs[name].is_linked == False:
  135. del input_map[name]; del input_map[inf_name]
  136. targets_deleted+=1
  137. elif targets_deleted: # move it back
  138. new_name = "Target."+str(i-targets_deleted).zfill(3); new_inf_name = "Value."+str(i-targets_deleted).zfill(3)
  139. input_map[new_name] = input_map[name]; input_map[new_inf_name] = input_map[inf_name]
  140. del input_map[name]; del input_map[inf_name]
  141. self.num_targets-=targets_deleted
  142. if self.inputs[-1].is_linked and self.inputs[-1].bl_idname == 'WildcardSocket':
  143. self.num_targets+=1
  144. self.inputs.clear()
  145. self.inputs.new('DeformerSocket', 'Deformer', )
  146. self.inputs.new('BooleanSocket', 'Use Shape Key', )
  147. self.inputs.new('BooleanSocket', 'Use Offset', )
  148. for i in range(self.num_targets):
  149. self.inputs.new("MorphTargetSocket", "Target."+str(i).zfill(3))
  150. self.inputs.new("FloatSocket", "Value."+str(i).zfill(3))
  151. simple_do_relink(self, input_map, in_out='INPUT')
  152. if self.inputs[-1].bl_idname not in ["WildcardSocket"]:
  153. self.inputs.new('WildcardSocket', '', identifier='__extend__')
  154. self.initialized = True
  155. def update(self):
  156. if self.id_data.is_exporting:
  157. return # so that we don't update it while saving/loading the tree
  158. self.update_morph_deformer(force=False)
  159. def display_update(self, parsed_tree, context):
  160. if self.inputs["Deformer"].is_linked:
  161. if self.inputs["Deformer"].links[0].from_node.bl_idname not in [self.bl_idname, "NodeGroupInput"]:
  162. self.inputs["Use Shape Key"].default_value = False
  163. self.inputs["Use Shape Key"].hide = True
  164. elif self.inputs["Deformer"].links[0].from_node.inputs["Use Shape Key"].default_value == False:
  165. self.inputs["Use Shape Key"].default_value = False
  166. self.inputs["Use Shape Key"].hide = True
  167. if self.inputs["Use Offset"] == False:
  168. self.inputs["Use Shape Key"].hide = True
  169. self.inputs["Use Shape Key"].default_value = False
  170. # TODO: there is no reason for this to be a separate node!
  171. class DeformerMorphTarget(Node, DeformerNode):
  172. '''A node representing a single Morph Target'''
  173. bl_idname = 'DeformerMorphTarget'
  174. bl_label = "Morph Target"
  175. bl_icon = 'SHAPEKEY_DATA'
  176. initialized : bpy.props.BoolProperty(default = False)
  177. num_targets : bpy.props.IntProperty(default = 0)
  178. mantis_node_class_name=bl_idname
  179. def init(self, context):
  180. self.inputs.new('xFormSocket', "Relative to")
  181. self.inputs.new('xFormSocket', "Object")
  182. self.inputs.new('StringSocket', "Vertex Group")
  183. self.outputs.new('MorphTargetSocket', "Morph Target")
  184. self.initialized = True
  185. class DeformerSurfaceDeform(Node, DeformerNode):
  186. '''A node representing a Surface Deform modifier'''
  187. bl_idname = 'DeformerSurfaceDeform'
  188. bl_label = "Surface Deform"
  189. bl_icon = 'MOD_SOFT'
  190. initialized : bpy.props.BoolProperty(default = False)
  191. num_targets : bpy.props.IntProperty(default = 0)
  192. mantis_node_class_name=bl_idname
  193. def init(self, context):
  194. self.init_sockets(SurfaceDeformSockets)
  195. self.initialized = True
  196. class DeformerMeshDeform(Node, DeformerNode):
  197. '''A node representing a Mesh Deform modifier'''
  198. bl_idname = 'DeformerMeshDeform'
  199. bl_label = "Mesh Deform"
  200. bl_icon = 'MOD_SOFT'
  201. initialized : bpy.props.BoolProperty(default = False)
  202. num_targets : bpy.props.IntProperty(default = 0)
  203. mantis_node_class_name=bl_idname
  204. def init(self, context):
  205. self.init_sockets(MeshDeformSockets)
  206. self.initialized = True
  207. # Set up the class property that ties the UI classes to the Mantis classes.
  208. for cls in TellClasses():
  209. cls.set_mantis_class()