[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [38275] branches/soc-2011-pepper/release/ scripts: Some optimizations and coding style improvements across the retargeting and constraint scripts

Benjy Cook benjycook at hotmail.com
Sat Jul 9 23:52:26 CEST 2011


Revision: 38275
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=38275
Author:   benjycook
Date:     2011-07-09 21:52:25 +0000 (Sat, 09 Jul 2011)
Log Message:
-----------
Some optimizations and coding style improvements across the retargeting and constraint scripts

Modified Paths:
--------------
    branches/soc-2011-pepper/release/scripts/modules/mocap_constraints.py
    branches/soc-2011-pepper/release/scripts/modules/retarget.py
    branches/soc-2011-pepper/release/scripts/startup/ui_mocap.py

Modified: branches/soc-2011-pepper/release/scripts/modules/mocap_constraints.py
===================================================================
--- branches/soc-2011-pepper/release/scripts/modules/mocap_constraints.py	2011-07-09 20:38:41 UTC (rev 38274)
+++ branches/soc-2011-pepper/release/scripts/modules/mocap_constraints.py	2011-07-09 21:52:25 UTC (rev 38275)
@@ -21,15 +21,11 @@
 import bpy
 from mathutils import *
 from bl_operators import nla
+from retarget import hasIKConstraint
 
 ### Utility Functions
 
 
-def hasIKConstraint(pose_bone):
-    #utility function / predicate, returns True if given bone has IK constraint
-    return ("IK" in [constraint.type for constraint in pose_bone.constraints])
-
-
 def getConsObj(bone):
     #utility function - returns related IK target if bone has IK
     ik = [constraint for constraint in bone.constraints if constraint.type == "IK"]
@@ -63,21 +59,18 @@
     real_constraint.name = "Mocap constraint " + str(len(cons_obj.constraints))
     m_constraint.real_constraint_bone = consObjToBone(cons_obj)
     m_constraint.real_constraint = real_constraint.name
-    setConstraint(m_constraint)
+    setConstraint(m_constraint, bpy.context)
 
 
 def removeConstraint(m_constraint, cons_obj):
     oldConstraint = cons_obj.constraints[m_constraint.real_constraint]
+    removeInfluenceFcurve(cons_obj, bpy.context.active_object, oldConstraint)
     cons_obj.constraints.remove(oldConstraint)
 
 ### Update functions. There are 2: UpdateType/UpdateBone
 ### and update for the others.
 
 
-def updateConstraint(self, context):
-    setConstraint(self)
-
-
 def updateConstraintBoneType(m_constraint, context):
     #If the constraint exists, we need to remove it
     #from the old bone
@@ -94,10 +87,24 @@
         addNewConstraint(m_constraint, cons_obj)
 
 
-# Function that copies all settings from m_constraint to the real Blender constraints
-# Is only called when blender constraint already exists
+def setConstraintFraming(m_constraint, context):
+    obj = context.active_object
+    bones = obj.pose.bones
+    bone = bones[m_constraint.constrained_bone]
+    cons_obj = getConsObj(bone)
+    real_constraint = cons_obj.constraints[m_constraint.real_constraint]
+    removeInfluenceFcurve(cons_obj, obj, real_constraint)
+    s, e = m_constraint.s_frame, m_constraint.e_frame
+    s_in, s_out = m_constraint.smooth_in, m_constraint.smooth_out
+    real_constraint.influence = 1
+    real_constraint.keyframe_insert(data_path="influence", frame=s)
+    real_constraint.keyframe_insert(data_path="influence", frame=e)
+    real_constraint.influence = 0
+    real_constraint.keyframe_insert(data_path="influence", frame=s - s_in)
+    real_constraint.keyframe_insert(data_path="influence", frame=e + s_out)
 
-def setConstraintFraming(m_constraint, cons_obj, obj, real_constraint):
+
+def removeInfluenceFcurve(cons_obj, obj, real_constraint):
     if isinstance(cons_obj, bpy.types.PoseBone):
         fcurves = obj.animation_data.action.fcurves
     else:
@@ -107,30 +114,24 @@
     fcurve = [fcurve for fcurve in fcurves if fcurve.data_path == influence_RNA]
     #clear the fcurve and set the frames.
     if fcurve:
-        fcurve = fcurve[0]
-        for i in range(len(fcurve.keyframe_points) - 1, 0, -1):
-            fcurve.keyframe_points.remove(fcurve.keyframe_points[i])
-    s, e = m_constraint.s_frame, m_constraint.e_frame
-    s_in, s_out = m_constraint.smooth_in, m_constraint.smooth_out
-    real_constraint.influence = 1
-    real_constraint.keyframe_insert(data_path="influence", frame=s)
-    real_constraint.keyframe_insert(data_path="influence", frame=e)
-    real_constraint.influence = 0
-    real_constraint.keyframe_insert(data_path="influence", frame=s - s_in)
-    real_constraint.keyframe_insert(data_path="influence", frame=e + s_out)
+        fcurves.remove(fcurve[0])
 
 
-def setConstraint(m_constraint):
+# Function that copies all settings from m_constraint to the real Blender constraints
+# Is only called when blender constraint already exists
+
+
+def setConstraint(m_constraint, context):
     if not m_constraint.constrained_bone:
         return
-    obj = bpy.context.active_object
+    obj = context.active_object
     bones = obj.pose.bones
     bone = bones[m_constraint.constrained_bone]
     cons_obj = getConsObj(bone)
     real_constraint = cons_obj.constraints[m_constraint.real_constraint]
 
     #frame changing section
-    setConstraintFraming(m_constraint, cons_obj, obj, real_constraint)
+    #setConstraintFraming(m_constraint, cons_obj, obj, real_constraint)
 
     #Set the blender constraint parameters
     if m_constraint.type == "point":
@@ -176,17 +177,17 @@
         real_constraint.limit_mode = "LIMITDIST_ONSURFACE"
         real_constraint.distance = m_constraint.targetDist
 
-    # active check
+    # active/baked check
     real_constraint.mute = (not m_constraint.active) and (m_constraint.baked)
 
 
 def updateBake(self, context):
     if self.baked:
         print("baking...")
-        bakeConstraint(self)
+        bakeConstraint(self, context)
     else:
         print("unbaking...")
-        unbakeConstraint(self)
+        unbakeConstraint(self, context)
 
 
 def bakeTransformFK(anim_data, s_frame, e_frame, end_bone, bones, cons_obj):
@@ -210,14 +211,15 @@
     return mute_ik
 
 
-def bakeConstraint(m_constraint):
-    obj = bpy.context.active_object
+def bakeConstraint(m_constraint, context):
+    obj = context.active_object
     bones = obj.pose.bones
     end_bone = bones[m_constraint.constrained_bone]
     cons_obj = getConsObj(end_bone)
-    scene = bpy.context.scene
-    s_frame = scene.frame_start
-    e_frame = scene.frame_end
+    s, e = m_constraint.s_frame, m_constraint.e_frame
+    s_in, s_out = m_constraint.smooth_in, m_constraint.smooth_out
+    s_frame = s - s_in
+    e_frame = e + s_out
     mute_ik = bakeTransformFK(obj.animation_data, s_frame, e_frame, end_bone, bones, cons_obj)
     if mute_ik:
         ik_con = hasIKConstraint(end_bone)
@@ -232,16 +234,14 @@
     constraintStrip.frame_end = e_frame
 
 
-def unbakeConstraint(m_constraint):
+def unbakeConstraint(m_constraint, context):
     # to unbake a constraint we need to delete the whole strip
     # and rebake all the other constraints
-    obj = bpy.context.active_object
+    obj = context.active_object
     bones = obj.pose.bones
     end_bone = bones[m_constraint.constrained_bone]
     cons_obj = getConsObj(end_bone)
     scene = bpy.context.scene
-    s_frame = scene.frame_start
-    e_frame = scene.frame_end
     constraintTrack = obj.animation_data.nla_tracks["Mocap constraints"]
     constraintStrip = constraintTrack.strips[0]
     action = constraintStrip.action
@@ -257,12 +257,3 @@
             ik_con.mute = False
     real_constraint = cons_obj.constraints[m_constraint.real_constraint]
     real_constraint.mute = False
-
-
-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

Modified: branches/soc-2011-pepper/release/scripts/modules/retarget.py
===================================================================
--- branches/soc-2011-pepper/release/scripts/modules/retarget.py	2011-07-09 20:38:41 UTC (rev 38274)
+++ branches/soc-2011-pepper/release/scripts/modules/retarget.py	2011-07-09 21:52:25 UTC (rev 38275)
@@ -22,40 +22,33 @@
 from mathutils import *
 from math import radians, acos
 
-#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
+    # clear any old data
+    for end_bone in end_arm.bones:
+        for mapping in end_bone.reverseMap:
+            end_bone.reverseMap.remove(0)
 
-    # 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)
+    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
+
     #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
+    return feetBones, root
 # list of empties created to keep track of "original"
 # position data
 # in final product, these locations can be stored as custom props
@@ -69,7 +62,7 @@
 # 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,23 +89,18 @@
     #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:

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list