2
$\begingroup$

My blender is at the path of '/home/abc/Destkop/blender/blender-2.78'. When command line is at the path '/home/abc/Destkop/blender/blender-2.78', executing './blender -b /home/abc/Destkop/blender/car.model' through command line is working.

I am trying to execute blender in python through subprocess, but the code doesn't work with an error "/usr/bin/python: can't open file ./blender -b /home/abc/Destkop/blender/car.model", but the path is all right

here is my code

import os
import sys
import subprocess

if __name__="__main__":
    os.chdir("/home/abc/Destkop/blender/blender-2.78")
    subprocess.Popen([sys.executable],"./blender -b /home/abc/Destkop/blender/car.model ")
$\endgroup$

1 Answer 1

1
$\begingroup$

First I expect you should use subprocess.run() rather than popen, which is a lower level call that offers more options that you aren't using.

I would suggest using a full path rather than the ./ prefix to a command.

The main source of your error is your arguments list, the first parameter to run() or popen() is called args and is the arguments used to run the external application. In your case you are passing the path to the current executable, which would be the python interpreter you are running the script in. The next argument is a single string, which python appears to be interpreting as a file path to a script to run.

The arg parameter should be a list of strings that you would use in a terminal, the first string is the executable and each following string would be each option passed to the executable.

By defining the list on multiple lines for clarity -

myargs = [
    "/home/abc/Destkop/blender/blender-2.78/blender",
    "-b",
    "/home/abc/Destkop/blender/car.model"
    ]
subprocess.run(myargs)

The full path can be made more flexible by using bpy.app.binary_path.

You also have a trailing space after the filename which would get interpreted as part of the filename.

$\endgroup$
6
  • $\begingroup$ thanks, sambler. subprocess is based on python3.5, while my system is based on python3.4 while many packages are based on python3.4 $\endgroup$
    – user824624
    Commented Oct 16, 2017 at 0:35
  • $\begingroup$ run() was added in 3.5, call() was used before that. Whether you use popen() or call() with 3.4, 3.5 or 2.7, the same applies to the args list that you pass to each method. $\endgroup$
    – sambler
    Commented Oct 16, 2017 at 11:36
  • $\begingroup$ The main issue is, the subprocess calls by default do not pass the args through a shell (unless you specify shell = True). Therefore you cannot pass a shell command line, you must pass the individual word strings that the called program would access through the argv vector. $\endgroup$ Commented Nov 3, 2017 at 21:59
  • $\begingroup$ Instead of hardcoding the path to Blender, use bpy.app.binary_path. This is guaranteed to be correct, also on different platforms. I've updated the answer with this, as depending on the current working directory is rather fragile. $\endgroup$
    – dr. Sybren
    Commented Dec 25, 2017 at 13:05
  • $\begingroup$ @dr.Sybren It would be if they were using blender. I'm sure this script is being run like a shell script to initially start blender. $\endgroup$
    – sambler
    Commented Dec 25, 2017 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .