0
$\begingroup$

I am using Blender 4.3.2 and writing a Python script to render animations via command-line using bpy. Below is the command I use to execute the script:

/blender/blender-4.3.2-linux-x64/blender --enable-autoexec -b -noaudio -P /blender/render.py -- {blend_file_path} {self.start} {self.end} 0

Unable to set tile size.How can I correctly set the tile size to 1024 x 1024 in Python?

Any guidance or suggestions would be greatly appreciated! Here is the relevant code snippet:

#render.py
import bpy
import sys

blend_filepath = sys.argv[-4]
start = int(sys.argv[-3])
end = int(sys.argv[-2])
select_dev = int(sys.argv[-1])
output_path = "/blender/output/"
print(f"From {start} To {end} using GPU {select_dev}")

# Configure Cycles rendering settings
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = "OPTIX"
cycles_prefs = bpy.context.preferences.addons['cycles'].preferences
cycles_prefs.refresh_devices()

for device in cycles_prefs.devices:
    device.use = False

i = 0
for device in cycles_prefs.devices:
    if device.type == 'OPTIX':
        if i == select_dev:
            device.use = True
        i += 1

bpy.ops.wm.open_mainfile(filepath=blend_filepath)
bpy.context.scene.cycles.device = "GPU"
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.filepath = output_path + "img"
bpy.context.scene.frame_start = start
bpy.context.scene.frame_end = end
bpy.context.scene.cycles.tile_x = 1024
bpy.context.scene.cycles.tile_y = 1024
bpy.context.scene.cycles.samples = 48
bpy.context.scene.cycles.adaptive_threshold = 0.1
bpy.context.scene.cycles.use_denoising = True
bpy.context.scene.cycles.denoiser = 'OPTIX'

bpy.ops.render.render(animation=True)

print("-----------------")
for device in cycles_prefs.devices:
    print("useDevice:", device.name, device.type, "use", device.use)

bpy.ops.wm.quit_blender()
```
$\endgroup$
1
  • $\begingroup$ If you do something after starting rendering in a script, it might produce unexpected results, because a Python scripts do not wait for rendering process to be finished properly. I would suggest setting what you need in the script, then rendering using command line functionality for that. You also try to open a file in the Python script - that makes no sens. The script will be executed with the file you open in command line, I also don't think you need to quit Blender in the script. $\endgroup$ Commented Jan 17 at 19:27

1 Answer 1

0
$\begingroup$

OK, so I tested it and what works is this:

I have a blend file and a script file and a batch file:

enter image description here

My batch file simply opens Blender in background mode (-b), loads the file and then runs the Python script (-P [path to script]) and then renders animation (-a). For me(on Windows) it looks like this:

"C:\Program Files\Blender Foundation\Blender 4.3\blender.exe" -b "C:\Users\-\Desktop\Command Line Test\a.blend" -P "C:\Users\-\Desktop\Command Line Test\s.py" -a

And that's it for the command line. The Python script only imports bpy and changes the settings I want to change:

import bpy

bpy.data.scenes["Scene"].cycles.use_auto_tile = True
bpy.data.scenes["Scene"].cycles.tile_size = 1024

Nothing more. No need to load the scene, no need to render there, no need to quit. All that stuff is done by the command line render. And it works fine. It renders tiles even though they are disabled in my file:

enter image description here

You can obviously add more stuff you change. It might be easier to just set what you want in the Python script without passing all the arguments around from command line, if you ask me. You are editing some script file to set it anyway, might as well be the Python one... but that's obviously up to you.

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.