#!BPY
"""
- Name: 'Save Mesh RVKs as MDD'
+ Name: 'Save Mesh as MDD'
Blender: 242
Group: 'Animation'
- Tooltip: 'baked vertex animation fromo selected model.'
+ Tooltip: 'Animated mesh to MDD vertex keyframe file.'
"""
__author__ = "Bill L.Nieuwendorp"
The .mdd format has become quite a popular Pipeline format<br>
for moving animations from package to package.
+
+Be sure not to use modifiers that change the number or order of verts in the mesh
"""
-# mdd export
-#
-#
-#
-# Warning if the vertex order or vertex count differs from frame to frame
-# The script will fail because the resulting file would be an invalid mdd file.
-#
-# mdd files should only be applied to the the origonating model with the origonal vert order
-#
-#Please send any fixes,updates,bugs to Slow67_at_Gmail.com
+#Please send any fixes,updates,bugs to Slow67_at_Gmail.com or cbarton_at_metavr.com
#Bill Niewuendorp
import Blender
except:
pack = None
+
+def zero_file(filepath):
+ '''
+ If a file fails, this replaces it with 1 char, better not remove it?
+ '''
+ file = open(filepath, 'w')
+ file.write('\n') # aparently macosx needs some data in a blank file?
+ file.close()
+
def mdd_export(filepath, ob, PREF_STARTFRAME, PREF_ENDFRAME, PREF_FPS):
Window.EditMode(0)
Blender.Window.WaitCursor(1)
mesh_orig = ob.getData(mesh=1)
-
- #Flip y and z matrix
+
+ #Flip y and z
+ '''
+ mat = Mathutils.Matrix()
+ mat[2][2] = -1
+ rotmat = Mathutils.RotationMatrix(90, 4, 'x')
+ mat_flip = mat*rotmat
+ '''
+ # Above results in this matrix
mat_flip= Mathutils.Matrix(\
- [1,0,0,0],\
- [0,0,1,0],\
- [0,-1,0,0],\
- [0,0,0,1],\
+ [1.0, 0.0, 0.0, 0.0],\
+ [0.0, 0.0, 1.0, 0.0],\
+ [0.0, 1.0, 0.0, 0.0],\
+ [0.0, 0.0, 0.0, 1.0],\
)
me_tmp = Mesh.New() # container mesh
Blender.Draw.PupMenu('Error%t|Number of verts has changed during animation|cannot export')
Blender.Window.WaitCursor(0)
f.close() # should we zero?
+ zero_file(filepath)
return
me_tmp.transform(ob.matrixWorld * mat_flip)
#Bill Niewuendorp
try:
- import struct
+ from struct import unpack
except:
- struct= None
+ unpack = None
import Blender
from Blender import Mesh, Object, Scene
Blender.Window.WaitCursor(1)
file = open(filepath, 'rb')
- toUnpack = file.read(8)
- frames, points = struct.unpack(">2i",toUnpack)
- floatBytes = frames * 4
- furtherUnpack = file.read(floatBytes)
- floatBytes2 = points * 12
- floatBytes3 = 12 * points * frames
- restsize = points * 3
- pfsize = points * frames * 3
- restPoseUnpack = file.read(floatBytes2)
- PerFrameUnpack = file.read(floatBytes3)
- pattern = ">%df" % frames
- pattern2 = ">%df" % restsize
- pattern3 = ">%df" % pfsize
- time = struct.unpack(pattern, furtherUnpack)
- rest_pose = struct.unpack(pattern2, restPoseUnpack)
- PerFramexyz = struct.unpack(pattern3, PerFrameUnpack)
+ frames, points = unpack(">2i", file.read(8))
+ time = unpack((">%df" % frames), file.read(frames * 4))
print '\tpoints:%d frames:%d' % (points,frames)
scn = Scene.GetCurrent()
ctx = scn.getRenderingContext()
- #ctx.startFrame(PREF_START_FRAME)
- #ctx.endFrame(PREF_START_FRAME+frames)
Blender.Set("curframe", PREF_START_FRAME)
me = ob.getData(mesh=1)
- xyzs = PerFramexyz
- Point_list = []
- for i in xrange(len(xyzs)/3):
- xpos, zpos = i*3, (i*3)+3
- Point_list.append(xyzs[xpos:zpos])
- Frm_points = []
- Blender.Window.DrawProgressBar (0.2, "3 Importing mdd ...")
- for i in xrange(len(Point_list)):
- first, last = i*points, (i*points)+points
- Frm_points.append(Point_list[first:last])
-
def UpdateMesh(me,fr):
- for vidx, v in enumerate(me.verts):
- v.co[:] = Frm_points[fr][vidx][0], Frm_points[fr][vidx][2], Frm_points[fr][vidx][1]
+ for v in me.verts:
+ # 12 is the size of 3 floats
+ x,y,z= unpack('>3f', file.read(12))
+ v.co[:] = x,z,y
me.update()
-
+
Blender.Window.DrawProgressBar (0.4, "4 Importing mdd ...")
if len(me.verts) > 1 and (curfr >= PREF_START_FRAME) and (curfr <= PREF_START_FRAME+frames):
UpdateMesh(me, i)
ob.insertShapeKey()
- ob.makeDisplayList()
- Blender.Window.RedrawAll()
Blender.Window.DrawProgressBar (0.5, "5 Importing mdd ...")
me.insertKey(1, 'relative')
key= me.key
- key.ipo = Blender.Ipo.New("Key", PREF_IPONAME)
+ key.ipo = Blender.Ipo.New('Key', PREF_IPONAME)
ipo = key.ipo
- block = key.getBlocks()
+ # block = key.getBlocks() # not used.
all_keys = ipo.curveConsts
- for i in xrange(PREF_JUMP, len(all_keys), PREF_JUMP):
- curve = ipo.getCurve(i)
- if curve == None:
- ipo.addCurve(all_keys[i])
-
- Blender.Window.DrawProgressBar (0.8, "appending to ipos")
- for i in xrange(PREF_JUMP, len(all_keys), PREF_JUMP):# Key Reduction
- mkpoints = ipo.getCurve(i)
- mkpoints.append((1+PREF_START_FRAME+i-1,1))
- mkpoints.append((1+PREF_START_FRAME+i- PREF_JUMP -1,0))
- mkpoints.append((1+PREF_START_FRAME+i+ PREF_JUMP-1,0))
- mkpoints.setInterpolation('Linear')
- mkpoints.recalc()
+ for i in xrange(PREF_JUMP+1, len(all_keys), PREF_JUMP):
+ curve = ipo.getCurve(i)
+ if curve == None:
+ curve = ipo.addCurve(all_keys[i])
+
+ curve.append((PREF_START_FRAME+i-1,1))
+ curve.append((PREF_START_FRAME+i- PREF_JUMP -1,0))
+ curve.append((PREF_START_FRAME+i+ PREF_JUMP-1,0))
+ curve.setInterpolation('Linear')
+ curve.recalc()
print 'done'
Blender.Window.WaitCursor(0)
Blender.Set('curframe', orig_frame)
if __name__ == '__main__':
- if not struct:
+ if not unpack:
Draw.PupMenu('Error%t|This script requires a full python install')
Blender.Window.FileSelector(mdd_import_ui, 'IMPORT MDD', '*.mdd')
\ No newline at end of file