2
$\begingroup$

I want to make a program, in Python or C++ likely, to carry out batch script functions across multiple .blend files. So how do I open a .blend file and have it auto-run a script (the script is already in the .blend file), without having to do anything manually?

Edit: I am using this command line: Marie.blend --python test.py

I am using this script (test.py):

import bpy

scn = bpy.data.scenes["Scene"]

scn.frame_end = 10

The script works when run 'normally', but when using the command line, the file just opens and the script is ignored. (Auto.run has already been enabled)

$\endgroup$

1 Answer 1

2
$\begingroup$

Take a look at command line arguments in the manual. The python options sections shows how to invoke a python command at run time. You can also register scripts to run at start time. See this question for details.

Two important notes, uncovered from our discussion:

  1. On systems like windows, where you can invoke Blender from the command line by naming the blend file (ie > Marie.blend) the --python argument will be ignored. On such systems you must invoke Blender by name and the --python argument must go before the blend file name.

  2. Scripts invoked at startup are run before everything is initialized. You need to wait for initialization before your script will work. I'll update the answer later with a technique for that.

$\endgroup$
8
  • $\begingroup$ I never used command line arguments in coding. How would you send a certain command line to Blender from another program, say, a C++ program? $\endgroup$ Feb 15, 2022 at 19:05
  • $\begingroup$ That would depend on the OS. In Unix-like systems you might call system. The system man page would explain the arguments. There's something similar for Windows. $\endgroup$ Feb 15, 2022 at 19:54
  • $\begingroup$ ARGH How do I do this?? I got this, it opens the file but does not run the script: import os cwd = os.getcwd() filepath = '"Marie.blend" --python Text' os.system(filepath) $\endgroup$ Feb 15, 2022 at 20:34
  • $\begingroup$ Sorry, the comments will not let me do line breaks... $\endgroup$ Feb 15, 2022 at 20:35
  • 1
    $\begingroup$ Is the script an external file or is it a text block inside the blender file? For an external file you need to pass system the equivalent of blender --python filename.py but for a text block use blender --python-text textname I suggest getting the command to work properly from the command line first; and then when you're sure it works write the C++ code to invoke system. Also, remember that you have to have 'autorun' enabled in preferences to be able to run scripts. $\endgroup$ Feb 15, 2022 at 20:55

You must log in to answer this question.

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