0
$\begingroup$

In the code below I am adding an image sequence(multiple images) and then render it as a video.

Scenario 1 - script in Blender text editor: When I run this code in the text editor in Blender it works fine. The video is good. When I open the saved blend file I can see in the sequencer in the N-panel under "Source" I can see that it changes the image frame number when I scroll through the timeline.

Scenario 2 - script in commandline: When I use the same script via commandline - it will create the video but it only shows the first frame of the image sequence. When I check the sequencer "source" again in this case it just shows the same first frame and does not update when scrubbing through the timeline.

Why is this happening? Why does the commandline not properly update the frames when it renders the video? And how can I fix this?

import bpy
from pathlib import Path

dir_path = "D:/test/frames"
img_dir = Path(dir_path)
glob = "*.png"
frame_duration = 24
files = sorted(list(img_dir.glob(glob)))

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

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

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

imstrip.frame_final_duration = duration

original_file_path = 'D:/test/'
original_file_name = "test123.blend"

original_full_path = original_file_path + original_file_name

new_file_name = "new_test1234.blend"
new_full_path = original_file_path + new_file_name

bpy.ops.wm.save_as_mainfile(filepath=new_full_path)

render_output_path = "D:/test/output"
render_output_filename = "animation_output"
render_output_format = 'FFMPEG'  # Use FFMPEG for mp4 output

bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.context.scene.render.ffmpeg.format = 'MPEG4'

bpy.context.scene.render.resolution_x = 1920  # Set your desired resolution
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.fps = 30  # Set your desired frame rate
bpy.context.scene.render.filepath = render_output_path + render_output_filename

bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = duration

bpy.ops.render.render(animation=True)

bpy.ops.wm.save_as_mainfile(filepath=original_full_path)
$\endgroup$
2
  • $\begingroup$ It's not clear how you execute scenario 2. I tried your script in Text Editor and it works fine. There are 3 frames in the output. How exactly did you do scenario 2? $\endgroup$ Commented Aug 6, 2023 at 8:41
  • $\begingroup$ cmd.exe on windows Then navigate to blender location. Then use the code below - without the single quotation marks of course. . (The "Test_scene" blend file is just a basic blender file with nothing in it) 'blender --factory-startup -b "D:/test/test_scene.blend" -P "D:/test/test_script.py"' $\endgroup$ Commented Aug 6, 2023 at 11:18

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.