Add images with bpy.ops.sequencer.image_strip_add

I want to make a script to add all images from folder : C: mp into Video Sequence Editor
using:

bpy.ops.sequencer.image_strip_add

I search on forum and internet and the result is just this: https://developer.blender.org/T43736

How can I do that ?

Thank you. Best regards.

I try this source of code but I got errors:

import bpy
import os

dir="C:/tmp/"  #not include files
dirproxy="C:/tmp/" #including proxy file

file=[]
print ("aaa")
for i in os.listdir(dir):
    print(i)
    frame={"name":i}
    print(frame)
    file.append(frame)
    print(file)
    bpy.ops.sequencer.image_strip_add( \
        directory = dir, \
        files=i,\
        frame_start=0,\
        channel=0, \
        filemode=9)\

This is the output:

0001.png
{'name': '0001.png'}
[{'name': '0001.png'}]

Traceback (most recent call last):
  File "<blender_console>", line 12, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.79\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

Hello,

I don’t know if you solve your problem,
but I had the same problem and the answer was to change the area context


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 

(thanks to https://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add)

@eceathar Thank you for reply.

I used dir=os.path.abspath(“C:/tmp/”) is a good way to use paths.

Also I have the 2.79a version now.
I cannot read files in Blender python (but same source code works fine on python 3.6.x).
You can see the bug I open https://developer.blender.org/T54514
About your idea with context then that just change area window from python text area to SEQUENCE_EDITOR
The main problem to add images is the function: image_strip_add.poll()

Eceathar’s answer is right. image_strip_add.poll() is failing because the context is not what the operator expects. Like Sergey says in the bug you linked, you should use a “low-level” function instead of an operator. In this case: scene.sequence_editor.sequences.new_image()
Docs: https://docs.blender.org/api/blender_python_api_current/bpy.types.Sequences.html#bpy.types.Sequences.new_image

The file reading issue is a permission issue, probably Blender (or Blender’s python interpreter) doesn’t have the permissions to read files from c: mp. Can you read files from other folders?
If you want to use the system’s temp folder consider using the tempfile module

I need a good example versus API 2.79a on my issue.
I understand the context issue and the error info - is failing because the context , the result is not working.
I don’t make changes on OS to Blender 3D - is a default install, but
my python 3.6.x install (not Blender 3D python) is running good , see:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import os
>>> dir=os.path.abspath(“C:/tmp/”)
>>> for i in os.listdir(dir):
print(i)
…
0001.png
0002.png
0003.png

the bug has close and refer a function on API not : BlendDataImages(bpy_struct) - version 2.79
your reply use the version 2.78 scene.sequence_editor.sequences.new_image()

1 Like