holgk
holgk

Reputation: 11

c# process that running a batch file for blender exporting gets paused

I hope someone can help me with this problem.

Situation:

I need to export an animation from blender as a sequence it has to be tigger inside of an c# app. I'm using blenders obj exporter with a python script that's pretty much the same as the example in this blender wiki: https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Import-Export/Wavefront_OBJ

This is the python script i wrote:

import bpy
import sys

argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"

obj_out = argv[0]

bpy.ops.export_scene.obj(filepath=obj_out, axis_forward='-Z', axis_up='Y', use_animation=1, keep_vertex_order=1, use_normals=1, use_materials=0)

in addition to that i wrote the following batch file:

"C:\Program Files\Blender Foundation\Blender\blender.exe" "C:\Users\holgk\Documents\Some Folders\Test.blend" --background --python "C:\Program Files\Blender Foundation\Blender\convert_blend_to_obj.py" -- "C:\Users\holgk\Documents\Some Folders\Test.obj"

My c# code looks like this:

using System;
using System.Diagnostics;

namespace BlenderToObjs
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo processInfo;
            Process process;

            processInfo = new ProcessStartInfo("cmd.exe", "/c \"" + @"C:\blender_export_to_obj.bat" + "\"");
            processInfo.CreateNoWindow = false;
            processInfo.UseShellExecute = false;

            processInfo.RedirectStandardError = true;
            processInfo.RedirectStandardOutput = true;

            process = Process.Start(processInfo);
            process.WaitForExit();
        }
    }
}

When i'm using the window cmd shell everything works as it should. For every frame an obj file is created. But when i'm using my c# app the cmd line is open, blender starts to execute and for the first frames about 12 of 250 everything works as it should and then nothing happens and the app is still waiting for the exit but no new obj files are created.

Before i wrote the batch file i tried to run blender with that python script directly as a process and i had the same problem it started but was "pausing"/"stalling" after a few frames of exporting.

I'm guessing that it has to do with my c# process code (because running in the cmd shell does work) but i don't know what i'm doing wrong.

Upvotes: 1

Views: 669

Answers (1)

Madlaina Kalunder
Madlaina Kalunder

Reputation: 661

could it be, that your blender is freezing in the process or that the obj export fails?

it seems like you don't catch the error and blender never gets terminated so it just keeps going.

first, you should wrap your python code into a try, except block:

import bpy
import sys

def main():
    try:
        # Do stuff
        argv = sys.argv
        argv = argv[argv.index("--") + 1:] # get all args after "--"

        obj_out = argv[0]

        bpy.ops.export_scene.obj(filepath=obj_out, axis_forward='-Z', axis_up='Y', use_animation=1, keep_vertex_order=1, use_normals=1, use_materials=0)
        O.wm.quit_blender()

    except:
        # you could write your error file here

        # exit blender
        sys.exit(1)
        O.wm.quit_blender()
main()

the problem here is that you can't exit blender (yet, or I never figured it out) with an exit code that would indicate the success of the python script, because even with sys.exit(1) blender will terminate with a success message.

add something like this to write your error messages to a file that you later can read (or that your c# code can read)

        log.error('Failed to bake light map textures', exc_info=True)
        if 'ERROR_FILE' in os.environ:
            with open(os.environ['ERROR_FILE'], 'w') as file:
                file.write(traceback.format_exc())

let me know if that helped you any further in solving the mystery

Upvotes: 0

Related Questions