PLY: add missing type: short
[blender-addons.git] / io_sequencer_edl / import_edl.py
blobb1a767c1a5348c5203df95b037c70cfca7666cc4
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 import bpy
22 from . import parse_edl
25 def id_animdata_action_ensure(id_data):
26 id_data.animation_data_create()
27 animation_data = id_data.animation_data
28 if animation_data.action is None:
29 animation_data.action = bpy.data.actions.new(name="Scene Action")
32 def scale_meta_speed(sequence_editor, strip_list, strip_movie, scale):
33 # Add an effect
34 dummy_frame = 0
35 strip_speed = sequence_editor.sequences.new_effect(
36 name="Speed",
37 type='SPEED',
38 seq1=strip_movie,
39 frame_start=dummy_frame,
40 channel=strip_movie.channel + 1)
41 strip_list.append(strip_speed)
43 # not working in 2.6x :|
44 strip_speed.use_frame_blend = True
45 # meta = sequence_editor.new([strip_movie, strip_speed], 199, strip_movie.channel)
47 # XXX-Meta Operator Mess
48 scene = sequence_editor.id_data
49 # we _know_ there are no others selected
50 for strip in strip_list:
51 strip.select = False
52 strip_movie.select = True
53 strip_speed.select = True
54 bpy.ops.sequencer.meta_make()
55 strip_meta = scene.sequence_editor.sequences[-1] # XXX, weak assumption
56 assert(strip_meta.type == 'META')
57 strip_list.append(strip_meta)
58 strip_movie.select = strip_speed.select = strip_meta.select = False
59 # XXX-Meta Operator Mess (END)
61 if scale >= 1.0:
62 strip_movie.frame_still_end = int(strip_movie.frame_duration * (scale - 1.0))
63 else:
64 strip_speed.multiply_speed = 1.0 / scale
65 strip_meta.frame_offset_end = strip_movie.frame_duration - int(strip_movie.frame_duration * scale)
67 strip_speed.update()
68 strip_meta.update()
69 return strip_meta
72 def apply_dissolve_fcurve(strip_movie, blendin):
73 scene = strip_movie.id_data
74 id_animdata_action_ensure(scene)
75 action = scene.animation_data.action
77 data_path = strip_movie.path_from_id("blend_alpha")
78 blend_alpha_fcurve = action.fcurves.new(data_path, index=0)
79 blend_alpha_fcurve.keyframe_points.insert(strip_movie.frame_final_start, 0.0)
80 blend_alpha_fcurve.keyframe_points.insert(strip_movie.frame_final_end, 1.0)
82 blend_alpha_fcurve.keyframe_points[0].interpolation = 'LINEAR'
83 blend_alpha_fcurve.keyframe_points[1].interpolation = 'LINEAR'
85 if strip_movie.type != 'SOUND':
86 strip_movie.blend_type = 'ALPHA_OVER'
89 def replace_ext(path, ext):
90 return path[:path.rfind(".") + 1] + ext
93 def load_edl(scene, filename, reel_files, reel_offsets, global_offset):
94 """
95 reel_files - key:reel <--> reel:filename
96 """
98 strip_list = []
100 import os
101 # For test file
102 # frame_offset = -769
104 fps = scene.render.fps
105 dummy_frame = 1
107 elist = parse_edl.EditList()
108 if not elist.parse(filename, fps):
109 return "Unable to parse %r" % filename
111 scene.sequence_editor_create()
112 sequence_editor = scene.sequence_editor
114 for strip in sequence_editor.sequences_all:
115 strip.select = False
117 # elist.clean()
119 track = 0
121 edits = elist.edits[:]
122 # edits.sort(key = lambda edit: int(edit.recIn))
124 prev_edit = None
125 for edit in edits:
126 print(edit)
127 if edit.reel.lower() in parse_edl.BLACK_ID:
128 frame_offset = 0
129 else:
130 frame_offset = reel_offsets[edit.reel]
132 src_start = int(edit.srcIn) + frame_offset
133 # UNUSED
134 # src_end = int(edit.srcOut) + frame_offset
135 # src_length = src_end - src_start
137 rec_start = int(edit.recIn) + 1
138 rec_end = int(edit.recOut) + 1
139 rec_length = rec_end - rec_start
141 # apply global offset
142 rec_start += global_offset
143 rec_end += global_offset
145 # print(src_length, rec_length, src_start)
147 if edit.m2 is not None:
148 scale = fps / edit.m2.fps
149 else:
150 scale = 1.0
152 unedited_start = rec_start - src_start
153 offset_start = src_start - int(src_start * scale) # works for scaling up AND down
155 if edit.transition_type == parse_edl.TRANSITION_CUT and (not elist.overlap_test(edit)):
156 track = 1
158 strip = None
159 final_strips = []
160 if edit.reel.lower() in parse_edl.BLACK_ID:
161 strip = sequence_editor.sequences.new_effect(
162 name="Color",
163 type='COLOR',
164 frame_start=rec_start,
165 frame_end=rec_start + max(1, rec_length),
166 channel=track + 1)
167 strip_list.append(strip)
168 final_strips.append(strip)
169 strip.color = 0.0, 0.0, 0.0
171 else:
172 path_full = reel_files[edit.reel]
173 path_dironly, path_fileonly = os.path.split(path_full)
175 if edit.edit_type & (parse_edl.EDIT_VIDEO | parse_edl.EDIT_VIDEO_AUDIO):
176 # and edit.transition_type == parse_edl.TRANSITION_CUT:
178 # try:
179 strip = sequence_editor.sequences.new_movie(
180 name=edit.reel,
181 filepath=path_full,
182 channel=track + 1,
183 frame_start=unedited_start + offset_start)
184 strip_list.append(strip)
185 # except:
186 # return "Invalid input for movie"
188 # Apply scaled rec in bounds
189 if scale != 1.0:
190 meta = scale_meta_speed(sequence_editor, strip_list, strip, scale)
191 final_strip = meta
192 else:
193 final_strip = strip
195 final_strip.update()
196 final_strip.frame_offset_start = rec_start - final_strip.frame_final_start
197 final_strip.frame_offset_end = rec_end - final_strip.frame_final_end
198 final_strip.update()
199 final_strip.frame_offset_end += (final_strip.frame_final_end - rec_end)
200 final_strip.update()
202 if edit.transition_duration:
203 if not prev_edit:
204 print("Error no previous strip")
205 else:
206 new_end = rec_start + int(edit.transition_duration)
207 for other in prev_edit.custom_data:
208 if other.type != 'SOUND':
209 other.frame_offset_end += (other.frame_final_end - new_end)
210 other.update()
212 # Apply disolve
213 if edit.transition_type == parse_edl.TRANSITION_DISSOLVE:
214 apply_dissolve_fcurve(final_strip, edit.transition_duration)
216 if edit.transition_type == parse_edl.TRANSITION_WIPE:
217 other_track = track + 2
218 for other in prev_edit.custom_data:
219 if other.type != 'SOUND':
220 strip_wipe = sequence_editor.sequences.new_effect(
221 name="Wipe",
222 type='WIPE',
223 seq1=final_strip,
224 frame_start=dummy_frame,
225 channel=other_track)
226 strip_list.append(strip_wipe)
228 from math import radians
229 if edit.wipe_type == parse_edl.WIPE_0:
230 strip_wipe.angle = radians(+90)
231 else:
232 strip_wipe.angle = radians(-90)
234 other_track += 1
236 # strip.frame_offset_end = strip.frame_duration - int(edit.srcOut)
237 # end_offset = (unedited_start + strip.frame_duration) - end
238 # print start, end, end_offset
239 # strip.frame_offset_end = end_offset
241 # break
242 # print(strip)
244 final_strips.append(final_strip)
246 if edit.edit_type & (parse_edl.EDIT_AUDIO | parse_edl.EDIT_AUDIO_STEREO | parse_edl.EDIT_VIDEO_AUDIO):
248 if scale == 1.0: # TODO - scaled audio
250 try:
251 strip = sequence_editor.sequences.new_sound(
252 name=edit.reel,
253 filepath=path_full,
254 channel=track + 6,
255 frame_start=unedited_start + offset_start)
256 strip_list.append(strip)
257 except:
259 # See if there is a wave file there
260 path_full_wav = replace_ext(path_full, "wav")
262 # try:
263 strip = sequence_editor.sequences.new_sound(
264 name=edit.reel,
265 filepath=path_full_wav,
266 channel=track + 6,
267 frame_start=unedited_start + offset_start)
268 strip_list.append(strip)
269 # except:
270 # return "Invalid input for audio"
272 final_strip = strip
274 # Copied from above
275 final_strip.update()
276 final_strip.frame_offset_start = rec_start - final_strip.frame_final_start
277 final_strip.frame_offset_end = rec_end - final_strip.frame_final_end
278 final_strip.update()
279 final_strip.frame_offset_end += (final_strip.frame_final_end - rec_end)
280 final_strip.update()
282 if edit.transition_type == parse_edl.TRANSITION_DISSOLVE:
283 apply_dissolve_fcurve(final_strip, edit.transition_duration)
285 final_strips.append(final_strip)
287 if final_strips:
288 for strip in final_strips:
289 # strip.frame_duration = length
290 strip.name = edit.as_name()
291 edit.custom_data[:] = final_strips
292 # track = not track
293 prev_edit = edit
294 track += 1
296 # break
298 for strip in strip_list:
299 strip.update(True)
300 strip.select = True
302 return ""
305 def _test():
306 elist = parse_edl.EditList()
307 _filename = "/fe/edl/cinesoft/rush/blender_edl.edl"
308 _fps = 25
309 if not elist.parse(_filename, _fps):
310 assert(0)
311 reels = elist.reels_as_dict()
313 print(list(reels.keys()))
315 # import pdb; pdb.set_trace()
316 msg = load_edl(bpy.context.scene,
317 _filename,
318 {'tapec': "/fe/edl/cinesoft/rush/rushes3.avi"},
319 {'tapec': 0}) # /tmp/test.edl
320 print(msg)
321 # _test()