Write a blender python script to render files, it needs to be modular and I want to be…
- User
Write a blender python script to render files, it needs to be modular and I want to be able to modify parameters with a json file
- Assistant
Sure! I'll provide you with a basic Python script that you can use to render files in Blender. You can extend the functionality with various render parameters in a JSON file that you can modify externally.
First, here is the Python script file called "render_script.py":
import bpy import json import sys import os def setup_cycles(sample_count, output_path): bpy.context.scene.render.engine = 'CYCLES' bpy.context.scene.cycles.device = 'GPU' # Use this line if you want to render on the GPU bpy.context.scene.cycles.samples = sample_count bpy.context.scene.render.image_settings.file_format = 'PNG' bpy.context.scene.render.filepath = output_path def setup_resolution(resolution_x, resolution_y, percent_scale=100): bpy.context.scene.render.resolution_x = resolution_x bpy.context.scene.render.resolution_y = resolution_y bpy.context.scene.render.resolution_percentage = percent_scale def load_parameters(json_file): with open(json_file, 'r') as f: parameters = json.load(f) setup_cycles(parameters['samples'], parameters['output_path']) setup_resolution(parameters['resolution_x'], parameters['resolution_y'], parameters['percent_scale']) def main(): argv = sys.argv argv = argv[argv.index("--") + 1:] # Get all args after "--" if len(argv) < 2: print("Please provide the .blend file and the JSON parameter file") sys.exit(1) blend_file = argv[0] json_parameters = argv[1] bpy.ops.wm.open_mainfile(filepath=blend_file) load_parameters(json_parameters) bpy.ops.render.render(write_still=True) # Render the image if __name__ == "__main__": main()Now you can create your JSON parameter file called "render_parameters.json":
{ "output_path": "//output/render.png", "samples": 128, "resolution_x": 1920, "resolution_y": 1080, "percent_scale": 100 }To run the script, make sure Blender is added to your PATH variable, and then open a terminal or command prompt in the directory containing both the Python script and the JSON parameter file. Then, execute the following command, replacing "input.blend" with the name of the .blend file you want to render:
blender input.blend --background --python render_script.py -- input.blend render_parameters.jsonThis should load the .blend file and render it based on the parameters you've set in the JSON file.
Feel free to modify the Python script and the JSON parameters to suit your specific needs.
- User
Now I need it to be able to render animations and save them with a specific name in a specific folders (as specified by the json file). I need to be able to set export format such as MP4 and JPEG/PNG
- Assistant
Sure, I made the necessary changes to the script and the JSON file. Here's the updated version of the script, and a new parameter for the frame range has also been added to the JSON file.
Updated "render_script.py":
import bpy import json import sys import os def setup_cycles(sample_count): bpy.context.scene.render.engine = 'CYCLES' bpy.context.scene.cycles.device = 'GPU' # Use this line if you want to render on the GPU bpy.context.scene.cycles.samples = sample_count def setup_output(output_path, output_format): file_format = output_format.upper() if file_format not in ['PNG', 'JPEG', 'FFMPEG']: print("Invalid output format. Please use 'PNG', 'JPEG' or 'FFMPEG'.") sys.exit(1) bpy.context.scene.render.filepath = output_path bpy.context.scene.render.image_settings.file_format = file_format if file_format == 'FFMPEG': bpy.context.scene.render.ffmpeg.format = 'MPEG4' bpy.context.scene.render.ffmpeg.codec = 'H264' bpy.context.scene.render.ffmpeg.constant_rate_factor = 'MEDIUM' def setup_resolution(resolution_x, resolution_y, percent_scale=100): bpy.context.scene.render.resolution_x = resolution_x bpy.context.scene.render.resolution_y = resolution_y bpy.context.scene.render.resolution_percentage = percent_scale def setup_frame_range(start_frame, end_frame): bpy.context.scene.frame_start = start_frame bpy.context.scene.frame_end = end_frame def load_parameters(json_file): with open(json_file, 'r') as f: parameters = json.load(f) setup_cycles(parameters['samples']) setup_output(parameters['output_path'], parameters['output_format']) setup_resolution(parameters['resolution_x'], parameters['resolution_y'], parameters['percent_scale']) setup_frame_range(parameters['start_frame'], parameters['end_frame']) def render_animation(): bpy.ops.render.render(animation=True) def main(): argv = sys.argv argv = argv[argv.index("--") + 1:] # Get all args after "--" if len(argv) < 2: print("Please provide the .blend file and the JSON parameter file") sys.exit(1) blend_file = argv[0] json_parameters = argv[1] bpy.ops.wm.open_mainfile(filepath=blend_file) load_parameters(json_parameters) render_animation() if __name__ == "__main__": main()Updated "render_parameters.json":
{ "output_path": "//output/render", "output_format": "FFMPEG", "samples": 128, "resolution_x": 1920, "resolution_y": 1080, "percent_scale": 100, "start_frame": 1, "end_frame": 250 }Now you can set the desired animation start and end frame as well as the output format (FFMPEG for MP4 videos or PNG/JPEG for image sequences) in the JSON parameters file.
Make sure to update the
output_pathparameter to include the specific folder where you want to save your files. Use forward slashes even on Windows, and Blender will convert it to the appropriate path. You can use "//" to reference the .blend file directory or an absolute path such as "D:/MyFolder/output/render".To run the script, the command remains the same:
blender input.blend --background --python render_script.py -- input.blend render_parameters.jsonThis will now render the animation based on parameters set in the JSON file.