How to pass arguments when invoking python script from blender on the command line

Thing you want to do

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.

error

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

Solution

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

Impressions

Sys.argv [sys.argv.index ('-') + 1:], which uses argparse to retrieve arguments, feels magical.

reference

Recommended Posts

How to pass arguments when invoking python script from blender on the command line
How to manage arguments when implementing a Python script as a command line tool
[Python / Tkinter] How to pass arguments to command
How to receive command line arguments in Python
How to create an article from the command line
Execute Python function from Powershell (how to pass arguments)
How to specify command line arguments when debugging in PyCharm
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
How to pass arguments to a Python script in SPSS Modeler Batch
How to get a string from a command line argument in python
[EC2] How to install and download chromedriver from the command line
How to hide the command prompt when running python in visual studio 2015
When running a Python shell from Electron, pass multiple arguments to run Python.
[Python] How to test command line parser click
Let's understand how to pass arguments (Python version)
How to measure line speed from the terminal
[Introduction to Udemy Python3 + Application] 67. Command line arguments
Convert XLSX to CSV on the command line
How to pass the execution result of a shell command in a list in Python
Correspondence memo when the direction key cannot be used on the python command line
[Python] How to remove duplicate values from the list
Think about how to program Python on the iPad
How to know the current directory in Python in Blender
The first step to getting Blender available from Python
How to enjoy Python on Android !! Programming on the go !!
(Remember quickly) How to use the LINUX command line
Steps to use the AWS command line interface (Python / awscli) on Mac OS X
How to run a Python program from within a shell script
Search for large files on Linux from the command line
[Hyperledger Iroha] Notes on how to use the Python SDK
How to deploy the easiest python textbook pybot on Heroku
[Python] Read command line arguments from file name or stdin
When you want to hit a UNIX command on Python
Pass OpenCV data from the original C ++ library to Python
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to rebuild python environment from pyenv on Mac environment (El Capitan)
How to pass and study the Python 3 Engineer Certification Basic Exam
How to get followers and followers from python using the Mastodon API
Things to note when running Python on EC2 from AWS Lambda
How to deal with the problem that the current directory moves when Python is executed from Atom
In the python command python points to python3.8
How to get the Python version
How to avoid duplication of data when inputting from Python to SQLite.
[Python] How to import the library
How to pass arguments using an instance with systemd's systemctl command
How to extract the desired character string from a line 4 commands
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python] I tried to make a simple program that works on the command line using argparse.
How to access wikipedia from python
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
How to update the python version of Cloud Shell on GCP
Articles to read when Blender Python script code doesn't work in 2.80
Python standard module that can be used on the command line
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Update Python on Mac from 2 to 3
How to fix the shit heavy when reading Google Cloud Storage images from Django deployed on GAE
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
How to use gcc when compiling extension modules from setup.py on Mac
How to deal with OAuth2 error when using Google APIs from Python
How to draw a vertical line on a heatmap drawn with Python seaborn