Skip to main content
2 of 2
added 81 characters in body

I had the same issue, so I use try and except to fix this problem in python 3.3 in blender 2.69.

Solution

try:
    args = parser.parse_args(argv)
    do_something(args)
except SystemExit as e:
    print(repr(e))
# END OF FILE

Results

When we run blender like this:

blender --background --python argparse_blender.py -- -h

(Or like this blender --b --P argparse_blender.py -- -h)

We get the desired output:

usage: blender [-h]

Run blender in background mode with this script: blender --background --python argparse_blender.py -- [options]

optional arguments:                                                                                                                                
    -h, --help  show this help message and exit
SystemExit(0,)

found bundled python: [...]/blender/2.69/python

Blender quit

Suggestion

I would write the whole argparse_blender.py like this:

import do_something
import argparse
import sys

argv = sys.argv
if "--" not in argv:
    argv = []
else:
   argv = argv[argv.index("--") + 1:]

## set the prog name to match real usage
parser = argparse.ArgumentParser(
    description = 'Run blender in background mode',
    prog = "blender -b -P "+__file__+" --",
)
try:
    args = parser.parse_args(argv)
    do_something(args)
except SystemExit as e:
    print(repr(e))
# END OF FILE

Because it gives nicer output:

usage: blender -b -P argparse_blender.py -- [-h]

Run blender in background mode

optional arguments:                                                                                                                                
    -h, --help  show this help message and exit
SystemExit(0,)

found bundled python: [...]/blender/2.69/python

Blender quit