deformer_definitions.py 8.3 KB

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