On Windows, I'm trying to have blender.exe execute a Python script that internally uses the argparse
module for the arguments handling. What I'm struggling with is how to properly pass the arguments that the script expects on the command line.
I'm well aware of this similar question that was already posted here but the solution that is proposed there does not seem to work properly.
First, the script that I want to execute looks as follows:
import argparse
import os
import sys
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--input-folder', help='Folder containing the files to be imported', default='WhateverFolder')
parser.add_argument('--output-file' , help='The path of the file to be created' , default='WhateverFile')
return parser.parse_known_args(sys.argv)
arguments, unknown = parse_arguments()
print('\n--input-folder: %s' % arguments.input_folder)
print('--output-file : %s' % arguments.output_file)
print('\nAll arguments:')
print(sys.argv)
print('\nRecognized Arguments:')
print(arguments)
print('\nUnknown Arguments:')
print(unknown)
According to the already mentioned similar question the arguments that shall be supplied to this script need to follow the sequence --
because apparently that is how blender.exe recognizes arguments that it shall not process and instead forward.
However, when invoking the script as follows the script output shows that the parameters are not properly processed by argparse
.
"%ProgramFiles%\Blender Foundation\Blender\blender.exe" --background --python test.py -- --input-folder OtherFolder --output-file OtherFile
The output that the script generates looks as follows:
Read prefs: C:\Users\<MyAccount>\AppData\Roaming\Blender Foundation\Blender\2.79\config\userpref.blend
found bundled python: C:\Program Files\Blender Foundation\Blender\2.79\python
--input-folder: WhateverFolder
--output-file : WhateverFile
All arguments:
['C:\\Program Files\\Blender Foundation\\Blender\\blender.exe', '--background', '--python', 'test.py', '--', '--input-folder', 'OtherFolder', '--output-file', 'OtherFile']
Recognized Arguments:
Namespace(input_folder='WhateverFolder', output_file='WhateverFile')
Unknown Arguments:
['C:\\Program Files\\Blender Foundation\\Blender\\blender.exe', '--background', '--python', 'test.py', '--', '--input-folder', 'OtherFolder', '--output-file', 'OtherFile']
Blender quit
As can be seen, the default value of arguments.input_folder
does not get overridden with OtherFolder
, nor is arguments.output_file
set to OtherFile
.
If I discard --
before the script arguments the script the outcome is as expected, i.e. arguments.input_folder
is OtherFolder
and arguments.output_file
is OtherFile
.
But the problem is that now blender.exe seems to interpret these arguments as something it needs to process which results in the following error message:
Error: Cannot read file 'C:Temp\--input-folder': No such file or directory
I'm using Blender for Windows, version 2.79b 64-bit.
What am I missing here? What is the proper way of passing command line arguments to a Python script invoked by blender.exe on Windows?
--
are stripped from thesys.argv
list asargv
try parsing that list for your arguments. $\endgroup$