[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4014] trunk/py/scripts/addons/ io_import_scene_mhx.py: Lipsync for new makehuman visemes.

Thomas Larsson thomas_larsson_01 at hotmail.com
Tue Nov 27 21:55:17 CET 2012


Revision: 4014
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4014
Author:   thomasl
Date:     2012-11-27 20:55:16 +0000 (Tue, 27 Nov 2012)
Log Message:
-----------
Lipsync for new makehuman visemes.

Modified Paths:
--------------
    trunk/py/scripts/addons/io_import_scene_mhx.py

Modified: trunk/py/scripts/addons/io_import_scene_mhx.py
===================================================================
--- trunk/py/scripts/addons/io_import_scene_mhx.py	2012-11-27 07:05:08 UTC (rev 4013)
+++ trunk/py/scripts/addons/io_import_scene_mhx.py	2012-11-27 20:55:16 UTC (rev 4014)
@@ -302,9 +302,8 @@
             #MyError(msg)
 
     scn.objects.active = theArmature
-    if not alpha7:
-        theArmature["MHAlpha8"] = True
-        #bpy.ops.wm.properties_edit(data_path="object", property="MhxRig", value=theArmature["MhxRig"])
+    theArmature.MhAlpha8 = not alpha7
+    #bpy.ops.wm.properties_edit(data_path="object", property="MhxRig", value=theArmature["MhxRig"])
         
     time2 = time.clock()
     print("toggle = %x" % toggle)
@@ -1516,7 +1515,10 @@
             prop = "Mhe" + val[0].capitalize()
             parseUnits(prop, ob, sub)
         elif key == 'Viseme':
-            prop = "Mhv" + val[0].upper()
+            name = val[0].upper()
+            if name in ["REST", "ETC"]:
+                name = name.capitalize()
+            prop = "Mhv" + name
             parseUnits(prop, ob, sub)            
     ob.active_shape_key_index = 0
     print("Shapekeys parsed")
@@ -1529,8 +1531,6 @@
         unit = words[0].replace("-","_")
         value = words[1][0]
         string += "%s:%s;" % (unit, value)
-    print(prop)
-    print("  ", string)
     rig = ob.parent
     rig[prop] = string
 
@@ -2160,11 +2160,11 @@
     elif string[0] == "*":
         string = "Mhs"+string[1:]
         alpha7 = True
-    elif string[0:4] == "Hide":
+    elif string.startswith("Hide"):
         string = "Mhh"+string[4:]
         alpha7 = True
     
-    if string[0:3] in ["Mha", "Mhf", "Mhs", "Mhh", "Mhv"]:
+    if string.startswith(("Mha", "Mhf", "Mhs", "Mhh", "Mhv")):
         name = string.replace("-","_")
         return name, name
     elif string[0] == "_":
@@ -3163,7 +3163,7 @@
     'Blink' : [('PUpLid', (0,1.0)), ('PLoLid', (0,-1.0))], 
     'Unblink' : [('PUpLid', (0,0)), ('PLoLid', (0,0))], 
 })
-"""
+
 bodyLanguageVisemes = ({
     'Rest' : [
         ('MouthWidth_L', 0), 
@@ -3448,13 +3448,13 @@
     def execute(self, context):
         makeVisemes(context.object, context.scene)
         return{'FINISHED'}    
-       
+"""       
 #
 #    mohoVisemes
 #    magpieVisemes
 #
 
