简体   繁体   中英

Passing arguments to a python blender script, through the brenda-web interface, how do I parse the arguments?

It seems like an easy task, but there is a requirement that "--" is added to the command, to tell the program to pass the arguments to the script (and not process them). For example:

  $ blender -b testscene.blend --python localrender.py -- -start 1 -type diffuse -samples 100

I found a way to separate out the script arguments by using this:

try:
    args = list(reversed(sys.argv))
    idx = args.index("--")
except ValueError:
    params = []
else:
    params = args[:idx][::-1]
    print("Script params:", params)

Which breaks them all up individually. Which is not what I think needs to happen. I also have tried argparse, but I think that the "--" is breaking it. Does anybody know a complex way to parse this stuff out? I'd like to keep the arg + value together, so that I can do something like this:

args = parser.parse_args()
if args.index:
    logging.info("index set to: " + str(args.index))

But I am stuck with "blender: error: unrecognized arguments:"

EDIT----------------------------- EDIT------------------------

I am currently using this to grab the args after "--"
try:
    args = list(reversed(sys.argv))
    idx = args.index("--")
except ValueError:
    params = []
else:
    params = args[:idx][::-1]
    #print("Script params:", params)

logging.info(params)

which creates this structure: ['-items', '1', '-type', 'AO', '-samples', '100', '-size', '1024']

What should I do? I think this is going in the wrong direction, seeing as I want to group by Option:Argument. I tried this code also, but it split the actual letters in the arguments into pieces:

try:
    #getopt.getopt(sys.argv[1:], 'x:y:')
    #opts,val = getopt.getopt(params,"hi:o:",["objindex="])
    opts, args = getopt.getopt(params, "h:o")
except getopt.GetoptError:
    logging.info('prototype.py --items <items>')
    sys.exit(2)

logging.info("params:")
logging.info(params)
logging.info("opts")
logging.info(opts)

exit()

for opt, val in opts:
 # print("option" + opt)
  if opt in ("--items", "-items"):
    objindex = val
  elif opt in ("--type", "-type"):
    logging.info("found render type: " + val)

You can make your own parser, by using iterators. This code will seperate your arguments into two dicts (before and after the -- ):

args = '-b testscene.blend --python localrender.py -- -start 1 -type diffuse -samples 100'
iter = (i for i in args.split(' '))

args_dict = {'part1': {}, 'part2': {}}
current_part = 'part1'
for chunk in iter:
    if chunk == '--':
        current_part = 'part2'
        continue
    if chunk.startswith('-'):
        args_dict[current_part][chunk.lstrip('-')] = next(iter)

print(args_dict)

Output:

{'part1': {'b': 'testscene.blend', 'python': 'localrender.py'},
 'part2': {'samples': '100', 'start': '1', 'type': 'diffuse'}}

I'm not really sure what you're trying to do with these arguments though, please clarify your question if you want more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM