Changed name of Mocap constraints to mocap fixes, for user clarity.
[blender.git] / release / scripts / modules / retarget.py
index 6409c5ed53500bab4c34fd88974dc51c91ec359f..bec7b8aaa3e532bee3460bb265d64c89549a41ea 100644 (file)
 import bpy
 from mathutils import *
 from math import radians, acos
+import cProfile
 
-#TODO: Only selected bones get retargeted.
-#      Selected Bones/chains get original pos empties,
-#      if ppl want IK instead of FK
-#      Some "magic" numbers - frame start and end,
-#      eulers of all orders instead of just quats keyframed
 
-# dictionary of mapping
-# this is currently manuall input'ed, but willW
-# be created from a more comfortable UI in the future
+def hasIKConstraint(pose_bone):
+    #utility function / predicate, returns True if given bone has IK constraint
+    ik = [constraint for constraint in pose_bone.constraints if constraint.type == "IK"]
+    if ik:
+        return ik[0]
+    else:
+        return False
 
 
 def createDictionary(perf_arm, end_arm):
-    bonemap = {}
-    #Bonemap: performer to enduser
-    for bone in perf_arm.bones:
-        bonemap[bone.name] = bone.map
-
-    # creation of a reverse map
-    # multiple keys get mapped to list values
-    #Bonemapr: enduser to performer
-    bonemapr = {}
-    for key, value in bonemap.items():
-        if not value in bonemapr:
-            if isinstance(bonemap[key], tuple):
-                for key_x in bonemap[key]:
-                    bonemapr[key_x] = [key]
-            else:
-                bonemapr[bonemap[key]] = [key]
-        else:
-            bonemapr[bonemap[key]].append(key)
+    # clear any old data
+    for end_bone in end_arm.bones:
+        for mapping in end_bone.reverseMap:
+            end_bone.reverseMap.remove(0)
+
+    for perf_bone in perf_arm.bones:
+        #find its match and add perf_bone to the match's mapping
+        if perf_bone.map:
+            end_bone = end_arm.bones[perf_bone.map]
+            newMap = end_bone.reverseMap.add()
+            newMap.name = perf_bone.name
+            end_bone.foot = perf_bone.foot
+
     #root is the root of the enduser
     root = end_arm.bones[0].name
     feetBones = [bone.name for bone in perf_arm.bones if bone.foot]
-    return bonemap, bonemapr, feetBones, root
-# list of empties created to keep track of "original"
-# position data
-# in final product, these locations can be stored as custom props
-# these help with constraining, etc.
+    return feetBones, root
+
+
+def loadMapping(perf_arm, end_arm):
+
+    for end_bone in end_arm.bones:
+        #find its match and add perf_bone to the match's mapping
+        if end_bone.reverseMap:
+            for perf_bone in end_bone.reverseMap:
+                perf_arm.bones[perf_bone.name].map = end_bone.name
 
 #creation of intermediate armature
 # the intermediate armature has the hiearchy of the end user,
@@ -69,7 +69,7 @@ def createDictionary(perf_arm, end_arm):
 # easily while concentrating on the hierarchy changes
 
 
-def createIntermediate(performer_obj, enduser_obj, bonemap, bonemapr, root, s_frame, e_frame, scene):
+def createIntermediate(performer_obj, enduser_obj, root, s_frame, e_frame, scene):
     #creates and keyframes an empty with its location
     #the original position of the tail bone
     #useful for storing the important data in the original motion
