1
$\begingroup$

I'm trying to run a headless Blender from inside a Python program. I intend to have Blender run another Python program after it opens a file.

I can make this work from the command line as such

D:\Documents\Programming\████████>"C:\Program Files\Blender Foundation\Blender 3.6\blender.exe" "kitchen table.blend" -b -P animate_book.py -- "./███████/███████/scripts/chapter-001.csv"
Blender 3.6.0 (hash c7fc78b81ecb built 2023-06-27 08:27:30)
Read prefs: "C:\Users\████████\AppData\Roaming\Blender Foundation\Blender\3.6\config\userpref.blend"
Read blend: "D:\Documents\Programming\█████████\kitchen table.blend"
Info: Total files 73 | Changed 29 | Failed 0
Info: Saved "chapter-001.blend"

This works as expected, opening in background mode, loading kitchen table.blend, and running the script using the custom argument after the --

However, when I try to do the same from Python I can't get Blender to digest the --python argument. Not as -P nor anything else, like --python-text.

Any insights?

I run a Python command like:

subprocess.run(['C:/Program Files/Blender Foundation/Blender 3.6/blender.exe', 'kitchen table.blend', '--background', '--python animate_book.py', '-- ' + chapter.title_filename + '.csv'])

But I get the following error:

Blender 3.6.0 (hash c7fc78b81ecb built 2023-06-27 08:27:30)
Read prefs: "C:\Users\████████\AppData\Roaming\Blender Foundation\Blender\3.6\config\userpref.blend"
Read blend: "D:\Documents\Programming\████████\kitchen table.blend"
unknown argument, loading as file: --python animate_book.py
Error: Cannot read file "D:\Documents\Programming\████████\--python animate_book.py": No such file or directory

I've looked around a bit, and found Blender doesn't recognize --python as a command line argument, but I don't seem to have the same problem? And it works from the command line already, just not from the Python subprocess.run() call?

Any thoughts appreciated.

$\endgroup$
2
  • 1
    $\begingroup$ Perhaps separate --python and the scrip file name in different quotes, as shown here? $\endgroup$
    – L0Lock
    Nov 19, 2023 at 19:54
  • $\begingroup$ OMG, I'm a knob... Thank you. $\endgroup$
    – CDMM
    Nov 19, 2023 at 20:03

1 Answer 1

1
$\begingroup$

Made a test script on my own:

import subprocess

subprocess.run([
    'C:/AppInstall/Blender/stable/blender-4.0.0+stable.878f71061b8e/blender.exe',
    'C:/tmp/test.blend',
    '--background',
    '--python',
    'C:/tmp/script.py'
    ])

Using two declarations for --python- and C:/tmp/script.py works, while combining them as one --python C:/tmp/script.py doesn't.

$\endgroup$

You must log in to answer this question.

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