How to pass command line arguments to a Blender python script or add-on

When starting Blender from the console it processes all parameters passed through the command line. However, some scripts and add-ons for proper work may require specifying their unique command line arguments. If you specify such additional parameters in the command line, Blender will also try to process them, which is likely to result in an error. Blender provides a special way to exclude such arguments from own processing.

Adding a double dash without an argument allows Blender to ignore subsequent parameters.

If you specify the command line like:

The parameters “param1” and “param2” will not be processed by Blender.

Consider how this works:

Let’s write the simplest script that outputs its special parameters passed through the command line.

A full list of the command line arguments is available in Blender’s scripts and add-ons through the common python variable “sys.argv”. Let’s check the double dash in the command line arguments and save the following parameters for later usage.

To make our work with passed parameters more convenient let’s use the “argparse” module.

We defined two arguments -s1 (file path) and -s2 (integer) which values can be passed through the command line. Inside the script that arguments can be accessed through the args object: args.sample_1 and args.sample_2.

Full code for printing the arguments passed to our script through the command line:

Save it to file with the name param_script.py.

A specific script execution can be called through the common Blender -P command line argument with specifying the full file path.

The command line for calling our script:

for Windows (all the necessary files are located in the root of drive D):

After executing this command, the following parameters will be displayed:

for Linux (all the necessary files are located in the /tmp directory):

After executing this command, the following parameters will be displayed:

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Inline Feedbacks
View all comments