1# ##### BEGIN GPL LICENSE BLOCK #####
2#
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.
7#
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.
12#
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.
16#
17# ##### END GPL LICENSE BLOCK #####
18
19# <pep8 compliant>
20
21# mdd importer by Bill L.Nieuwendorp
22# conversion to blender 2.5: Ivo Grigull (loolarge)
23#
24# Warning if the vertex order or vertex count differs from the
25# origonal model the mdd was Baked out from their will be Strange
26# behavior
27#
28# vertex animation to ShapeKeys with ipo  and gives the frame a value of 1.0
29# A modifier to read mdd files would be Ideal but that's for another day :)
30#
31# Please send any fixes,updates,bugs to Slow67_at_Gmail.com
32# Bill Niewuendorp
33
34import bpy
35from struct import unpack
36
37def set_linear_interpolation(obj, shapekey):
38    anim_data = obj.data.shape_keys.animation_data
39    data_path = "key_blocks[\"" + shapekey.name + "\"].value"
40
41    for fcu in anim_data.action.fcurves:
42        if fcu.data_path == data_path:
43            for keyframe in fcu.keyframe_points:
44                keyframe.interpolation = 'LINEAR'
45
46
47def obj_update_frame(file, scene, obj, start, fr, step):
48
49    # Insert new shape key
50    new_shapekey = obj.shape_key_add()
51    new_shapekey.name = ("frame_%.4d" % fr)
52    new_shapekey_index = len(obj.data.shape_keys.key_blocks) - 1
53
54    obj.active_shape_key_index = new_shapekey_index
55    obj.show_only_shape_key = True
56
57    verts = new_shapekey.data
58
59    for v in verts:  # 12 is the size of 3 floats
60        v.co[:] = unpack('>3f', file.read(12))
61
62    # me.update()
63    obj.show_only_shape_key = False
64
65    # insert keyframes
66    new_shapekey = obj.data.shape_keys.key_blocks[new_shapekey_index]
67    frame = start + fr*step
68
69    new_shapekey.value = 0.0
70    new_shapekey.keyframe_insert("value", frame=frame - step)
71
72    new_shapekey.value = 1.0
73    new_shapekey.keyframe_insert("value", frame=frame)
74
75    new_shapekey.value = 0.0
76    new_shapekey.keyframe_insert("value", frame=frame + step)
77
78    set_linear_interpolation(obj, new_shapekey)
79
80    obj.data.update()
81
82
83def load(context, filepath, frame_start=0, frame_step=1):
84
85    scene = context.scene
86    obj = context.object
87
88    print('\n\nimporting mdd %r' % filepath)
89
90    if bpy.ops.object.mode_set.poll():
91        bpy.ops.object.mode_set(mode='OBJECT')
92
93    file = open(filepath, 'rb')
94    frames, points = unpack(">2i", file.read(8))
95    time = unpack((">%df" % frames), file.read(frames * 4))
96
97    print('\tpoints:%d frames:%d' % (points, frames))
98    print('\tstart frame:%d step:%d' % (frame_start, frame_step))
99
100    # If target object doesn't have Basis shape key, create it.
101    if not obj.data.shape_keys:
102        basis = obj.shape_key_add()
103        basis.name = "Basis"
104        obj.data.update()
105
106    for i in range(frames):
107        obj_update_frame(file, scene, obj, frame_start, i, frame_step)
108
109    return {'FINISHED'}
110