My main goal is to achieve that this script file will open My_file.blend, apply my script on it, render and save an image to output path
-
$\begingroup$ Do you intend that the script also starts Blender or would it already be open? $\endgroup$– Robert GützkowCommented Jul 24, 2019 at 11:52
-
$\begingroup$ I intend that the script also starts Blender, doing the script, and then save an image $\endgroup$– cxntCommented Jul 24, 2019 at 11:57
-
$\begingroup$ that will require two separate scripts. One for starting Blender and one for performing actions in Blender. $\endgroup$– Robert GützkowCommented Jul 24, 2019 at 12:06
-
$\begingroup$ See docs.blender.org/manual/en/latest/advanced/command_line/… for more information $\endgroup$– Robert GützkowCommented Jul 24, 2019 at 12:37
2 Answers
I would suggest using the Blender command line interface (CLI) to trigger the script execution and rendering. The CLI is described in the Blender documentation.
For instance you could start Blender from the command line, let it run a python script and then render a frame using the following command (you will have to add the correct paths to the relevant files). The following command renders frame 0 of project.blend after the script.py has been executed.
.\blender.exe project.blend -b -P script.py -o //frame_ -f 0
-b
or --background
starts Blender without a user interface.
-P
or --python
allows to run a script.
-o
or --render-output
defines how the rendered files will be named and where they will be stored. //
denotes the current working directory.
-f
or --render-frame
tells Blender to render a specific frame.
In case you don't want to execute Blender with this command manually, you can use subprocess
in a another python script to start Blender. The following function is an example of how you could start Blender from another python script. As arguments you need to pass the path to Blender, the project and the script and it will start another process that runs Blender with these arguments.
import subprocess
def run_blender(blender, project, script):
output = subprocess.check_output([blender,
project,
'--background',
'--python', script,
'--render-output', '//frame_',
'--render-frame', '0'])
print(output.decode("utf-8"))
-
$\begingroup$ Fixed a mistake in the argument order. $\endgroup$ Commented Jul 24, 2019 at 13:10
-
$\begingroup$ Cool, have you ever tried
plumbum
I'm addicted to it. $\endgroup$ Commented Jul 24, 2019 at 13:30 -
$\begingroup$ @batFINGER no I haven't, thanks for the tip! $\endgroup$ Commented Jul 24, 2019 at 13:35
-
$\begingroup$ @rjg Could you please edit this code, add random filepath(arguments), so I know where to put it and how to use it and etc I'm pretty new to coding and blender, I don't understand much. Thanks in advance for helping me $\endgroup$– cxntCommented Jul 26, 2019 at 10:14
Start with the basics
blender --background my.blend --python myscript.py
where myscript.py contains your logic and
bpy.ops.render.render(write_still=True)
To produce the render
in your script you can change just about anything e.g.
bpy.context.scene.render.image_settings.file_format='PNG'
You can also pass parameters and include multiple python scripts.
-
$\begingroup$ So I've to create a Script.py and type these lines in it: blender --background my.blend --python myscript.py Right? $\endgroup$– cxntCommented Jul 24, 2019 at 11:39
-
$\begingroup$ @cxnt no that is what you type on the command line to start blender and run the script stored in myscript.py $\endgroup$ Commented Jul 24, 2019 at 11:49
-
$\begingroup$ I ran it in windows 10 cmd, but it doesn't work Should I use Linux? $\endgroup$– cxntCommented Jul 24, 2019 at 11:55
-
1$\begingroup$ No Windows is fine, this is likely because
blender
isn't in your PATH variable and thus it doesn't know what to do with that name. You can either give the full path to your blender.exe or switch to the directory and execute it there. So for example if your Blender is installed inC:\Program Files\Blender Foundation\Blender\blender.exe
and your script and .blend file are inC:\Users\user\Documents\Blender
then you would useC:\Program Files\Blender Foundation\Blender\blender.exe --background C:\Users\user\Documents\Blender\my.blend --python C:\Users\user\Documents\Blender\myscript.py
. $\endgroup$ Commented Jul 24, 2019 at 12:01 -
$\begingroup$ @cxnt Adjust the paths to the ones were you actually stored the files. $\endgroup$ Commented Jul 24, 2019 at 12:01