Exit blender after running script; let script 'consume' command line parameters?

I’m sure this will have been written somewhere, but I can’t find it at the moment.

I have a script which works. I want to run blender from a command line, have it find this script, run it, then exit.

Originally I’d have liked blender to run in --background mode, but for some reason this causes Cycles to render me a nice grey box, so for the moment I’m running it as usual, with the GUI and everything, using this command:

blender.exe autoGen.blend --file xml.xml

So autoGen.blend is the blendfile with my script in it, and xml.xml is a file that the script uses. As is, this works adequately.

Couple of cosmetic problems: blender bitches about the “–file xml.xml” because it doesn’t know what it means. Is there a way I can tell it that everything’s fine?

Secondly, the program opens but doesn’t close. When the script finishes, blender’s gui appears and lets me edit autoGen.blend. This is a problem because tomorrow I’m handing the script over to someone else with no knowledge of blender. I don’t want them to keep having to close windows and I certainly don’t want them to be able to poke around inside the blend file without knowing what they’re doing.

What I have at the moment is ‘Auto run python scripts’ checked in the User Prefs, and ‘Register’ checked at the bottom of the script’s text window. I have no understanding as to why, but that makes the thing open and run. Where do I go from here?

I have no clue if this would actually work, but everything in the Blender UI is supposed to be accessible though the Python interface. Have you check “bpy.ops” to see if there is a command that ties to the “Quit” button. It seems like after your script has finished running you should be able to tell Blender to Quit.

As far as the command line stuff goes, I don’t have much experience there.

did you check the options from:
./blender -h

there is a whole list of possible usage.
./blender -h
Blender 2.61 (sub 0)
Usage: blender [args …] [file] [args …]

Render Options:
-b or --background <file>
Load <file> in background (often used for UI-less rendering)

-a or --render-anim
Render frames from start to end (inclusive)

-S or --scene <name>
Set the active scene <name> for rendering

… and so on.

blender --background autoGen.blend – file.xml

in your script:

import sys
# file.xml is in sys.argv[-1]
# do your job
# when finish, exit with:


blender -h

Ends option processing, following arguments passed unchanged. Access via python’s sys.argv

Nice, thanks everyone.

Okay, the script runs and quits fine, but I get this in the terminal:

Rendering completed.

Saved session recovery to C:\Users	cas\AppData\Local\Temp\quit.blend
Error: Not freed memory blocks: 174
bArgs len: 48 000000000389B748
bArgs passes len: 20 000000000382D578
bArgs passes gh len: 48 000000000389B7C8
memory pool len: 48 000000000389B848
BLI_Mempool Chunk len: 24 000000000382D5D8
BLI Mempool Chunk Data len: 1536 00000000038BC778
BLI_Mempool Chunk len: 24 000000000382D638
BLI Mempool Chunk Data len: 1536 00000000038BCDC8
bArgDoc len: 48 00000000038E8AF8
bArgument len: 32 0000000003871038
bAKey len: 24 000000000382D698
bArgDoc len: 48 00000000038E8B78
bArgument len: 32 00000000038ECAF8
bAKey len: 24 000000000382D6F8
bArgument len: 32 00000000038ECB68
...(goes on for a while)
bAKey len: 24 000000000382ED18
bArgument len: 32 00000000038EE508
bAKey len: 24 000000000382ED78
wmOperatorReportList len: 40 000000001C72E598

Blender quit

My code for that section looks like this:

    #job done, exit the program
    print ("

Rendering completed.


Anyone know what’s causing this?

Oh yeah: you get the same result with sys.exit() or quit_blender(), it makes no difference.

I’d really like to hide that output if possible, so it can’t confuse users of the script.

you may re-route the stderr and stdout output to a dummy file and delete this later or directly to /dev/null
and a short quit-python-file to only run a special blend-file could be this:

import bpy

saved as quit.py
and the start of blender in the blender-directory:

./blender -b myblend.blend -P quit.py 2&gt;1 1&gt;/dev/null

the python script could be something more usefull, maybe like doing a render of an image and save this …

EDIT: i personally think to hide error-messages, status-messages is a bad habit, because no one could see if something goes totally wrong … but some people like to be blinded …

test-dr: the main reason I want to hide it is because it’s eclipsing my own script’s output.

This means I can’t just pipe into a text file, because I want the piping to start halfway through (ie, only start hiding blender’s output when my own script has finished.)