[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4002] trunk/py/scripts/addons/ io_import_scene_mhx.py: MHX importer: Handles expressions and not just units.

Thomas Larsson thomas_larsson_01 at hotmail.com
Sun Nov 25 10:01:18 CET 2012


Revision: 4002
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4002
Author:   thomasl
Date:     2012-11-25 09:01:13 +0000 (Sun, 25 Nov 2012)
Log Message:
-----------
MHX importer: Handles expressions and not just units. Prettier user interface.

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-24 13:23:54 UTC (rev 4001)
+++ trunk/py/scripts/addons/io_import_scene_mhx.py	2012-11-25 09:01:13 UTC (rev 4002)
@@ -26,7 +26,7 @@
 """
 Abstract
 MHX (MakeHuman eXchange format) importer for Blender 2.5x.
-Version 1.13.2
+Version 1.14.0
 
 This script should be distributed with Blender.
 If not, place it in the .blender/scripts/addons dir
@@ -39,8 +39,8 @@
 bl_info = {
     'name': 'Import: MakeHuman (.mhx)',
     'author': 'Thomas Larsson',
-    'version': (1, 13, 2),
-    "blender": (2, 6, 3),
+    'version': (1, 14, 0),
+    "blender": (2, 6, 4),
     'location': "File > Import > MakeHuman (.mhx)",
     'description': 'Import files in the MakeHuman eXchange format (.mhx)',
     'warning': '',
@@ -50,8 +50,9 @@
     'category': 'Import-Export'}
 
 MAJOR_VERSION = 1
-MINOR_VERSION = 13
-SUB_VERSION = 3
+MINOR_VERSION = 14
+FROM_VERSION = 13
+SUB_VERSION = 0
 
 #
 #
@@ -325,7 +326,7 @@
 def checkMhxVersion(major, minor):
     global warnedVersion
     print("MHX", (major,minor), (MAJOR_VERSION, MINOR_VERSION), warnedVersion)
-    if  major != MAJOR_VERSION or minor != MINOR_VERSION:
+    if  major != MAJOR_VERSION or minor < FROM_VERSION:
         if warnedVersion:
             return
         else:
@@ -333,7 +334,7 @@
 "Wrong MHX version\n" +
 "Expected MHX %d.%02d but the loaded file " % (MAJOR_VERSION, MINOR_VERSION) +
 "has version MHX %d.%02d\n" % (major, minor))
-            if minor < MINOR_VERSION:
+            if minor < FROM_VERSION:
                 msg += (
 "You can disable this error message by deselecting the \n" +
 "Enforce version option when importing. Better:\n" +
@@ -746,7 +747,14 @@
     targ.id = loadedData[dtype][name]
     #print("    ->", targ.id)
     for (key, val, sub) in tokens:
-        defaultKey(key, val, sub, 'targ', [], globals(), locals())
+        if key == 'data_path':
+            words = val[0].split('"')
+            if len(words) > 1:
+                targ.data_path = propNames(words[1])[1]
+            else:
+                targ.data_path = propNames(val)[1]
+        else:
+            defaultKey(key, val, sub, 'targ', [], globals(), locals())
     return targ
 
     
@@ -1496,6 +1504,17 @@
         elif key == 'AnimationData':
             if me.shape_keys:
                 parseAnimationData(me.shape_keys, val, sub)
+        elif key == 'Expression':
+            prop = "Mhe" + val[0].capitalize()
+            string = ""
+            for words in sub:
+                unit = words[0].replace("-","_")
+                value = words[1][0]
+                string += "%s:%s;" % (unit, value)
+            print(prop)
+            print("  ", string)
+            rig = ob.parent
+            rig[prop] = string
     ob.active_shape_key_index = 0
     print("Shapekeys parsed")
     return
@@ -2112,36 +2131,75 @@
 #    defaultKey(ext, args, tokens, var, exclude, glbals, lcals):
 #
 
-thePropTip = ""
+theProperty = None
 
+def propNames(string):
+    if string[0] == "&":
+        name = "Mha" + string[1:].replace("-","_")
+        return name,name
+    elif string[0] == "*":
+        name = "Mhs" + string[1:].replace("-","_")
+        return name,name
+    elif string[0:4] == "Hide":
+        name =  "Mhh" + string[4:]
+        return name, '["%s"]' % name
+    elif string[0] == "_":
+        return None,None
+    else:
+        return string,string
+        
+def setProperty(args, var, glbals, lcals):
+    global theProperty
+    tip = ""
+    name = propNames(args[0])[0]
+    value = args[1]
+    if name:
+        expr = '%s["%s"] = %s' % (var, name, value)
+        exec(expr, glbals, lcals)
+        print(expr)
+        
+        if len(args) > 2:
+            tip = 'description="%s"' % args[2].replace("_", " ")
+        if value in ["True", "False"]:
+            proptype = "Bool"
+        elif value[0] in ["'",'"']:
+            proptype = "String"
+        elif "." in value:
+            proptype = "Float"
+        else:
+            proptype = "Int"
+        theProperty = (name, tip, proptype)
+        #if proptype == "Int":
+        #    halt
+    return
+
+
+def defineProperty(args):
+    global theProperty
+    if theProperty is None:
+        return
+    (name, tip, proptype) = theProperty
+    if len(args) >= 2 and proptype != "Bool":
+        if "BOOLEAN" in args[1]:
+            proptype = "Bool"
+        else:
+            tip = tip + ", " + args[1].replace(":", "=").replace('"', "")
+    expr = "bpy.types.Object.%s = %sProperty(%s)" % (name, proptype, tip)
+    print(expr)
+    exec(expr)
+    if proptype == "Int":
+        halt
+    theProperty = None
+    return
+
+
 def defaultKey(ext, args, tokens, var, exclude, glbals, lcals):
-    global todo, thePropTip
+    global todo
 
     if ext == 'Property':
-        thePropTip = ""
-        try:
-            expr = '%s["%s"] = %s' % (var, args[0], args[1])
-        except:
-            expr = None
-        #print("Property", expr)
-        if expr:
-            exec(expr, glbals, lcals)
-            if len(args) > 2:
-                thePropTip =  '"description":"%s"' % args[2].replace("_", " ")
-        return
+        return setProperty(args, var, glbals, lcals)        
     elif ext == 'PropKeys':
-        if len(args) < 2:
-            values = '{%s}' % thePropTip
-        else:
-            values = '{%s%s}' % (args[1], thePropTip)        
-        try:
-            expr = '%s["_RNA_UI"]["%s"] = %s' % (var, args[0], values)
-        except:
-            expr = None
-        #print("PropKeys", expr)
-        if expr:
-            exec(expr, glbals, lcals)
-        return
+        return defineProperty(args)
 
     if ext == 'bpyops':
         expr = "bpy.ops.%s" % args[0]
@@ -3613,8 +3671,8 @@
 
     def execute(self, context):
         rig,mesh = getMhxRigMesh(context.object)
-        props = getShapeProps(rig)
-        for (prop, name) in props:
+        props = getProps(rig, "Mhs")
+        for prop in props:
             rig[prop] = 0.0
         updatePose(context)
         return{'FINISHED'}    
@@ -3630,12 +3688,13 @@
 
     def execute(self, context):
         rig,mesh = getMhxRigMesh(context.object)
-        props = getShapeProps(rig)
+        props = getProps(rig, "Mhs")
         frame = context.scene.frame_current
-        for (prop, name) in props:
+        for prop in props:
             rig.keyframe_insert('["%s"]' % prop, frame=frame)
         updatePose(context)
-        return{'FINISHED'}    
+        return{'FINISHED'}   
+        
 #
 #    class VIEW3D_OT_MhxPinExpressionButton(bpy.types.Operator):
 #
@@ -3648,10 +3707,10 @@
 
     def execute(self, context):
         rig,mesh = getMhxRigMesh(context.object)
-        props = getShapeProps(rig)
+        props = getProps(rig, "Mhs")
         if context.tool_settings.use_keyframe_insert_auto:
             frame = context.scene.frame_current
-            for (prop, name) in props:
+            for prop in props:
                 old = rig[prop]
                 if prop == self.expression:
                     rig[prop] = 1.0
@@ -3660,7 +3719,7 @@
                 if abs(rig[prop] - old) > 1e-3:
                     rig.keyframe_insert('["%s"]' % prop, frame=frame)
         else:                    
-            for (prop, name) in props:
+            for prop in props:
                 if prop == self.expression:
                     rig[prop] = 1.0
                 else:
@@ -3669,16 +3728,50 @@
         return{'FINISHED'}    
 
 #
-#   getShapeProps(ob):        
+#    class VIEW3D_OT_MhxMhmButton(bpy.types.Operator):
 #
 
-def getShapeProps(rig):
+def setMhmProps(rig, string, factor):
+    words = string.split(";")
+    units = []
+    print(string)
+    for word in words:
+        if word == "":
+            continue
+        unit = word.split(":") 
+        prop = "Mhs" + unit[0]
+        value = float(unit[1])
+        rig[prop] = factor*value
+        print(prop, " = ", value)
+            
+
+class VIEW3D_OT_MhxMhmButton(bpy.types.Operator):
+    bl_idname = "mhx.pose_mhm"
+    bl_label = "Mhm"
+    bl_options = {'UNDO'}
+    data = StringProperty()
+
+    def execute(self, context):   
+        rig,mesh = getMhxRigMesh(context.object)
+        props = getProps(rig, "Mhs")
+        for prop in props:
+            rig[prop] = 0.0
+        setMhmProps(rig, self.data, rig.MhxStrength)
+        updatePose(context)
+        return{'FINISHED'}  
+                
+
+#
+#   getProps(ob, prefix):        
+#
+
+def getProps(rig, prefix):
     props = []        
     plist = list(rig.keys())
     plist.sort()
     for prop in plist:
-        if prop[0] == '*':
-            props.append((prop, prop[1:]))
+        if prop[0:3] == prefix:
+            props.append(prop)
     return props                
 
 #
@@ -3705,17 +3798,28 @@
             layout.label("No shapekey drivers.")
             layout.label("Set expression values in mesh context instead")
             return
-        props = getShapeProps(rig)
+        props = getProps(rig, "Mhs")
         if not props:
             return
-        layout.label(text="Expressions")
+            
         layout.operator("mhx.pose_reset_expressions")
         layout.operator("mhx.pose_key_expressions")
         #layout.operator("mhx.update")
+
+        exprs = getProps(rig, "Mhe")
+        if exprs:
+            layout.prop(rig, "MhxStrength")
+            layout.separator()
+            layout.label(text="Expressions")
+            for prop in exprs:
+                if prop[0:3] == "Mhe":
+                    layout.operator("mhx.pose_mhm", text=prop[3:]).data=rig[prop]
+        
         layout.separator()
-        for (prop, name) in props:
+        layout.label(text="Expressions Units")
+        for prop in props:
             row = layout.split(0.85)
-            row.prop(rig, '["%s"]' % prop, text=name)
+            row.prop(rig, '["%s"]' % prop, text=prop[3:])
             row.operator("mhx.pose_pin_expression", text="", icon='UNPINNED').expression = prop
         return
 
@@ -3813,7 +3917,7 @@
     matchPoseRotation(loarmFk, loarmFk, auto)
     matchPoseScale(loarmFk, loarmFk, auto)
 
-    if rig["&HandFollowsWrist" + suffix]:
+    if rig["MhaHandFollowsWrist" + suffix]:
         matchPoseRotation(handFk, wrist, auto)
         matchPoseScale(handFk, wrist, auto)
     return
@@ -3864,7 +3968,7 @@
     #rig["&KneeFollowsHip" + suffix] = False

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list