Start blender in the background from the command line and execute blender processing with python. I want to have some arguments when running python.
Use python's argparse library to help organize the arguments.
Take an argument and execute it like run.bat.
run.bat
blender.exe --background --python generate.py --gender f --location i
, An error is output. '--' Is not in the argument list.
File "C:\hogehoge\generateData\generate.py", line 23, in
<module>
args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:])
ValueError: '--' is not in list
unknown argument, loading as file: --gender
Error: Cannot read file 'C:\hogehoge\generateData\--gender': No such file or directory
There seems to be a way to give an argument to python script with blender, and add'-' before the argument.
run.bat
blender.exe --background --python generate.py -- --gender f --location i
Then the arguments are kept in sys.argv. The python code from sys.argv to retrieve the arguments with argparse looks like this:
main()
import sys
import argparse
if '__main__' == __name__:
parser = argparse.ArgumentParser()
parser.add_argument('--gender', type=str, choices=['f', 'm'], required=True,
help='gender: f->female, m->male')
parser.add_argument('--location', type=str, choices=['i', 'o'], required=True,
help='location: i->inside, o->outside')
args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:])
gender = args.gender
location = args.location
Sys.argv [sys.argv.index ('-') + 1:], which uses argparse to retrieve arguments, feels magical.
Recommended Posts