AnimAll: rename the "Animate" tab back to "Animation"
[blender-addons.git] / io_shape_mdd / import_mdd.py
blob48aa98d1651d6c68e401701dd2c823ea29ae2987
1 # SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # mdd importer by Bill L.Nieuwendorp
6 # conversion to blender 2.5: Ivo Grigull (loolarge)
8 # Warning if the vertex order or vertex count differs from the
9 # origonal model the mdd was Baked out from their will be Strange
10 # behavior
12 # vertex animation to ShapeKeys with ipo and gives the frame a value of 1.0
13 # A modifier to read mdd files would be Ideal but that's for another day :)
15 # Please send any fixes,updates,bugs to Slow67_at_Gmail.com
16 # Bill Niewuendorp
18 import bpy
19 from struct import unpack
21 def set_linear_interpolation(obj, shapekey):
22 anim_data = obj.data.shape_keys.animation_data
23 data_path = "key_blocks[\"" + shapekey.name + "\"].value"
25 for fcu in anim_data.action.fcurves:
26 if fcu.data_path == data_path:
27 for keyframe in fcu.keyframe_points:
28 keyframe.interpolation = 'LINEAR'
31 def obj_update_frame(file, scene, obj, start, fr, step):
33 # Insert new shape key
34 new_shapekey = obj.shape_key_add()
35 new_shapekey.name = ("frame_%.4d" % fr)
36 new_shapekey_index = len(obj.data.shape_keys.key_blocks) - 1
38 obj.active_shape_key_index = new_shapekey_index
39 obj.show_only_shape_key = True
41 verts = new_shapekey.data
43 for v in verts: # 12 is the size of 3 floats
44 v.co[:] = unpack('>3f', file.read(12))
46 # me.update()
47 obj.show_only_shape_key = False
49 # insert keyframes
50 new_shapekey = obj.data.shape_keys.key_blocks[new_shapekey_index]
51 frame = start + fr*step
53 new_shapekey.value = 0.0
54 new_shapekey.keyframe_insert("value", frame=frame - step)
56 new_shapekey.value = 1.0
57 new_shapekey.keyframe_insert("value", frame=frame)
59 new_shapekey.value = 0.0
60 new_shapekey.keyframe_insert("value", frame=frame + step)
62 set_linear_interpolation(obj, new_shapekey)
64 obj.data.update()
67 def load(context, filepath, frame_start=0, frame_step=1):
69 scene = context.scene
70 obj = context.object
72 print('\n\nimporting mdd %r' % filepath)
74 if bpy.ops.object.mode_set.poll():
75 bpy.ops.object.mode_set(mode='OBJECT')
77 file = open(filepath, 'rb')
78 frames, points = unpack(">2i", file.read(8))
79 time = unpack((">%df" % frames), file.read(frames * 4))
81 print('\tpoints:%d frames:%d' % (points, frames))
82 print('\tstart frame:%d step:%d' % (frame_start, frame_step))
84 # If target object doesn't have Basis shape key, create it.
85 if not obj.data.shape_keys:
86 basis = obj.shape_key_add()
87 basis.name = "Basis"
88 obj.data.update()
90 for i in range(frames):
91 obj_update_frame(file, scene, obj, frame_start, i, frame_step)
93 return {'FINISHED'}