1 # SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Contributors: Bill L.Nieuwendorp
8 This script Exports Lightwaves MotionDesigner format.
10 The .mdd format has become quite a popular Pipeline format<br>
11 for moving animations from package to package.
13 Be sure not to use modifiers that change the number or order of verts in the mesh
18 from struct
import pack
21 def zero_file(filepath
):
23 If a file fails, this replaces it with 1 char, better not remove it?
25 file = open(filepath
, 'w')
26 file.write('\n') # apparently macosx needs some data in a blank file?
30 def check_vertcount(mesh
, vertcount
):
32 check and make sure the vertcount is consistent throughout the frame range
34 if len(mesh
.vertices
) != vertcount
:
35 raise Exception('Error, number of verts has changed during animation, cannot export')
38 def save(context
, filepath
="", frame_start
=1, frame_end
=300, fps
=25.0, use_rest_frame
=False):
40 Blender.Window.WaitCursor(1)
42 mesh_orig = Mesh.New()
43 mesh_orig.getFromObject(obj.name)
49 if bpy
.ops
.object.mode_set
.poll():
50 bpy
.ops
.object.mode_set(mode
='OBJECT')
52 orig_frame
= scene
.frame_current
53 scene
.frame_set(frame_start
)
54 depsgraph
= context
.evaluated_depsgraph_get()
55 obj_eval
= obj
.evaluated_get(depsgraph
)
56 me
= obj_eval
.to_mesh()
60 mat_flip = mathutils.Matrix(((1.0, 0.0, 0.0, 0.0),
66 mat_flip
= mathutils
.Matrix()
68 numverts
= len(me
.vertices
)
70 numframes
= frame_end
- frame_start
+ 1
74 f
= open(filepath
, 'wb') # no Errors yet:Safe to create file
77 f
.write(pack(">2i", numframes
, numverts
))
79 # Write the frame times (should we use the time IPO??)
80 f
.write(pack(">%df" % (numframes
), *[frame
/ fps
for frame
in range(numframes
)])) # seconds
83 check_vertcount(me
, numverts
)
84 me
.transform(mat_flip
@ obj
.matrix_world
)
85 f
.write(pack(">%df" % (numverts
* 3), *[axis
for v
in me
.vertices
for axis
in v
.co
]))
87 obj_eval
.to_mesh_clear()
89 for frame
in range(frame_start
, frame_end
+ 1): # in order to start at desired frame
90 scene
.frame_set(frame
)
91 depsgraph
= context
.evaluated_depsgraph_get()
92 obj_eval
= obj
.evaluated_get(depsgraph
)
93 me
= obj_eval
.to_mesh()
94 check_vertcount(me
, numverts
)
95 me
.transform(mat_flip
@ obj
.matrix_world
)
97 # Write the vertex data
98 f
.write(pack(">%df" % (numverts
* 3), *[axis
for v
in me
.vertices
for axis
in v
.co
]))
100 obj_eval
.to_mesh_clear()
104 print('MDD Exported: %r frames:%d\n' % (filepath
, numframes
- 1))
105 scene
.frame_set(orig_frame
)