Prevent Editor from exiting when running from command line with -ExecutePythonScript

I have a render task that I want to automate with Python and running from command line.

The idea is to run an -ExecutePythonScript that will start the render, wait for the render to finish and then exit the engine.

.\UnrealEditor-Cmd.exe Project.uproject  -ExecutePythonScript="runrender.py" -Log -StdOut -allowStdOutLogVerbosity

BUT
As far as I understand as soon as the script supplied with ExecutePythonScript is finished, the editor receives an exit command, while the MoviePipelinePIEExecutor is still doing its job. Blocking the script with let’s say endless loop blocks the engine and it just hangs.

Is there a way to prevent editor from exiting on script finish? Or prevent engine from blocking when the script is blocking?

Here is a simplified script I would like to run (it runs smoothly from the running editor):

import unreal


def start_movie_pipeline_queue(queue_path):
    unreal.log(f"STARTED")

    movie_pipeline_queue_asset = unreal.load_asset(queue_path, unreal.MoviePipelineQueue)
    if not movie_pipeline_queue_asset:
        unreal.log_error(f"Failed to load Movie Pipeline Queue at {queue_path}")
        return
    movie_pipeline_queue_subsystem = unreal.get_editor_subsystem(unreal.MoviePipelineQueueSubsystem)
    movie_pipeline_queue_subsystem.load_queue(movie_pipeline_queue_asset, prompt_on_replacing_dirty_queue=False)
    executor = unreal.MoviePipelinePIEExecutor()
    movie_pipeline_queue_subsystem.render_queue_with_executor_instance(executor)  # or use MoviePipelineExecutorBase for headless mode if needed
    
    if executor:
        pp_finished_inst = unreal.OnMoviePipelineExecutorFinished()
        def pp_finished_handler(one, two):
            unreal.log(f"FINISHED YOYO")
        pp_finished_inst.add_callable(pp_finished_handler)
        executor.set_editor_property('on_executor_finished_delegate',pp_finished_inst)
        unreal.log(f"PROP SET")
    else:
        unreal.log_error(f"Failed to load Executor")

start_movie_pipeline_queue("/Game/Cinematics/RenderSettings/Only_MasterSQ_queue")

Thanks!

I suggest that you give a try to the remote python feature.

The principle is that you start the UnrealEditor.exe C:\xxx\xxx.uproject,
Then you wait for the editor to be responsive,
Then you send the remote python script you want the editor to execute,
Then when you know the process is completed (all frames are rendered), you simply use the windows taskkill to kill the editor.

We have build a huge pipeline around that strategy, so we know that this requires a lot of development, but at the end it works very well. In our case we even use a render farm of windows machines to generate our renders.

Two solutions for you:

  1. Prevent Editor to exit on Python script compete in UE5.5:
    unreal.EditorPythonScripting — Unreal Python 5.5 (Experimental) documentation
    unreal.EditorPythonScripting.set_keep_python_script_alive(True)
    Then subscribe Python function for Tick() and you may check for you job complete:
    unreal — Unreal Python 5.5 (Experimental) documentation
    unreal.register_slate_pre_tick_callback(callback_editor_tick)

  2. For Movie Render Pipeline use embedded cmd parameters:
    Command Line Rendering With Unreal Engine Movie Render Queue | Community tutorial
    Example:
    "C:\Program Files\Epic Games\UE_4.26\Engine\Binaries\Win64\UE4Editor.exe" "D:\MyProject\MyProject.uproject" "/Game/Levels/MyLevel" -MovieSceneCaptureType="/Script/MovieSceneCapture.AutomatedLevelSequenceCapture" -LevelSequence="/Game/Sequencer/MySequencer" -MovieFolder="D:\Output" -Game -RenderOffScreen -Unattended -NoLoadingScreen -NoScreenMessages -NoTextureStreaming -ResX=1920 -ResY=1080 -ForceRes -MovieFrameRate=25 -MovieFormat=PNG -MovieQuality=100 -MovieName="{world}.{frame}" -MovieCinematicMode=Yes -MovieWarmUpFrames=1 -MovieDelayBeforeWarmUp=0 -MovieStartFrame=0 -MovieEndFrame=100