OSDN Git Service

implement shape key animation.
authorousttrue <ousttrue@gmail.com>
Sun, 16 May 2010 14:59:56 +0000 (23:59 +0900)
committerousttrue <ousttrue@gmail.com>
Sun, 16 May 2010 14:59:56 +0000 (23:59 +0900)
swig/blender25/import_anim_vmd.py

index d24d9fa..923fb34 100644 (file)
@@ -8,7 +8,8 @@ vmd Importer
 
 This script imports a vmd into Blender for editing.
 0.1 2010/05/15 first implement.
-0.2 2010/05/16 implement bone motion.
+0.2 2010/05/16 implement bone animation.
+0.3 2010/05/16 implement shape key animation.
 '''
 
 import bpy
@@ -60,21 +61,11 @@ def import_motion_key(l, o):
             armRotate=bone.matrix_local.rotation_part()
             # quat
             armRotateQuaternion=armRotate.to_quat()
-            #if armRotateQuaternion.w<0:
-            #    armRotateQuaternion.w=-armRotateQuaternion.w
-            #    armRotateQuaternion.x=-armRotateQuaternion.x
-            #    armRotateQuaternion.y=-armRotateQuaternion.y
-            #    armRotateQuaternion.z=-armRotateQuaternion.z
 
             # inv
             armRotateInv=mathutils.Matrix(armRotate).transpose()
             # quat
             armRotateInvQuaternion=armRotateInv.to_quat()
-            #if armRotateInvQuaternion.w<0:
-            #    armRotateInvQuaternion.w=-armRotateInvQuaternion.w
-            #    armRotateInvQuaternion.x=-armRotateInvQuaternion.x
-            #    armRotateInvQuaternion.y=-armRotateInvQuaternion.y
-            #    armRotateInvQuaternion.z=-armRotateInvQuaternion.z
 
             reverseFlag=False
             lastQ=None
@@ -141,39 +132,39 @@ def IPO_CURVE_get_or_create(ipo, name):
     return ipo.addCurve(name)
 
 
-def import_shape_key(l, mesh):
-    print('import_shape_key: %s' % mesh.name)
-    return 0
-    # ToDo
-    key = mesh.getData().key
-    if key is None:
-        Draw.PupMenu('selecting mesh not has a Key')
-        return
-    # get or create IPO  
-    ipo = key.getIpo()
-    if ipo == None:
-        ipo = Blender.Ipo.New("Key", "ShapeKey")
-        key.ipo = ipo
+def getKey(mesh, name):
+    for shapeKey in mesh.shape_keys.keys:
+        if shapeKey.name==name:
+            return shapeKey
+
+
+def import_shape_key(l, meshObject):
+    print('import_shape_key: %s' % meshObject.name)
 
     # insert shape keys
     counter=0
     last_frame=0
+
     for n in l.morphKeys:
         keyFrames=l.getMorphKeyFrameList(n)
         if n=='base':
             continue
         name=englishmap.getEnglishSkinName(n)
-        try:
-            curve=IPO_CURVE_get_or_create(ipo, name)
-        except NameError as msg:
-            print(NameError, msg)
-            print(n, name)
+        if not name:
+            name=n
+
+        shapeKey=getKey(meshObject.data, name)
+
+        if not shapeKey:
+            print("not found shapeKey: %s" % name)
             continue
-        curve.interpolation = Blender.IpoCurve.InterpTypes.LINEAR
-        for i in xrange(len(keyFrames.list)):
+
+        for i in range(len(keyFrames.list)):
             key=keyFrames.getKey(i)
+            shapeKey.value=key.weight
             frame=keyFrames.getFrame(i)
-            curve[frame]=key.weight
+            shapeKey.keyframe_insert("value", -1, frame)
+
             last_frame=max(last_frame, frame)
 
             counter+=1