0

I'm trying to write a simple script that adds png images to the video seqeuence editor, and stitches them into a movie. However, between the blender API and all of the old documentation that's floating around online, I can't seem to get it to work. Here's my code:

import bpy
import os, glob

img_dir = 'my/img/dir'

set movie render settings

resx = 1024 resy = 350

bpy.data.scenes["Scene"].render.resolution_x = resx bpy.data.scenes["Scene"].render.resolution_y = resy bpy.data.scenes["Scene"].render.resolution_percentage = 100

scene = bpy.data.scenes[0] editor = scene.sequence_editor_create()

get image files

image_files = [] for _file in os.listdir(path=img_dir): if _file.endswith('.png'): image_files.append(_file)

context = bpy.context

add to sequencer

img_strip = [{'name':i} for i in image_files] frames = len(image_files) frame_end = frames - 1 # offset for python counting

a = bpy.ops.sequencer.image_strip_add(directory=img_dir, files=img_strip, channel=1, frame_start=0, frame_end=frame_end)

strip_name = file[0].get("name") bpy.data.scenes["Scene"].frame_end = frames bpy.data.scenes["Scene"].render.image_settings.file_format = 'AVI_JPEG' bpy.data.scenes["Scene"].render.filepath = out_dir bpy.ops.render.render( animation=True )

with output:

line 32, in <module>
    channel=1, frame_start=0, frame_end=frame_end)
  File "/usr/share/blender/scripts/modules/bpy/ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.sequencer.image_strip_add.poll() failed, context is incorrect

What does this error mean? And how might I go about modifying my code?

2 Answers2

3

Ditch the operator.

Have updated the code from

Importing multiple movie clips inside one directory via script

  • look at all images in a path
  • glob on "*.png"
  • to use pathlib.Path

Adding each image as a strip.

import bpy
from pathlib import Path

glob all movie files from

dir_path = "/home/batfinger/Pictures" img_dir = Path(dir_path) glob = "*.png" frame_duration = 24 files = sorted(list(img_dir.glob(glob)))

get sequence editor

scene = bpy.context.scene #scene.sequence_editor_clear() sed = scene.sequence_editor_create() seq = sed.sequences

add movie strips

for i, fp in enumerate(files): ms = seq.new_image( name=fp.name, filepath=str(fp), channel=i + 1, frame_start= i * frame_duration + 1 ,

        )
ms.frame_final_duration = frame_duration
# print some details
print(&quot;%s ch: %d fs: %3.1f len: %d&quot; %
        (fp.name, i + 1, ms.frame_start, ms.frame_final_duration)
        )

Adding images as sequence to one strip.

import bpy
from pathlib import Path

dir_path = "/home/batfinger/Pictures" img_dir = Path(dir_path) glob = "*.png" frame_duration = 24 files = sorted(list(img_dir.glob(glob)))

get sequence editor

scene = bpy.context.scene #scene.sequence_editor_clear() sed = scene.sequence_editor_create() seq = sed.sequences duration = len(files)

add image strip using first

fp = files.pop(0) imstrip = seq.new_image( name=fp.name, filepath=str(fp), frame_start=1, channel=1, )

add the elements

while files: imstrip.elements.append(files.pop(0).name)

imstrip.frame_final_duration = duration imstrip.update()

batFINGER
  • 84,216
  • 10
  • 108
  • 233
2

As batFINGER suggested, you get this error because you try run a function in a context that is not suitable. If you click on the run/start-button in the text editor, the context of the running code is the TEXT_EDITOR, not the SEQUENCE_EDITOR. Therefore, you would have to switch context.

This can be done as follows (see response of eceathar for the original solution):

area = bpy.context.area
old_type = area.type
area.type = 'SEQUENCE_EDITOR'

call the image_strip_add here and other functions

area.type = old_type

Further reading: poll() failed, context incorrect? - Example: bpy.ops.view3d.background_image_add()

brockmann
  • 12,613
  • 4
  • 50
  • 93
Husch
  • 593
  • 2
  • 7