export matrix was wrong, cleanup import, more memory efficient and import the rest data to an unused key.

This commit is contained in:
Campbell Barton 2006-09-28 04:45:08 +00:00
parent bd6e5d20c7
commit 22ce0faffe
2 changed files with 50 additions and 70 deletions

@ -1,10 +1,10 @@
#!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"
@ -13,17 +13,10 @@ This script Exports Lightwaves MotionDesigner format.
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
@ -34,18 +27,34 @@ try:
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
@ -71,6 +80,7 @@ def mdd_export(filepath, ob, PREF_STARTFRAME, PREF_ENDFRAME, PREF_FPS):
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)

@ -27,9 +27,9 @@ for moving animations from package to package.
#Bill Niewuendorp
try:
import struct
from struct import unpack
except:
struct= None
unpack = None
import Blender
from Blender import Mesh, Object, Scene
@ -44,46 +44,21 @@ def mdd_import(filepath, ob, PREF_IPONAME, PREF_START_FRAME, PREF_JUMP):
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 ...")
@ -96,8 +71,6 @@ def mdd_import(filepath, ob, PREF_IPONAME, PREF_START_FRAME, PREF_JUMP):
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 ...")
@ -108,24 +81,21 @@ def mdd_import(filepath, ob, PREF_IPONAME, PREF_START_FRAME, PREF_JUMP):
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):
for i in xrange(PREF_JUMP+1, len(all_keys), PREF_JUMP):
curve = ipo.getCurve(i)
if curve == None:
ipo.addCurve(all_keys[i])
curve = 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()
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)
@ -161,7 +131,7 @@ def mdd_import_ui(filepath):
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')