smooth all objects (so we see their normals)
[blender-addons.git] / io_shape_mdd / import_mdd.py
blob8caedccde618c1d99de4ee3b2a80bc4dd211ad5b
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 #####
19 # <pep8 compliant>
21 # mdd importer by Bill L.Nieuwendorp
22 # conversion to blender 2.5: Ivo Grigull (loolarge)
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
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 thats for another day :)
31 # Please send any fixes,updates,bugs to Slow67_at_Gmail.com
32 # Bill Niewuendorp
34 import bpy
35 from struct import unpack
37 def set_linear_interpolation(obj, shapekey):
38 anim_data = obj.data.shape_keys.animation_data
39 data_path = "key_blocks[\"" + shapekey.name + "\"].value"
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'
47 def obj_update_frame(file, scene, obj, start, fr, step):
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
54 obj.active_shape_key_index = new_shapekey_index
55 obj.show_only_shape_key = True
57 verts = new_shapekey.data
59 for v in verts: # 12 is the size of 3 floats
60 v.co[:] = unpack('>3f', file.read(12))
62 # me.update()
63 obj.show_only_shape_key = False
65 # insert keyframes
66 new_shapekey = obj.data.shape_keys.key_blocks[new_shapekey_index]
67 frame = start + fr*step
69 new_shapekey.value = 0.0
70 new_shapekey.keyframe_insert("value", frame=frame - step)
72 new_shapekey.value = 1.0
73 new_shapekey.keyframe_insert("value", frame=frame)
75 new_shapekey.value = 0.0
76 new_shapekey.keyframe_insert("value", frame=frame + step)
78 set_linear_interpolation(obj, new_shapekey)
80 obj.data.update()
83 def load(operator, context, filepath, frame_start=0, frame_step=1):
85 scene = context.scene
86 obj = context.object
88 print('\n\nimporting mdd %r' % filepath)
90 if bpy.ops.object.mode_set.poll():
91 bpy.ops.object.mode_set(mode='OBJECT')
93 file = open(filepath, 'rb')
94 frames, points = unpack(">2i", file.read(8))
95 time = unpack((">%df" % frames), file.read(frames * 4))
97 print('\tpoints:%d frames:%d' % (points, frames))
98 print('\tstart frame:%d step:%d' % (frame_start, frame_step))
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()
106 for i in range(frames):
107 obj_update_frame(file, scene, obj, frame_start, i, frame_step)
109 return {'FINISHED'}