2
$\begingroup$

I'm trying to add image sequence to sequencer using python, but it looks like sequence_editor.sequences.new_image() can only add singular images and not image sequences.

Is there any way to add image sequence to sequencer without using bpy.ops.sequencer.image_strip_add()? I'm in a situation where I can't quite use it.

$\endgroup$

1 Answer 1

2
$\begingroup$

The bpy.ops.sequencer.image_strip_add() operator can add multiple images as a single strip. Here's a simple example that takes directory C:\Users\xxxx\Desktop\test\ containing 5 images of the format image_####.png and imports them as a sequence. You need to use the proper context, which is SEQUENCE_EDITOR, in order to use the bpy.ops.sequencer.image_strip_add command:

import bpy

directory = r"C:\Users\xxxx\Desktop\test"
prefix = "image_" #prefix in image_0001.png

frame_start = 1
frame_end = 5

files = []
for frame in range(frame_start, frame_end + 1):
    files.append({"name":f"{prefix}{frame:04d}.png"})

area_type = 'SEQUENCE_EDITOR'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

with bpy.context.temp_override(
    window=bpy.context.window,
    area=areas[0],
    regions=[region for region in areas[0].regions if region.type == 'WINDOW'][0],
    screen=bpy.context.window.screen
):
    bpy.ops.sequencer.image_strip_add(
        directory=directory,
        files=files,
        relative_path=True,
        show_multiview=False,
        frame_start=frame_start,
        frame_end=frame_end,
        channel=1,
        fit_method='FIT'
    )

On the other hand, the new_image() method is designed to create a new image sequence strip for a single image file. It does not have the ability to automatically combine multiple images into a single strip so you will end up with 5 image sequences:

import bpy
import os

directory = r"C:\Users\xxxx\Desktop\test"
prefix = "image_" #prefix in image_0001.png

frame_start = 1
frame_end = 5

for frame in range(frame_start, frame_end + 1):
    filename = f"{prefix}{str(frame).zfill(4)}.png"
    seq = bpy.data.scenes[0].sequence_editor.sequences.new_image(
        name = filename,
        filepath = os.path.join(directory, filename),
        channel = 1,
        frame_start = frame,
        fit_method = 'ORIGINAL'
    )
$\endgroup$
3
  • $\begingroup$ Thank you so much for the code and explanation Harry! Really appreciate your help. I do run into an issue though. Your code, and context overwrite works well when there is a visible area that is the actual Sequencer. What should we do in the case when there is no Sequencer visible at all during the code execution? $\endgroup$ Commented Feb 24, 2023 at 19:53
  • 1
    $\begingroup$ @MaciejGliwa Hi I'm glad it worked out for you! Either the sequencer needs to be open or you can automate it being opened by adding the line bpy.context.area.ui_type = 'SEQUENCE_EDITOR' at the beginning of the script. No other way to do it :) $\endgroup$
    – Harry McKenzie
    Commented Feb 25, 2023 at 2:19
  • $\begingroup$ I have the image_strip_add example working, however my images are not orientated correctly, some being rotated 90 or 180 degrees. I've tried different values for fit_method, but this seems to be ignored. Any ideas? (I've also looked at the Exif orientation data of the images but this doesn't seem to have any bearing on how they end up in the strip) $\endgroup$
    – macduff
    Commented Mar 9, 2023 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .