Skip to content

Conversation

@kroketio
Copy link
Contributor

@kroketio kroketio commented Jul 28, 2024

When running Blender direct import, it executes some Python but does not check if it succeeded.

static constexpr char PYTHON_SCRIPT_DIRECT[] = R"(
import bpy, sys
opts = %s
if bpy.app.version < (3, 0, 0):
print('Blender 3.0 or higher is required.', file=sys.stderr)
bpy.ops.wm.open_mainfile(filepath=opts['path'])
if opts['unpack_all']:
bpy.ops.file.unpack_all(method='USE_LOCAL')
bpy.ops.export_scene.gltf(**opts['gltf_options'])
)";

Which goes to start_blender(...) and exitcode is checked to determine failure.

Error EditorImportBlendRunner::start_blender(const String &p_python_script, bool p_blocking) {
String blender_path = EDITOR_GET("filesystem/import/blender/blender_path");
List<String> args;
args.push_back("--background");
args.push_back("--python-expr");
args.push_back(p_python_script);
Error err;
if (p_blocking) {
int exitcode = 0;
err = OS::get_singleton()->execute(blender_path, args, nullptr, &exitcode);
if (exitcode != 0) {
return FAILED;
}

However, Blender does not return exitcode 1 when the Python script fails: blender/issues/82494

There is --python-exit-code which returns the exitcode in the given value whenever the script fails:

$ blender --help
--python-exit-code <code>
        Set the exit-code in [0..255] to exit if a Python exception is raised
        (only for scripts executed from the command line), zero disables.

This PR adds the flag, as well as print the stderr of the Blender process for more clarity.

Copy link
Member

@fire fire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok.

@AThousandShips AThousandShips added this to the 4.4 milestone Jul 30, 2024
Copy link
Member

@aaronfranke aaronfranke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. I did not test it.

@kroketio
Copy link
Contributor Author

I did test it, should work for both Blender 3 and 4 major releases.

@kroketio kroketio requested a review from a team as a code owner October 11, 2024 17:07
@Repiteo Repiteo modified the milestones: 4.4, 4.5 Feb 24, 2025
@Repiteo Repiteo modified the milestones: 4.5, 4.6 Sep 8, 2025
@Repiteo Repiteo merged commit dfc38d2 into godotengine:master Nov 19, 2025
@Repiteo
Copy link
Contributor

Repiteo commented Nov 19, 2025

Thanks!

@akien-mga akien-mga changed the title blender/import: correct exit code on Python exception Blender import: Correct exit code on Python exception Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants