bpy.ops.sequencer.scene_strip_add() throw "context is incorrect"


I would like to add current scene to video editor with python.
This procedure is working from menu (video editing / add / scene / Scene ) but if I’m in Scripting screen and in command line typeing this


then I get an error message:

… context is incorrect.

What can I do?

The problem is the context, obviously.

I had the same problem, until I dove deep into Blender’s C code and discovered the secret to scripting the sequence editor.

The sequence editor operators require a sequence editor area to be invoked. Part of that reason is that some of these
operators are concurrent tasks/threads which require user input to work their magic.

Now, when you call your script from the text editor or the console window, obviously the context’s area isn’t a sequence editor. But you can override that context. You just need to have an area handy. As far as I have checked, it shouldn’t matter which area.

Try this:

def find_sequencer_area():
    screens = [bpy.context.screen]+list(bpy.data.screens)
        for screen in screens:
            for area in screen.areas:
                if area.type == 'SEQUENCE_EDITOR':
                    return area

    # If that still doesn't work, I don't know what will
    return area
bpy.ops.sequence_editor.scene_strip_add({'area': area}, ...)

Operators are really a strange concept, especially if you’re more familiar with Python than with C or Blender. They can be just simple procedure calls. They can be like concurrent threads/tasks, because multiple tasks can go on simultaneously, but everything is still running in the same operating system thread.

They never return anything, which is particularly annoying in the sequence editor. You want to do anything to the strip you just added? The only way is to use “strip = bpy.context.scene.sequence_editor.sequences[-1]” after the operator call, and pray that this undocumented assumption is never violated.

Another annoying thing about the operator API is that they take an awful amount of parameters, often completely changing what the operator will do. For example “sequencer.effect_strip_add” can do a cross fade between two strips, modify one strip with an effect layer, or open a file dialog, all dependend on the arguments and the context. And despite having a ton of keyword parameters (which probably should be split into multiple operators), that’s still not enough to run the operator, you often need to select strips to “pass” them to the operator through its context. Which is sort of implicit and undocumented.

You always have to be on the lookout for operators trying to do user interaction. For scripting authors, that is almost never what you want, but can happen “by accident”.