@@ -96,26 +96,18 @@ def createIntermediate(performer_obj, enduser_obj, bonemap, bonemapr, root, s_fr
     #determines the type of hierachy change needed and calls the
     #right function
     def retargetPerfToInter(inter_bone):
-        if inter_bone.name in bonemapr:
-            perf_bone_name = bonemapr[inter_bone.name]
-            #is it a 1 to many?
-            if isinstance(bonemap[perf_bone_name[0]], tuple):
-                pass
+        if inter_bone.bone.reverseMap:
+            perf_bone_name = inter_bone.bone.reverseMap
                 # 1 to many not supported yet
-            else:
                 # then its either a many to 1 or 1 to 1
-
-                if len(perf_bone_name) > 1:
-                    performer_bones_s = [performer_bones[name] for name in perf_bone_name]
-                    #we need to map several performance bone to a single
-                    inter_bone.matrix_basis = manyPerfToSingleInterRetarget(inter_bone, performer_bones_s)
-                else:
-                    perf_bone = performer_bones[perf_bone_name[0]]
-                    inter_bone.matrix_basis = singleBoneRetarget(inter_bone, perf_bone)
-
+            if len(perf_bone_name) > 1:
+                performer_bones_s = [performer_bones[map.name] for map in perf_bone_name]
+                #we need to map several performance bone to a single
+                inter_bone.matrix_basis = manyPerfToSingleInterRetarget(inter_bone, performer_bones_s)
+            else:
+                perf_bone = performer_bones[perf_bone_name[0].name]
+                inter_bone.matrix_basis = singleBoneRetarget(inter_bone, perf_bone)
         inter_bone.keyframe_insert("rotation_quaternion")
-        for child in inter_bone.children:
-            retargetPerfToInter(child)
 
     #creates the intermediate armature object
     inter_obj = enduser_obj.copy()
@@ -124,7 +116,20 @@ def createIntermediate(performer_obj, enduser_obj, bonemap, bonemapr, root, s_fr
     inter_obj.name = "intermediate"
     bpy.context.scene.objects.active = inter_obj
     bpy.ops.object.mode_set(mode='EDIT')
+    #add some temporary connecting bones in case end user bones are not connected to their parents
+    print("creating temp bones")
+    for bone in inter_obj.data.edit_bones:
+        if not bone.use_connect and bone.parent:
+            if inter_obj.data.bones[bone.parent.name].reverseMap or inter_obj.data.bones[bone.name].reverseMap:
+                newBone = inter_obj.data.edit_bones.new("Temp")
+                newBone.head = bone.parent.tail
+                newBone.tail = bone.head
+                newBone.parent = bone.parent
+                bone.parent = newBone
+                bone.use_connect = True
+                newBone.use_connect = True
     #resets roll
+    print("retargeting to intermediate")
     bpy.ops.armature.calculate_roll(type='Z')
     bpy.ops.object.mode_set(mode="OBJECT")
     inter_obj.data.name = "inter_arm"
@@ -133,14 +138,19 @@ def createIntermediate(performer_obj, enduser_obj, bonemap, bonemapr, root, s_fr
     inter_bones = inter_obj.pose.bones
     #clears inheritance
     for inter_bone in inter_bones:
-        inter_bone.bone.use_inherit_rotation = False
+        if inter_bone.bone.reverseMap:
+            inter_bone.bone.use_inherit_rotation = False
+        else:
+            inter_bone.bone.use_inherit_rotation = True
 
     for t in range(s_frame, e_frame):
+        if (t - s_frame) % 10 == 0:
+            print("First pass: retargeting frame {0}/{1}".format(t, e_frame - s_frame))
         scene.frame_set(t)
-        inter_bone = inter_bones[root]
-        retargetPerfToInter(inter_bone)
+        for bone in inter_bones:
+            retargetPerfToInter(bone)
 
-    return inter_obj, inter_arm
+    return inter_obj
 
 # this procedure copies the rotations over from the intermediate
 # armature to the end user one.
@@ -163,7 +173,10 @@ def retargetEnduser(inter_obj, enduser_obj, root, s_frame, e_frame, scene):
         rest_matrix = trg_bone.bone.matrix_local
 
         if trg_bone.parent and trg_bone.bone.use_inherit_rotation:
-            parent_mat = src_bone.parent.matrix
+            srcParent = src_bone.parent
+            if "Temp" in srcParent.name:
+                srcParent = srcParent.parent
+            parent_mat = srcParent.matrix
             parent_rest = trg_bone.parent.bone.matrix_local
             parent_rest_inv = parent_rest.copy()
             parent_rest_inv.invert()
@@ -175,13 +188,23 @@ def retargetEnduser(inter_obj, enduser_obj, root, s_frame, e_frame, scene):
         rest_matrix_inv = rest_matrix.copy()
         rest_matrix_inv.invert()
         bake_matrix = rest_matrix_inv * bake_matrix
-        trg_bone.matrix_basis = bake_matrix
-        end_bone.keyframe_insert("rotation_quaternion")
+        end_bone.matrix_basis = bake_matrix
+        rot_mode = end_bone.rotation_mode
+        if rot_mode == "QUATERNION":
+            end_bone.keyframe_insert("rotation_quaternion")
+        elif rot_mode == "AXIS_ANGLE":
+            end_bone.keyframe_insert("rotation_axis_angle")
+        else:
+            end_bone.keyframe_insert("rotation_euler")
+        if not end_bone.bone.use_connect:
+            end_bone.keyframe_insert("location")
 
         for bone in end_bone.children:
             bakeTransform(bone)
 
     for t in range(s_frame, e_frame):
+        if (t - s_frame) % 10 == 0:
+            print("Second pass: retargeting frame {0}/{1}".format(t, e_frame - s_frame))
         scene.frame_set(t)
         end_bone = end_bones[root]
         end_bone.location = Vector((0, 0, 0))
@@ -193,13 +216,13 @@ def retargetEnduser(inter_obj, enduser_obj, root, s_frame, e_frame, scene):
 # (they don't move, despite root moving) somewhere in the animation.
 
 
-def copyTranslation(performer_obj, enduser_obj, perfFeet, bonemap, bonemapr, root, s_frame, e_frame, scene, enduser_obj_mat):
+def copyTranslation(performer_obj, enduser_obj, perfFeet, root, s_frame, e_frame, scene, enduser_obj_mat):
 
     perf_bones = performer_obj.pose.bones
     end_bones = enduser_obj.pose.bones
 
-    perfRoot = bonemapr[root][0]
-    endFeet = [bonemap[perfBone] for perfBone in perfFeet]
+    perfRoot = perf_bones[0].name
+    endFeet = [perf_bones[perfBone].bone.map for perfBone in perfFeet]
     locDictKeys = perfFeet + endFeet + [perfRoot]
 
     def tailLoc(bone):
@@ -208,7 +231,7 @@ def copyTranslation(performer_obj, enduser_obj, perfFeet, bonemap, bonemapr, roo
     #Step 1 - we create a dict that contains these keys:
     #(Performer) Hips, Feet
     #(End user) Feet
-    # where the values are their world position on each (1,120) frame
+    # where the values are their world position on each frame in range (s,e)
 
     locDict = {}
     for key in locDictKeys:
@@ -231,10 +254,7 @@ def copyTranslation(performer_obj, enduser_obj, perfFeet, bonemap, bonemapr, roo
 
     for key in locDict.keys():
         graph = locDict[key]
-        for t in range(len(graph) - 1):
-            x = graph[t]
-            xh = graph[t + 1]
-            locDeriv[key].append(xh - x)
+        locDeriv[key] = [graph[t + 1] - graph[t] for t in range(len(graph) - 1)]
 
     # now find the plant frames, where perfFeet don't move much
 
@@ -244,7 +264,7 @@ def copyTranslation(performer_obj, enduser_obj, perfFeet, bonemap, bonemapr, roo
         for i in range(len(locDeriv[key]) - 1):
             v = locDeriv[key][i]
             hipV = locDeriv[perfRoot][i]
-            endV = locDeriv[bonemap[key]][i]
+            endV = locDeriv[perf_bones[key].bone.map][i]
             if (v.length < 0.1):
                 #this is a plant frame.
                 #lets see what the original hip delta is, and the corresponding
@@ -257,29 +277,29 @@ def copyTranslation(performer_obj, enduser_obj, perfFeet, bonemap, bonemapr, roo
     stride_bone.name = "stride_bone"
 
     if linearAvg:
+        #determine the average change in scale needed
         avg = sum(linearAvg) / len(linearAvg)
         scene.frame_set(s_frame)
         initialPos = (tailLoc(perf_bones[perfRoot]) / avg)
         for t in range(s_frame, e_frame):
             scene.frame_set(t)
+            #calculate the new position, by dividing by the found ratio between performer and enduser
             newTranslation = (tailLoc(perf_bones[perfRoot]) / avg)
             stride_bone.location = (newTranslation - initialPos) * enduser_obj_mat
             stride_bone.keyframe_insert("location")
     return stride_bone
 
 
-def IKRetarget(bonemap, bonemapr, performer_obj, enduser_obj, s_frame, e_frame, scene):
+def IKRetarget(performer_obj, enduser_obj, s_frame, e_frame, scene):
     end_bones = enduser_obj.pose.bones
     for pose_bone in end_bones:
-        if "IK" in [constraint.type for constraint in pose_bone.constraints]:
+        ik_constraint = hasIKConstraint(pose_bone)
+        if ik_constraint:
             target_is_bone = False
             # set constraint target to corresponding empty if targetless,
             # if not, keyframe current target to corresponding empty
-            perf_bone = bonemapr[pose_bone.name]
-            if isinstance(perf_bone, list):
-                perf_bone = bonemapr[pose_bone.name][-1]
+            perf_bone = pose_bone.bone.reverseMap[-1].name
             orgLocTrg = originalLocationTarget(pose_bone)
-            ik_constraint = [constraint for constraint in pose_bone.constraints if constraint.type == "IK"][0]
             if not ik_constraint.target:
                 ik_constraint.target = orgLocTrg
                 target = orgLocTrg
@@ -292,6 +312,7 @@ def IKRetarget(bonemap, bonemapr, performer_obj, enduser_obj, s_frame, e_frame,
             else:
                 target = ik_constraint.target
 
+            # bake the correct locations for the ik target bones
             for t in range(s_frame, e_frame):
                 scene.frame_set(t)
                 if target_is_bone:
@@ -314,20 +335,22 @@ def turnOffIK(enduser_obj):
             #pose_bone.ik_stiffness_x = 0.5
             #pose_bone.ik_stiffness_y = 0.5
             #pose_bone.ik_stiffness_z = 0.5
-        if "IK" in [constraint.type for constraint in pose_bone.constraints]:
-            ik_constraint = [constraint for constraint in pose_bone.constraints if constraint.type == "IK"][0]
+        ik_constraint = hasIKConstraint(pose_bone)
+        if ik_constraint:
             ik_constraint.mute = True
 
 
+#copy the object matrixes and clear them (to be reinserted later)
 def cleanAndStoreObjMat(performer_obj, enduser_obj):
     perf_obj_mat = performer_obj.matrix_world.copy()
     enduser_obj_mat = enduser_obj.matrix_world.copy()
-    zero_mat = Matrix()  # Matrix(((0,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)))
+    zero_mat = Matrix()
     performer_obj.matrix_world = zero_mat
     enduser_obj.matrix_world = zero_mat
     return perf_obj_mat, enduser_obj_mat
 
 
+#restore the object matrixes after parenting the auto generated IK empties
 def restoreObjMat(performer_obj, enduser_obj, perf_obj_mat, enduser_obj_mat, stride_bone):
     pose_bones = enduser_obj.pose.bones
     for pose_bone in pose_bones:
@@ -339,6 +362,7 @@ def restoreObjMat(performer_obj, enduser_obj, perf_obj_mat, enduser_obj_mat, str
     enduser_obj.parent = stride_bone
 
 
+#create (or return if exists) the related IK empty to the bone
 def originalLocationTarget(end_bone):
     if not end_bone.name + "Org" in bpy.data.objects:
         bpy.ops.object.add()
@@ -350,30 +374,53 @@ def originalLocationTarget(end_bone):
     return empty
 
 
-def totalRetarget():
-    print("retargeting...")
-    enduser_obj = bpy.context.active_object
-    performer_obj = [obj for obj in bpy.context.selected_objects if obj != enduser_obj]
-    if enduser_obj is None or len(performer_obj) != 1:
-        print("Need active and selected armatures")
-    else:
-        performer_obj = performer_obj[0]
+#create the specified NLA setup for base animation, constraints and tweak layer.
+def NLASystemInitialize(enduser_obj, s_frame):
+    anim_data = enduser_obj.animation_data
+    mocapAction = anim_data.action
+    mocapAction.name = "Base Mocap"
+    anim_data.use_nla = True
+    mocapTrack = anim_data.nla_tracks.new()
+    mocapTrack.name = "Base Mocap Track"
+    mocapStrip = mocapTrack.strips.new("Base Mocap", s_frame, mocapAction)
+    constraintTrack = anim_data.nla_tracks.new()
+    constraintTrack.name = "Mocap fixes"
+    constraintAction = bpy.data.actions.new("Mocap fixes")
+    constraintStrip = constraintTrack.strips.new("Mocap fixes", s_frame, constraintAction)
+    constraintStrip.extrapolation = "NOTHING"
+    userTrack = anim_data.nla_tracks.new()
+    userTrack.name = "Mocap manual fix"
+    userAction = bpy.data.actions.new("Mocap manual fix")
+    userStrip = userTrack.strips.new("Mocap manual fix", s_frame, userAction)
+    userStrip.extrapolation = "HOLD"
+    #userStrip.blend_type = "MULITPLY" - doesn't work due to work, will be activated soon
+    anim_data.nla_tracks.active = constraintTrack
+    anim_data.action = constraintAction
+    anim_data.action_extrapolation = "NOTHING"
+
+
+#Main function that runs the retargeting sequence.
+def totalRetarget(performer_obj, enduser_obj, scene, s_frame, e_frame):
     perf_arm = performer_obj.data
     end_arm = enduser_obj.data
-    scene = bpy.context.scene
-    s_frame = scene.frame_start
-    e_frame = scene.frame_end
-    bonemap, bonemapr, feetBones, root = createDictionary(perf_arm, end_arm)
+    print("creating Dictionary")
+    feetBones, root = createDictionary(perf_arm, end_arm)
+    print("cleaning stuff up")
     perf_obj_mat, enduser_obj_mat = cleanAndStoreObjMat(performer_obj, enduser_obj)
     turnOffIK(enduser_obj)
-    inter_obj, inter_arm = createIntermediate(performer_obj, enduser_obj, bonemap, bonemapr, root, s_frame, e_frame, scene)
+    print("Creating intermediate armature (for first pass)")
+    inter_obj = createIntermediate(performer_obj, enduser_obj, root, s_frame, e_frame, scene)
+    print("First pass: retargeting from intermediate to end user")
     retargetEnduser(inter_obj, enduser_obj, root, s_frame, e_frame, scene)
-    stride_bone = copyTranslation(performer_obj, enduser_obj, feetBones, bonemap, bonemapr, root, s_frame, e_frame, scene, enduser_obj_mat)
-    IKRetarget(bonemap, bonemapr, performer_obj, enduser_obj, s_frame, e_frame, scene)
+    print("Second pass: retargeting root translation and clean up")
+    stride_bone = copyTranslation(performer_obj, enduser_obj, feetBones, root, s_frame, e_frame, scene, enduser_obj_mat)
+    IKRetarget(performer_obj, enduser_obj, s_frame, e_frame, scene)
     restoreObjMat(performer_obj, enduser_obj, perf_obj_mat, enduser_obj_mat, stride_bone)
     bpy.ops.object.mode_set(mode='OBJECT')
     bpy.ops.object.select_name(name=inter_obj.name, extend=False)
     bpy.ops.object.delete()
+    NLASystemInitialize(enduser_obj, s_frame)
+    print("retargeting done!")
 
 if __name__ == "__main__":
     totalRetarget()