Running a python script within blender from command line?

Hi guys,

Does anyone know how I can get Blender’s python interpreter to run a python script from command line?

For example, if I have a blender file called “foo.blend” which does not contain any python scripts at the moment, and I have a python file called “bar.py”, and I want tell blender to open up foo.blend and then immediately execute bar.py from command line. Is this possible?

Thanks a lot.

-D

blender foo.blend -P bar.py
blender -b foo.blend -P bar.py, if you dont want to open the blend file in blender, but just want to execute bar.py and exit (for example, I use it for some custom rendering, where I dont want to open the blend file but just render it and exit).

Thanks so much DreamArtist! That was brilliant!

Now, another question, how do you pass arguments to the python script without making blender thinks it’s another blender file? For example, if I give the command line:

blender foo.blend -P bar.py file1

blender would give an error that says:


Loading file1 failed: File is not a Blender file

However, I do realize that I can still get the entire argv using python’s sys.argv, so I’m just wondering if there was a nicer way to do this?

Thanks a lot.

-D

How about reading the forum here before asking?
http://blenderartists.org/forum/showpost.php?p=923523&postcount=2

I read that post, and I’m afraid that’s a terrible solution. Not only do you have to create a temporary file, you also have to include the name of that temporary file in your python script.

I think a better solution would be to take advantage of the fact that the -P option doesn’t care what the actual script is called, so I’d create a shell script that has the following command line argument format:


blenderSH <blender file name> <python script name> <python arg1> < python arg2> ....

then you just concatenate the script name with all its arguments together to get a new string, then you create a symbolic link with the new string to the actual python script, then in your python code, you just use sys.argv to get the name of the symbolic link, then you can parse it to get the names of your arguments. Finally, you can remove that symbolic link.

For example, if you want to pass “10 f1 f2” to your script bar.py to run with foo.blend, you’d create a symlink called bar.py_10_f1_f2.

Alternatively, if you cannot find a suitable separator, you can just get python to give you a tmp unique file name, then write the args into that file, then concat the tmp file’s name to the back of your script name. For example, if os.tempnam gives you ‘/tmp/filepuO9wv’, you can create a symlink called bar.py_filepuO9wv, then write all your arguments into ‘/tmp/filepuO9wv’.

This way, you just need to write a few lines of code for parsing a string, or openning a file, and avoid hardwiring a file name into your code.

Hope it helps.

-D

I’m entirely against that approach.

If you really want changes then you should be asking for a commandline argument like a “-PA”

adding anything to the script name is evil

you could also set environment variables before running blender and then get them from within python once blenders started,

Oh well, suit yourself then. My approach doesn’t change the script name at all, and asking for a new feature is not solution that can be implemented soon.

Yeah, that would work too, but has a small risk of env var name clashing and overwriting.

modified… see below

I think you can emulate the feature you want with a bit of work. Here is my idea (assuming you are on linux):

  1. write a script say blpy.sh and let /usr/bin/blender (or whatever your path) point to blpy.sh instead of your original blender
  2. arguments to this script is exactly same as args to blender except that it allows additional -PA option whose value is “some string” (let quotes be there in the value) where some string is set of args you want to pass to your python script
  3. your blpy.sh should look for -PA option and set pyargs=“some string” where some string will be empty if no -PA is passed
  4. blpy will do “export BLPY_ARGS=“some string”; bash -c /actual/path/blender … rest of args…”. This will make sure that no two instances of blpy’s clash in the env variable.
  5. write a python module myblpyargs.py and place it in your library (I have my own library dir for blender python. I place it there)
  6. the module should just behave like sys.argv etc… where when you call myblpyargs.argv(), it should parse string specified in $BLPYARG and return arguments into our code

so this should emulate exactly the behavior you want with -PA. I dont have code written right now, but may right in next 2-3 weeks. If you are planning to write I will wait for it :slight_smile: