1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
21 # Contributors: Bill L.Nieuwendorp
24 This script Exports Lightwaves MotionDesigner format.
26 The .mdd format has become quite a popular Pipeline format<br>
27 for moving animations from package to package.
29 Be sure not to use modifiers that change the number or order of verts in the mesh
34 from struct
import pack
37 def zero_file(filepath
):
39 If a file fails, this replaces it with 1 char, better not remove it?
41 file = open(filepath
, 'w')
42 file.write('\n') # apparently macosx needs some data in a blank file?
46 def check_vertcount(mesh
, vertcount
):
48 check and make sure the vertcount is consistent throughout the frame range
50 if len(mesh
.vertices
) != vertcount
:
51 raise Exception('Error, number of verts has changed during animation, cannot export')
54 def save(context
, filepath
="", frame_start
=1, frame_end
=300, fps
=25.0, use_rest_frame
=False):
56 Blender.Window.WaitCursor(1)
58 mesh_orig = Mesh.New()
59 mesh_orig.getFromObject(obj.name)
65 if bpy
.ops
.object.mode_set
.poll():
66 bpy
.ops
.object.mode_set(mode
='OBJECT')
68 orig_frame
= scene
.frame_current
69 scene
.frame_set(frame_start
)
70 depsgraph
= context
.evaluated_depsgraph_get()
71 obj_eval
= obj
.evaluated_get(depsgraph
)
72 me
= obj_eval
.to_mesh()
76 mat_flip = mathutils.Matrix(((1.0, 0.0, 0.0, 0.0),
82 mat_flip
= mathutils
.Matrix()
84 numverts
= len(me
.vertices
)
86 numframes
= frame_end
- frame_start
+ 1
90 f
= open(filepath
, 'wb') # no Errors yet:Safe to create file
93 f
.write(pack(">2i", numframes
, numverts
))
95 # Write the frame times (should we use the time IPO??)
96 f
.write(pack(">%df" % (numframes
), *[frame
/ fps
for frame
in range(numframes
)])) # seconds
99 check_vertcount(me
, numverts
)
100 me
.transform(mat_flip
@ obj
.matrix_world
)
101 f
.write(pack(">%df" % (numverts
* 3), *[axis
for v
in me
.vertices
for axis
in v
.co
]))
103 obj_eval
.to_mesh_clear()
105 for frame
in range(frame_start
, frame_end
+ 1): # in order to start at desired frame
106 scene
.frame_set(frame
)
107 depsgraph
= context
.evaluated_depsgraph_get()
108 obj_eval
= obj
.evaluated_get(depsgraph
)
109 me
= obj_eval
.to_mesh()
110 check_vertcount(me
, numverts
)
111 me
.transform(mat_flip
@ obj
.matrix_world
)
113 # Write the vertex data
114 f
.write(pack(">%df" % (numverts
* 3), *[axis
for v
in me
.vertices
for axis
in v
.co
]))
116 obj_eval
.to_mesh_clear()
120 print('MDD Exported: %r frames:%d\n' % (filepath
, numframes
- 1))
121 scene
.frame_set(orig_frame
)