0

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

cxnt
  • 397
  • 1
  • 5
  • 27

2 Answers2

2

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'

Set the output filename

You can also pass parameters and include multiple python scripts.

rob
  • 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
  • I ran it in windows 10 cmd, but it doesn't work Should I use Linux? – cxnt Jul 24 '19 at 11:55
  • 1
    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 in C:\Program Files\Blender Foundation\Blender\blender.exe and your script and .blend file are in C:\Users\user\Documents\Blender then you would use C:\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 -P or --python followed 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
2

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"))
Robert Gützkow
  • 25,622
  • 3
  • 47
  • 78