deformer_definitions.py 9.1 KB

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