# How to insert a keyframe in the VSE via script?

I have the following code in which I want to add a keyframe at two points at the end:

import bpy
import os, glob

# glob all movie files from
vid_dir = "/Volumes/Temp/Grau_Benjamin/01_avi_sequences_264frames_backw/0_glitched/"
# note this will recursively find movies in folder
exts = bpy.path.extensions_movie
files = [f for ext in exts for f in glob.glob(os.path.join(vid_dir, "*%s" % ext))]
files.sort()

scene = bpy.context.scene
scene.sequence_editor_create()
seq = scene.sequence_editor.sequences
for i, fp in enumerate(files):
frame_start = (i * 48) + (i * i) + 1# some offset
name = os.path.basename(fp)
seq.new_movie(name=name,
filepath=fp,
channel=32 - i,
frame_start=frame_start
)
scene.sequence_editor.sequences_all[name].blend_type = 'CROSS'
bpy.context.scene.frame_current = frame_start + ((i+1) * 48) + ((i+1) * (i+1)) + 1
scene.sequence_editor.sequences_all[name].blend_alpha = 100
# Here I want to set a keyframe for the blend parameter
bpy.context.scene.frame_current = frame_start + ((i+2) * 48) + ((i+2) * (i+2)) + 1
scene.sequence_editor.sequences_all[name].blend_alpha = 0
# Here I want to set a keyframe for the blend parameter


I tried using the automatic keyframe insertion, but this wouldn't work. And I don't see what's happening inside the console, so that I could paste it to my code, when doing it manually.

Thank you very much.

Benni

Keyframe insert

In a late edit to previous answer I wanted to point out that the sequencers new_movie(..) method returns the newly created strip. (In the docs from the link where it says "Returns") Assigned it to variable ms

The context scene alreasy is assigned to scene. Hence there is no need for bpy.context.scene.sequences_all[name]... Doing it this way will cause problems if there already a strip named "foo", the new one will be named "foo.001" and you will be looking at old one instead. This is one big reason to use API methods that return a reference to the object they've created.

There are oodles of questions and answers on bse re adding keyfraems using keyframe_insert(datapath, frame) which can be used like

# set the value
ob.foo_bar = 22
# insert the keyframe at frame f
ob.keyframe_insert("foo_bar", frame=f)


There is no need to change the scene.frame_current first.

Ammended code.

import bpy
import os, glob

# glob all movie files from
vid_dir = "/home/batfinger/Videos/"
# note this will recursively find movies in folder
exts = bpy.path.extensions_movie
files = [f for ext in exts for f in glob.glob(os.path.join(vid_dir, "*%s" % ext))]
files.sort()

scene = bpy.context.scene
scene.sequence_editor_create()
seq = scene.sequence_editor.sequences
for i, fp in enumerate(files):
frame_start = (i * 48) + (i * i) + 1# some offset
name = os.path.basename(fp)
ms = seq.new_movie(name=name,
filepath=fp,
channel=32 - i,
frame_start=frame_start
)
ms.blend_type = 'CROSS'
f = frame_start + ((i+1) * 48) + ((i+1) * (i+1)) + 1
ms.blend_alpha = 100
ms.keyframe_insert("blend_alpha", frame=f)

f = frame_start + ((i+2) * 48) + ((i+2) * (i+2)) + 1
ms.blend_alpha = 0
ms.keyframe_insert("blend_alpha", frame=f)


To see what's happening you need to print something (or throw an error)

print("this will be seen in system console (terminal)")