deformer_definitions.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 (xF_node := mantis_node.GetxForm()):
  72. if (ob := xF_node.bObject) and isinstance (xF_node, Object):
  73. if ob.type != 'CURVE': is_curve_hook=False
  74. for socket in curve_sockets:
  75. socket.hide=not is_curve_hook
  76. from .utilities import get_socket_maps, relink_socket_map
  77. # TODO this should probably not be in this file but intstead in Utilities or something
  78. def simple_do_relink(node, map, in_out='INPUT'):
  79. from bpy.types import NodeSocket
  80. for key, val in map.items():
  81. s = node.inputs.get(key) if in_out == "INPUT" else node.outputs.get(key)
  82. if s is None:
  83. if in_out == "INPUT":
  84. if node.num_targets > 0:
  85. s = node.inputs["Target."+str(node.num_targets-1).zfill(3)]
  86. else:
  87. continue
  88. if isinstance(val, list):
  89. for sub_val in val:
  90. if isinstance(sub_val, NodeSocket):
  91. if in_out =='INPUT':
  92. node.id_data.links.new(input=sub_val, output=s)
  93. else:
  94. node.id_data.links.new(input=s, output=sub_val)
  95. else:
  96. try:
  97. s.default_value = val
  98. 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
  99. pass
  100. # Dynamic
  101. # - each Morph Target gets a MT input
  102. # - each Morph Target gets an influence input
  103. class DeformerMorphTargetDeform(Node, DeformerNode):
  104. '''A node representing a Morph Target Deformer'''
  105. bl_idname = 'DeformerMorphTargetDeform'
  106. bl_label = "Morph Deform"
  107. bl_icon = 'MOD_ARMATURE'
  108. initialized : bpy.props.BoolProperty(default = False)
  109. num_targets : bpy.props.IntProperty(default = 0)
  110. mantis_node_class_name=bl_idname
  111. def init(self, context):
  112. self.id_data.do_live_update = False
  113. self.inputs.new('DeformerSocket', 'Deformer', )
  114. self.inputs.new('BooleanSocket', 'Use Shape Key', )
  115. s = self.inputs.new('BooleanSocket', 'Use Offset', )
  116. s.default_value = True
  117. self.inputs.new('WildcardSocket', '', identifier='__extend__')
  118. self.outputs.new('DeformerSocket', "Deformer")
  119. self.update_morph_deformer()
  120. def update_morph_deformer(self, force=False):
  121. self.initialized = False
  122. # use_offset = self.inputs["Use Offset"].default_value
  123. socket_maps = get_socket_maps(self)
  124. if socket_maps is None:
  125. return
  126. input_map = socket_maps[0]
  127. # checc to see if targets have been removed... then modify the input map if necessary
  128. targets_deleted = 0 # this should usually be either 0 or 1
  129. for i in range(self.num_targets):
  130. name = "Target."+str(i).zfill(3); inf_name = "Value."+str(i).zfill(3)
  131. if self.inputs[name].is_linked == False:
  132. del input_map[name]; del input_map[inf_name]
  133. targets_deleted+=1
  134. elif targets_deleted: # move it back
  135. new_name = "Target."+str(i-targets_deleted).zfill(3); new_inf_name = "Value."+str(i-targets_deleted).zfill(3)
  136. input_map[new_name] = input_map[name]; input_map[new_inf_name] = input_map[inf_name]
  137. del input_map[name]; del input_map[inf_name]
  138. self.num_targets-=targets_deleted
  139. if self.inputs[-1].is_linked and self.inputs[-1].bl_idname == 'WildcardSocket':
  140. self.num_targets+=1
  141. self.inputs.clear()
  142. self.inputs.new('DeformerSocket', 'Deformer', )
  143. self.inputs.new('BooleanSocket', 'Use Shape Key', )
  144. self.inputs.new('BooleanSocket', 'Use Offset', )
  145. for i in range(self.num_targets):
  146. self.inputs.new("MorphTargetSocket", "Target."+str(i).zfill(3))
  147. self.inputs.new("FloatSocket", "Value."+str(i).zfill(3))
  148. simple_do_relink(self, input_map, in_out='INPUT')
  149. if self.inputs[-1].bl_idname not in ["WildcardSocket"]:
  150. self.inputs.new('WildcardSocket', '', identifier='__extend__')
  151. self.initialized = True
  152. def update(self):
  153. if self.id_data.is_executing:
  154. return # so that we don't update it while saving/loading the tree
  155. def display_update(self, parsed_tree, context):
  156. if self.inputs["Deformer"].is_linked:
  157. if self.inputs["Deformer"].links[0].from_node.bl_idname not in [self.bl_idname, "NodeGroupInput"]:
  158. self.inputs["Use Shape Key"].default_value = False
  159. self.inputs["Use Shape Key"].hide = True
  160. elif self.inputs["Deformer"].links[0].from_node.inputs["Use Shape Key"].default_value == False:
  161. self.inputs["Use Shape Key"].default_value = False
  162. self.inputs["Use Shape Key"].hide = True
  163. if self.inputs["Use Offset"] == False:
  164. self.inputs["Use Shape Key"].hide = True
  165. self.inputs["Use Shape Key"].default_value = False
  166. # TODO: there is no reason for this to be a separate node!
  167. class DeformerMorphTarget(Node, DeformerNode):
  168. '''A node representing a single Morph Target'''
  169. bl_idname = 'DeformerMorphTarget'
  170. bl_label = "Morph Target"
  171. bl_icon = 'SHAPEKEY_DATA'
  172. initialized : bpy.props.BoolProperty(default = False)
  173. num_targets : bpy.props.IntProperty(default = 0)
  174. mantis_node_class_name=bl_idname
  175. def init(self, context):
  176. self.inputs.new('xFormSocket', "Relative to")
  177. self.inputs.new('xFormSocket', "Object")
  178. self.inputs.new('StringSocket', "Vertex Group")
  179. self.outputs.new('MorphTargetSocket', "Morph Target")
  180. self.initialized = True
  181. # Set up the class property that ties the UI classes to the Mantis classes.
  182. for cls in TellClasses():
  183. cls.set_mantis_class()