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
-
Do you intend that the script also starts Blender or would it already be open? – Robert Gützkow Jul 24 '19 at 11:52
-
I intend that the script also starts Blender, doing the script, and then save an image – cxnt Jul 24 '19 at 11:57
-
that will require two separate scripts. One for starting Blender and one for performing actions in Blender. – Robert Gützkow Jul 24 '19 at 12:06
-
See https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html for more information – Robert Gützkow Jul 24 '19 at 12:37
2 Answers
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.
- 1,718
- 1
- 16
- 21
-
So I've to create a Script.py and type these lines in it: blender --background my.blend --python myscript.py Right? – cxnt Jul 24 '19 at 11:39
-
@cxnt no that is what you type on the command line to start blender and run the script stored in myscript.py – Robert Gützkow Jul 24 '19 at 11:49
-
-
1No Windows is fine, this is likely because
blenderisn'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.exeand your script and .blend file are inC:\Users\user\Documents\Blenderthen 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. – Robert Gützkow Jul 24 '19 at 12:01 -
@cxnt Adjust the paths to the ones were you actually stored the files. – Robert Gützkow Jul 24 '19 at 12:01
-
This is what i get:
C:\Program Files\Blender Foundation\Blender>blender.exe --background C:\Users\7even\Documents\Blender\UnaEdited.blend --python C:\Users\7even\Documents\Blender\HairGenerationScript280.py Blender 2.80 (sub 74) (hash 38d4483c6a51 built Thu 07/18/2019 09:28 AM) AL lib: (EE) UpdateDeviceParams: Failed to set 48000hz, got 44100hz instead found bundled python: C:\Program Files\Blender Foundation\Blender\2.80\python Read blend: C:\Users\7even\Documents\Blender\UnaEdited.blend
Blender quit
– cxnt Jul 24 '19 at 12:26 -
that looks fine. Did it write the render where you expected? Try without the --background parameter and Blender will open and stay open after everything has finished so you can check it did what you wanted. – rob Jul 24 '19 at 14:06
-
@rjg so what if I want to apply two or three scripts on by one? is it possible? – cxnt Jul 26 '19 at 09:23
-
@cxnt yes that is possible also via command line. You can simply add another
-Por--pythonfollowed by the path to another script. See: https://blender.stackexchange.com/questions/32986/run-multiple-python-scripts-in-blender-from-the-command-line – Robert Gützkow Jul 26 '19 at 09:32
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"))
- 25,622
- 3
- 47
- 78
-
-
-
-
@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 – cxnt Jul 26 '19 at 10:14