# Python script for adding images always puts in the first file in the folder. How do I fix this?

I have some code (with elements from here) that should work but gives me a weird error. I import images in strips inside a while loop. The images imported are defined via the frame_range_ parameters. The frame_range_forw_start and the frame_range_back_start work, as the sequence is progressing but they also import the first file in the folder, which is not intended.

 import os

from bpy import context
scene = context.scene

files = os.listdir(path)
files.sort()

amp = 20 #Min 1
end = 250
i = 0

# create the sequencer data

while i <= end - amp + 1:

starta = amp * i * 2 # start of forward motion
startb = (amp * i * 2) + amp # start of backward motion

frame_range_forw_start = (1 + i * 2) # first imported image of forward motion
frame_range_forw_end = (1 + i * 2 + amp) # last imported image of forward motion
frame_range_back_start = (2 + i * 2) # first imported image of backward motion (strip will be reversed)
frame_range_back_end = (2 + i * 2 + amp) # last imported image of backward motion (strip will be reversed)

scene.sequence_editor_create()

seq = scene.sequence_editor.sequences.new_image(
name="MyStrip",
filepath=os.path.join(path, files[0]),
channel=1, frame_start=starta)

# add the rest of the images.
for f in files[frame_range_forw_start:frame_range_forw_end]:
seq.elements.append(f)

seq = scene.sequence_editor.sequences.new_image(
name="MyStrip",
filepath=os.path.join(path, files[0]),
channel=2, frame_start=startb)

# add the rest of the images.
for f in files[frame_range_back_start:frame_range_back_end]:
seq.elements.append(f)

# reverse if you want
seq.use_reverse_frames = True
#bpy.ops.sequencer.gap_remove(all=False)
i += 1


The first imported image in every strip (from frame_range_forw_start AND frame_range_back_start) is ALWAYS the first file in the folder. The frames after are correct.

this is the right frame this is the first image in the folder

What have I done wrong?

I also tried it with other equations:

frame_range_forw_start = (i)
frame_range_forw_end = (i + amp)
frame_range_back_start = (2 + i)
frame_range_back_end = (2 + i + amp)


But the problem persists.

• On a quick glance would move scene.sequence_editor_create() outside the while loop, it only needs to be called once (if scene.sequence_editor is None) – batFINGER May 25 at 7:50
• Thank you. I have done it. Didn't fix the issue though. – Benni May 25 at 7:53
• Is filepath=os.path.join(path, files[0]), the first file in folder? – batFINGER May 25 at 7:57
• Thank you so much. I replaced filepath=os.path.join(path, files[0]) with filepath=os.path.join(path, files[i]) and filepath=os.path.join(path, files[2+i]) And now it works. :) – Benni May 25 at 8:22
• Excellent. Write up explanation as answer. (backticks (above tab) marks up code in comments) – batFINGER May 25 at 8:36