Sun Position: fix error in HDRI mode when no env tex is selected
[blender-addons.git] / io_shape_mdd / import_mdd.py
blob485fa8739d323b63672b2a1dea93b29376ca00c7
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # mdd importer by Bill L.Nieuwendorp
4 # conversion to blender 2.5: Ivo Grigull (loolarge)
6 # Warning if the vertex order or vertex count differs from the
7 # origonal model the mdd was Baked out from their will be Strange
8 # behavior
10 # vertex animation to ShapeKeys with ipo and gives the frame a value of 1.0
11 # A modifier to read mdd files would be Ideal but that's for another day :)
13 # Please send any fixes,updates,bugs to Slow67_at_Gmail.com
14 # Bill Niewuendorp
16 import bpy
17 from struct import unpack
19 def set_linear_interpolation(obj, shapekey):
20 anim_data = obj.data.shape_keys.animation_data
21 data_path = "key_blocks[\"" + shapekey.name + "\"].value"
23 for fcu in anim_data.action.fcurves:
24 if fcu.data_path == data_path:
25 for keyframe in fcu.keyframe_points:
26 keyframe.interpolation = 'LINEAR'
29 def obj_update_frame(file, scene, obj, start, fr, step):
31 # Insert new shape key
32 new_shapekey = obj.shape_key_add()
33 new_shapekey.name = ("frame_%.4d" % fr)
34 new_shapekey_index = len(obj.data.shape_keys.key_blocks) - 1
36 obj.active_shape_key_index = new_shapekey_index
37 obj.show_only_shape_key = True
39 verts = new_shapekey.data
41 for v in verts: # 12 is the size of 3 floats
42 v.co[:] = unpack('>3f', file.read(12))
44 # me.update()
45 obj.show_only_shape_key = False
47 # insert keyframes
48 new_shapekey = obj.data.shape_keys.key_blocks[new_shapekey_index]
49 frame = start + fr*step
51 new_shapekey.value = 0.0
52 new_shapekey.keyframe_insert("value", frame=frame - step)
54 new_shapekey.value = 1.0
55 new_shapekey.keyframe_insert("value", frame=frame)
57 new_shapekey.value = 0.0
58 new_shapekey.keyframe_insert("value", frame=frame + step)
60 set_linear_interpolation(obj, new_shapekey)
62 obj.data.update()
65 def load(context, filepath, frame_start=0, frame_step=1):
67 scene = context.scene
68 obj = context.object
70 print('\n\nimporting mdd %r' % filepath)
72 if bpy.ops.object.mode_set.poll():
73 bpy.ops.object.mode_set(mode='OBJECT')
75 file = open(filepath, 'rb')
76 frames, points = unpack(">2i", file.read(8))
77 time = unpack((">%df" % frames), file.read(frames * 4))
79 print('\tpoints:%d frames:%d' % (points, frames))
80 print('\tstart frame:%d step:%d' % (frame_start, frame_step))
82 # If target object doesn't have Basis shape key, create it.
83 if not obj.data.shape_keys:
84 basis = obj.shape_key_add()
85 basis.name = "Basis"
86 obj.data.update()
88 for i in range(frames):
89 obj_update_frame(file, scene, obj, frame_start, i, frame_step)
91 return {'FINISHED'}