-mohoVisemes = dict({
+MohoVisemes = dict({
     'rest' : 'Rest', 
     'etc' : 'Etc', 
     'AI' : 'AH', 
@@ -3467,7 +3467,7 @@
     'FV' : 'FV', 
 })
 
-magpieVisemes = dict({
+MagpieVisemes = dict({
     "CONS" : "t,d,k,g,T,D,s,z,S,Z,h,n,N,j,r,tS", 
     "AI" : "i,&,V,aU,I,0,@,aI", 
     "E" : "eI,3,e", 
@@ -3484,7 +3484,7 @@
 #    setBoneLocation(context, pbone, loc, mirror, setKey, frame):
 #    class VIEW3D_OT_MhxVisemeButton(bpy.types.Operator):
 #
-
+"""
 def getVisemeSet(context, rig):
     try:
         visset = rig['MhxVisemeSet']
@@ -3498,7 +3498,7 @@
         raise MhxError("Unknown viseme set %s" % visset)
 
 
-def setViseme(context, vis, setKey, frame):
+def setVisemeAlpha7(context, vis, visemes, setKey, frame):
     (rig, mesh) = getMhxRigMesh(context.object)
     isPanel = False
     isProp = False
@@ -3512,7 +3512,7 @@
             isProp = True
     elif mesh:
         shapekeys = mesh.data.shape_keys.key_blocks
-    visemes = getVisemeSet(context, rig)
+
     for (skey, value) in visemes[vis]:
         if isPanel:
             (b, (x,z)) = VisemePanelBones[skey]
@@ -3530,7 +3530,7 @@
                 continue
             rig[skey] = value*scale
             if setKey or context.tool_settings.use_keyframe_insert_auto:
-                rig.keyframe_insert(skey, frame=frame, group="Visemes")    
+                rig.keyframe_insert('["%s"]' % skey, frame=frame, group="Visemes")    
         elif shapekeys:
             try:
                 shapekeys[skey].value = value*scale
@@ -3540,8 +3540,8 @@
                 shapekeys[skey].keyframe_insert("value", frame=frame)            
     updatePose(context)
     return
-    
 
+
 class VIEW3D_OT_MhxVisemeButton(bpy.types.Operator):
     bl_idname = 'mhx.pose_viseme'
     bl_label = 'Viseme'
@@ -3549,15 +3549,15 @@
     viseme = StringProperty()
 
     def invoke(self, context, event):
-        setViseme(context, self.viseme, False, context.scene.frame_current)
+        (rig, mesh) = getMhxRigMesh(context.object)
+        visemes = getVisemeSet(context, rig)
+        setVisemeAlpha7(context, self.viseme, visemes, False, context.scene.frame_current)
         return{'FINISHED'}
 
+"""
 
-
 #
-#    openFile(context, filepath):
-#    readMoho(context, filepath, offs):
-#    readMagpie(context, filepath, offs):
+#    readLipsync(context, filepath, offs, struct)
 #
 
 def openFile(context, filepath):
@@ -3565,91 +3565,79 @@
     (name, ext) = os.path.splitext(fileName)
     return open(filepath, "rU")
 
-def readMoho(context, filepath, offs):
+
+def readLipsync(context, filepath, offs, struct):
     (rig, mesh) = getMhxRigMesh(context.object)
+    if rig.MhAlpha8:
+        props = getProps(rig, "Mhv")
+        visemes = {}
+        oldKeys = []
+        for prop in props:
+            dummy,units = getUnitsFromString("x;"+rig[prop])
+            print("VV", prop, units)
+            visemes[prop] = units
+        props = getProps(rig, "Mhsmouth")
+        auto = context.tool_settings.use_keyframe_insert_auto
+        auto = True
+        factor = rig.MhxStrength
+    else:
+        visemes = getVisemeSet(context, rig)    
     context.scene.objects.active = rig
     bpy.ops.object.mode_set(mode='POSE')    
+    
     fp = openFile(context, filepath)        
     for line in fp:
         words= line.split()
         if len(words) < 2:
-            pass
+            continue
         else:
-            vis = mohoVisemes[words[1]]
-            setViseme(context, vis, True, int(words[0])+offs)
+            vis = "Mhv" + struct[words[1]]
+            frame = int(words[0])+offs
+        if rig.MhAlpha8:
+            print("Frame", frame, words[1], vis)
+            print("  ", visemes[vis])
+            setMhmProps(rig, "Mhsmouth", visemes[vis], factor, auto, frame)
+        else:
+            setVisemeAlpha7(context, vis, visemes, True, frame)
     fp.close()
+    
     setInterpolation(rig)
     updatePose(context)
-    print("Moho file %s loaded" % filepath)
-    return
+    print("Lipsync file %s loaded" % filepath)
 
-def readMagpie(context, filepath, offs):
-    rig,mesh = getMhxRigMesh(context.object)
-    context.scene.objects.active = rig
-    bpy.ops.object.mode_set(mode='POSE')    
-    fp = openFile(context, filepath)        
-    for line in fp: 
-        words= line.split()
-        if len(words) < 3:
-            pass
-        elif words[2] == 'X':
-            vis = magpieVisemes[words[3]]
-            setViseme(context, vis, True, int(words[0])+offs)
-    fp.close()
-    setInterpolation(rig)
-    updatePose(context)
-    print("Magpie file %s loaded" % filepath)
-    return
 
-# 
-#    class VIEW3D_OT_MhxLoadMohoButton(bpy.types.Operator):
-#
-
-class VIEW3D_OT_MhxLoadMohoButton(bpy.types.Operator):
-    bl_idname = "mhx.pose_load_moho"
+class VIEW3D_OT_MhxLipsyncButton(bpy.types.Operator, ImportHelper):
+    bl_idname = "mhx.pose_load_lipsync"
     bl_label = "Moho (.dat)"
     bl_options = {'UNDO'}
-    filepath = StringProperty(subtype='FILE_PATH')
+
+    type = StringProperty()
     startFrame = IntProperty(name="Start frame", description="First frame to import", default=1)
 
-    def execute(self, context):
-        import bpy, os, mathutils
-        readMoho(context, self.properties.filepath, self.properties.startFrame-1)        
-        return{'FINISHED'}    
+    filename_ext = ".dat"
+    filter_glob = StringProperty(default="*.dat", options={'HIDDEN'})
+    filepath = StringProperty(subtype='FILE_PATH')    
 
-    def invoke(self, context, event):
-        context.window_manager.fileselect_add(self)
-        return {'RUNNING_MODAL'}    
-
-#
-#    class VIEW3D_OT_MhxLoadMagpieButton(bpy.types.Operator):
-#
-
-class VIEW3D_OT_MhxLoadMagpieButton(bpy.types.Operator):
-    bl_idname = "mhx.pose_load_magpie"
-    bl_label = "Magpie (.mag)"
-    filepath = StringProperty(subtype='FILE_PATH')
-    startFrame = IntProperty(name="Start frame", description="First frame to import", default=1)
-
     def execute(self, context):
         import bpy, os, mathutils
-        readMagpie(context, self.properties.filepath, self.properties.startFrame-1)        
+        if self.type == "Moho":
+            struct = MohoVisemes
+        elif self.type == "Magpie":
+            struct = MagpieVisemes
+        readLipsync(context, self.properties.filepath, self.properties.startFrame-1, struct)        
         return{'FINISHED'}    
 
     def invoke(self, context, event):
         context.window_manager.fileselect_add(self)
         return {'RUNNING_MODAL'}    
 
-#
-#    class MhxLipsyncPanel(bpy.types.Panel):
-#
 
 class MhxLipsyncPanel(bpy.types.Panel):
     bl_label = "MHX Lipsync"
     bl_space_type = "VIEW_3D"
     bl_region_type = "UI"
     bl_options = {'DEFAULT_CLOSED'}
-    
+
     @classmethod
     def poll(cls, context):
         return pollMhx(context.object)
@@ -3658,23 +3646,31 @@
         rig,mesh = getMhxRigMesh(context.object)
         if not rig:
             return
-        try:
-            alpha8 = rig["MHAlpha8"]
-        except:
-            alpha8 = False
-
         layout = self.layout        
-        layout.label(text="Visemes")
         
-        if alpha8:
+        if rig.MhAlpha8:
             visemes = getProps(rig, "Mhv")
-            layout.operator("mhx.pose_reset_expressions")
-            layout.operator("mhx.pose_key_expressions")
+            layout.operator("mhx.pose_reset_expressions", text="Reset visemes").prefix="Mhsmouth"
+            layout.operator("mhx.pose_key_expressions", text="Key visemes").prefix="Mhsmouth"
             layout.prop(rig, "MhxStrength")
             layout.separator()
+            n = 0
             for prop in visemes:
-                layout.operator("mhx.pose_mhm", text=prop[3:]).data=rig[prop]        
-        else:         
+                if n % 3 == 0:
+                    row = layout.row()
+                    n = 0
+                row.operator("mhx.pose_mhm", text=prop[3:]).data="Mhsmouth;"+rig[prop]        
+                n += 1
+            while n % 3 != 0:
+                row.label("")
+                n += 1
+            layout.separator()
+            row = layout.row()

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list