I trying to write a blender (2.74) python script (not plugin) for the Video Sequence Editor (VSE) which adds cross fades (audio and video) on adjacent sequences (located on different channels).
Here is the current script:
# this blender python script adds cross transition effect strips on adjacent strips which are on different channels
import bpy
# number of frames for the crossing
CROSS_FRAMES = 20
# find the right context
screen = bpy.context.window.screen
for area in screen.areas:
if area.type == 'SEQUENCE_EDITOR':
for region in area.regions:
if region.type == 'WINDOW':
override = {'window': bpy.context.window, 'screen': screen, 'area': area, 'region': region, 'scene': bpy.context.scene}
# for two following sequences in the current scene
for sequence_first in bpy.context.scene.sequence_editor.sequences_all:
for sequence_second in bpy.context.scene.sequence_editor.sequences_all:
if sequence_first.frame_final_end==sequence_second.frame_final_start and sequence_first.channel!=sequence_second.channel:
# visual sequences
if ((sequence_first.type=='MOVIE' or sequence_first.type=='IMAGE') and (sequence_second.type=='MOVIE' or sequence_second.type=='IMAGE')) or (sequence_first.type=='SOUND' and sequence_second.type=='SOUND'):
print("crossing "+sequence_first.name+" with "+sequence_second.name)
# add cross margin to first sequence
if sequence_first.type=='IMAGE':
sequence_first.frame_final_duration += CROSS_FRAMES/2
else: # MOVIE or SOUND
if sequence_first.frame_offset_end<CROSS_FRAMES/2:
sequence_first.frame_offset_end = 0
else:
sequence_first.frame_offset_end -= CROSS_FRAMES/2
# add cross margin to second sequence
if sequence_second.type=='IMAGE':
sequence_second.frame_start -= CROSS_FRAMES/2
sequence_second.frame_final_duration += CROSS_FRAMES/2
else: # MOVIE or SOUND
if sequence_second.frame_offset_start<CROSS_FRAMES/2:
sequence_second.frame_offset_start = 0
else:
sequence_second.frame_offset_start -= CROSS_FRAMES/2
# select sequences
bpy.ops.sequencer.select_all(action='DESELECT')
sequence_first.select = True
bpy.context.scene.sequence_editor.active_strip = sequence_first
sequence_second.select = True
bpy.context.scene.sequence_editor.active_strip = sequence_second
#bpy.ops.sequencer.select()
# add cross fade
if sequence_first.type=='SOUND' and sequence_second.type=='SOUND':
bpy.ops.sequencer.crossfade_sounds()
print("audio")
else: # MOVIE/IMAGE
bpy.ops.sequencer.effect_strip_add(override, type='CROSS', frame_start=sequence_second.frame_final_start, frame_end=sequence_first.frame_final_end)
break
bpy.ops.sequencer.select_all(action='DESELECT')
I’m running the script using
blender montage.blend --python BlenderAddCross.py
Here some aspects I don’t really understand:
- I have to use override (from the beginning of the script) in effect_strip_add else the context is wrong and I can’t add the effect strip
- I have to set bpy.context.scene.sequence_editor.active_strip = sequence_second else crossfade_sounds() complains the context is wrong because no sound is selected (even it is selected just before)
- I have to set bpy.context.scene.sequence_editor.active_strip = sequence_second else effect_strip_add() will often add the effect a unexpected locations (not the the sequences overlap)
- bpy.ops.sequencer.select() complains I’m in the wrong context, but I couldn’t find if the python source code which one it expects.
And the most important issue right now it that crossfade_sounds() will often increase/decrease the volume over the whole sequence, instead of the overlapping pieces.
My guess is that there is something wrong with the selection, but I didn’t manage to use select() because of a context issue I couldn’t understand.
Could someone explain this strange behaviour where the effect is not applied on the overlapping piece, or point to a context/select tutorial?
edit 2015-05-16:
I found a solution by myself for this problem.
See the third post on this thread for the description (once the moderators approved it)