[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2090] trunk/py/scripts/addons/ io_import_scene_mhx.py: Mhx importer v 1.5.0: can now use Rigify.

Thomas Larsson thomas_larsson_01 at hotmail.com
Mon Jul 4 19:22:50 CEST 2011


Revision: 2090
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2090
Author:   thomasl
Date:     2011-07-04 17:22:49 +0000 (Mon, 04 Jul 2011)
Log Message:
-----------
Mhx importer v 1.5.0: can now use Rigify.

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	2011-07-04 03:24:18 UTC (rev 2089)
+++ trunk/py/scripts/addons/io_import_scene_mhx.py	2011-07-04 17:22:49 UTC (rev 2090)
@@ -26,7 +26,7 @@
 """
 Abstract
 MHX (MakeHuman eXchange format) importer for Blender 2.5x.
-Version 1.4.0
+Version 1.5.0
 
 This script should be distributed with Blender.
 If not, place it in the .blender/scripts/addons dir
@@ -39,9 +39,9 @@
 bl_info = {
     'name': 'Import: MakeHuman (.mhx)',
     'author': 'Thomas Larsson',
-    'version': (1, 4, 0),
-    "blender": (2, 5, 7),
-    "api": 35774,
+    'version': (1, 5, 0),
+    "blender": (2, 5, 8),
+    "api": 37702,
     'location': "File > Import > MakeHuman (.mhx)",
     'description': 'Import files in the MakeHuman eXchange format (.mhx)',
     'warning': '',
@@ -51,9 +51,9 @@
     'category': 'Import-Export'}
 
 MAJOR_VERSION = 1
-MINOR_VERSION = 4
+MINOR_VERSION = 5
 SUB_VERSION = 0
-BLENDER_VERSION = (2, 57, 0)
+BLENDER_VERSION = (2, 58, 0)
 
 #
 #
@@ -114,7 +114,8 @@
 T_Opcns = 0x2000
 T_Symm = 0x4000
 
-toggle = T_EnforceVersion + T_Replace + T_Mesh + T_Armature + T_Face + T_Shape + T_Proxy + T_Clothes
+toggle = (T_EnforceVersion + T_Replace + T_Mesh + T_Armature + 
+        T_Face + T_Shape + T_Proxy + T_Clothes + T_Rigify)
 
 #
 #    Blender versions
@@ -481,6 +482,12 @@
             hideLayers(val)
         elif key == "CorrectRig":
             correctRig(val)
+        elif key == "Rigify":
+            rig = loadedData['Object'][val[0]]
+            rig['MhxRigify'] = True
+            bpy.context.scene.objects.active = rig
+            if toggle & T_Rigify:
+                rigifyMhx(bpy.context, rig)
         elif key == 'AnimationData':
             try:
                 ob = loadedData['Object'][val[0]]
@@ -1532,11 +1539,6 @@
     obname = args[1]
     mode = args[2]
     
-    if mode == 'Rigify':
-        toggle |= T_Rigify
-        return parseRigify(amtname, obname, tokens)
-
-    toggle &= ~T_Rigify
     amt = bpy.data.armatures.new(amtname)
     ob = createObject('ARMATURE', obname, amt, amtname)    
 
@@ -1574,52 +1576,6 @@
             defaultKey(key, val,  sub, "amt", ['MetaRig'], globals(), locals())
     bpy.ops.object.mode_set(mode='OBJECT')
     return amt
-
-#
-#    parseRigify(amtname, obname, tokens):        
-#
-
-def parseRigify(amtname, obname, tokens):        
-    (key,val,sub) = tokens[0]
-    if key != 'MetaRig':
-        raise NameError("Expected MetaRig")
-    typ = val[0]
-    if typ == "human":
-        bpy.ops.object.armature_human_advanced_add()
-    else:
-        bpy.ops.pose.metarig_sample_add(type = typ)
-    ob = bpy.context.scene.objects.active
-    amt = ob.data
-    loadedData['Rigify'][obname] = ob
-    loadedData['Armature'][amtname] = amt
-    loadedData['Object'][obname] = ob
-    print("Rigify object", ob, amt)
-
-    bpy.ops.object.mode_set(mode='OBJECT')
-    bpy.ops.object.mode_set(mode='EDIT')
-
-    heads = {}
-    tails = {}
-    for (bname, bone) in amt.edit_bones.items():
-        heads[bname] = 10*theScale*bone.head
-        tails[bname] = 10*theScale*bone.tail
-
-    for (key, val, sub) in tokens:
-        if key == 'Bone':
-            bname = val[0]
-            print("Bone", bname)
-            try:
-                bone = amt.edit_bones[bname]
-            except:
-                print("Did not find bone %s" % bname)
-                bone = None
-            print(" -> ", bone)
-            if bone:
-                parseBone(bone, amt, sub, heads, tails)
-        else:
-            defaultKey(key, val,  sub, "amt", ['MetaRig'], globals(), locals())
-    bpy.ops.object.mode_set(mode='OBJECT')
-    return amt
         
 #
 #    parseBone(bone, amt, tokens, heads, tails):
@@ -1654,8 +1610,6 @@
 
 def parsePose (args, tokens):
     global todo
-    if toggle & T_Rigify:
-        return
     name = args[0]
     ob = loadedData['Object'][name]
     bpy.context.scene.objects.active = ob
@@ -2084,21 +2038,6 @@
         ob = None
     if toggle & T_Diamond == 0 and ob:
         deleteDiamonds(ob)
-    if toggle & T_Rigify and False:
-        for rig in loadedData['Rigify'].values():
-            bpy.context.scene.objects.active = rig
-            print("Rigify", rig)
-            bpy.ops.pose.metarig_generate()
-            print("Metarig generated")
-            #bpy.context.scene.objects.unlink(rig)
-
-            rig = bpy.context.scene.objects.active
-            print("Rigged", rig, bpy.context.object)
-            ob = loadedData['Object'][human]
-            mod = ob.modifiers[0]
-            print(ob, mod, mod.object)
-            mod.object = rig
-            print("Rig changed", mod.object)
     return            
 
 #
@@ -2611,9 +2550,321 @@
     fp.close()
     return
 
+###################################################################################
 #
+#   Postprocessing of rigify rig
+#
+#   rigifyMhx(context, mhx):
+#
+###################################################################################
+
+def rigifyMhx(context, mhx):
+    print("Modifying MHX rig to Rigify")
+    # Delete widgets
+    scn = context.scene 
+    for ob in scn.objects:
+        if ob.type == 'MESH' and ob.name[0:3] == "WGT":
+            scn.objects.unlink(ob)
+
+    # Save mhx bone locations    
+    name = mhx.name
+    heads = {}
+    tails = {}
+    rolls = {}
+    parents = {}
+    extras = {}
+    bpy.ops.object.mode_set(mode='EDIT')
+
+    newParents = {
+        'head' : 'DEF-head',
+        'ribs' : 'DEF-ribs',
+        'upper_arm.L' : 'DEF-upper_arm.L.02',
+        'thigh.L' : 'DEF-thigh.L.02',
+        'upper_arm.R' : 'DEF-upper_arm.R.02',
+        'thigh.R' : 'DEF-thigh.R.02',
+    }
+
+    for eb in mhx.data.edit_bones:
+        heads[eb.name] = eb.head.copy()
+        tails[eb.name] = eb.tail.copy()
+        rolls[eb.name] = eb.roll
+        if eb.parent:            
+            par = eb.parent.name
+            print(eb.name, par)
+            try:
+                parents[eb.name] = newParents[par]
+            except:
+                parents[eb.name] = par
+        else:
+            parents[eb.name] = None
+        extras[eb.name] = not eb.layers[16]
+    bpy.ops.object.mode_set(mode='OBJECT')
+   
+    # Find corresponding meshes. Can be several (clothes etc.)   
+    meshes = []
+    for ob in scn.objects:
+        for mod in ob.modifiers:
+            if (mod.type == 'ARMATURE' and mod.object == mhx):
+                meshes.append((ob, mod))
+    if meshes == []:
+        raise NameError("Did not find matching mesh")
+        
+    # Rename Head vertex group    
+    for (mesh, mod) in meshes:
+        try:
+            vg = mesh.vertex_groups['DfmHead']
+            vg.name = 'DEF-head'
+        except:
+            pass
+
+    # Change rigify bone locations    
+    scn.objects.active = None 
+    try:
+        bpy.ops.object.armature_human_advanced_add()
+        success = True
+    except:
+        success = False
+    if not success:
+        raise NameError("Unable to create advanced human. Make sure that the Rigify add-on is enabled. It is found under Rigging.")
+        return
+
+    rigify = context.object
+    bpy.ops.object.mode_set(mode='EDIT')
+    for eb in rigify.data.edit_bones:
+        eb.head = heads[eb.name]
+        eb.tail = tails[eb.name]
+        eb.roll = rolls[eb.name]
+        extras[eb.name] = False
+
+    fingerPlanes = [
+        ('UP-thumb.L', 'thumb.01.L', 'thumb.03.L', ['thumb.02.L']),
+        ('UP-index.L', 'finger_index.01.L', 'finger_index.03.L', ['finger_index.02.L']),
+        ('UP-middle.L', 'finger_middle.01.L', 'finger_middle.03.L', ['finger_middle.02.L']),
+        ('UP-ring.L', 'finger_ring.01.L', 'finger_ring.03.L', ['finger_ring.02.L']),
+        ('UP-pinky.L', 'finger_pinky.01.L', 'finger_pinky.03.L', ['finger_pinky.02.L']),
+        ('UP-thumb.R', 'thumb.01.R', 'thumb.03.R', ['thumb.02.R']),
+        ('UP-index.R', 'finger_index.01.R', 'finger_index.03.R', ['finger_index.02.R']),
+        ('UP-middle.R', 'finger_middle.01.R', 'finger_middle.03.R', ['finger_middle.02.R']),
+        ('UP-ring.R', 'finger_ring.01.R', 'finger_ring.03.R', ['finger_ring.02.R']),
+        ('UP-pinky.R', 'finger_pinky.01.R', 'finger_pinky.03.R', ['finger_pinky.02.R']),
+    ]
+
+    for (upbone, first, last, middles) in fingerPlanes:
+        extras[upbone] = False
+        #lineateChain(upbone, first, last, middles, 0.01, rigify, heads, tails)
+
+    ikPlanes = [
+        ('UP-leg.L', 'thigh.L', 'shin.L'),
+        ('UP-arm.L', 'upper_arm.L', 'forearm.L'),
+        ('UP-leg.R', 'thigh.R', 'shin.R'),
+        ('UP-arm.R', 'upper_arm.R', 'forearm.R'),
+    ]
+
+    for (upbone, first, last) in ikPlanes:
+        extras[upbone] = False
+        lineateChain(upbone, first, last, [], 0.1, rigify, heads, tails)
+
+    bpy.ops.object.mode_set(mode='OBJECT')
+
+    # Generate meta rig    
+    bpy.ops.pose.rigify_generate()
+    scn.objects.unlink(rigify)
+    meta = context.object
+    meta.name = name+"Meta"
+    meta.show_x_ray = True
+    for (mesh, mod) in meshes:
+        mod.object = meta
+
+    # Parent widgets under empty
+    empty = bpy.data.objects.new("Widgets", None)
+    scn.objects.link(empty)
+    empty.layers = 20*[False]
+    empty.layers[19] = True
+    for ob in scn.objects:
+        if ob.type == 'MESH' and ob.name[0:4] == "WGT-" and not ob.parent:
+            ob.parent = empty
+
+    # Copy extra bones to meta rig
+    bpy.ops.object.mode_set(mode='EDIT')
+    for name in heads.keys():
+        if extras[name]:
+            eb = meta.data.edit_bones.new(name)
+            eb.head = heads[name]
+            eb.tail = tails[name]
+            eb.roll = rolls[name]            
+    for name in heads.keys():
+        if extras[name] and parents[name]:
+            eb = meta.data.edit_bones[name]
+            eb.parent = meta.data.edit_bones[parents[name]]
+
+    # Copy constraints etc.
+    bpy.ops.object.mode_set(mode='POSE')
+    for name in heads.keys():
+        if extras[name]:
+            pb1 = mhx.pose.bones[name]
+            pb2 = meta.pose.bones[name]
+            pb2.custom_shape = pb1.custom_shape
+            pb2.lock_location = pb1.lock_location
+            pb2.lock_rotation = pb1.lock_rotation
+            pb2.lock_scale = pb1.lock_scale
+            b1 = pb1.bone
+            b2 = pb2.bone
+            b2.use_deform = b1.use_deform

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list