AnimAll: rename the "Animate" tab back to "Animation"
[blender-addons.git] / io_shape_mdd / export_mdd.py
blob9db4d6c97282e1afc3c91aac4b439d7f67b3002a
1 # SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Contributors: Bill L.Nieuwendorp
7 """
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
14 """
16 import bpy
17 import mathutils
18 from struct import pack
21 def zero_file(filepath):
22 """
23 If a file fails, this replaces it with 1 char, better not remove it?
24 """
25 file = open(filepath, 'w')
26 file.write('\n') # apparently macosx needs some data in a blank file?
27 file.close()
30 def check_vertcount(mesh, vertcount):
31 """
32 check and make sure the vertcount is consistent throughout the frame range
33 """
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):
39 """
40 Blender.Window.WaitCursor(1)
42 mesh_orig = Mesh.New()
43 mesh_orig.getFromObject(obj.name)
44 """
46 scene = context.scene
47 obj = context.object
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()
58 #Flip y and z
59 '''
60 mat_flip = mathutils.Matrix(((1.0, 0.0, 0.0, 0.0),
61 (0.0, 0.0, 1.0, 0.0),
62 (0.0, 1.0, 0.0, 0.0),
63 (0.0, 0.0, 0.0, 1.0),
65 '''
66 mat_flip = mathutils.Matrix()
68 numverts = len(me.vertices)
70 numframes = frame_end - frame_start + 1
71 if use_rest_frame:
72 numframes += 1
74 f = open(filepath, 'wb') # no Errors yet:Safe to create file
76 # Write the header
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
82 if use_rest_frame:
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()
102 f.close()
104 print('MDD Exported: %r frames:%d\n' % (filepath, numframes - 1))
105 scene.frame_set(orig_frame)
107 return {'FINISHED'}