Python Render Script - Django Integration

This is kinda ma first post in Python Support.

I’m into Django, which fully dwells on the Python language. I recently built blender from source into a module. Importing the .py in the Django framework works nice.

My problem: I’m not yet a sharp-edged python programmer, thus lacking the needed skills. I made a simple UI to that effect below [attached]

The script will basically do the below upon .blend upload:

  • Decompress file uploaded if its .zip
  • Read render engine of .blend
  • enable all layers of .blend
  • If cycles render engine, proceed, if Blender Internal, abort
  • if render samples of .blend is =< 500 proceed with render
  • else, make render samples = 500
  • render .blend
  • save the rendered .png (or any other format) of .blend to a location on disk
  • render_to_response supplied with whereabouts of rendered .png
  • a bit of cleaning

Your support is much appreciated. Thank you in advance.:slight_smile:


Youll have two parts:

  1. a script that calls blender from command line, possibly in background mode (-b)
  2. a script that is run inside blender to do the blender stuff

The following should help you on your way.
However, for safety reasons be careful with script one. Make sure nobody can put in nasty stuff like “; rm -rf *”

example for 1):


import subprocess
blender = 'C:\\Program Files\\Blender Foundation\\Blender267b\\blender.exe'
command = ' '.join([blender, '-b render_file.blend -P render_tha_file.py'])
ans = subprocess.call(command, stderr=subprocess.STDOUT)

example for 2):


import bpy
import os.path

filepath = 'D:/tmp/renders'

if not os.path.exists(filepath):                    
    os.makedirs(filepath)                    
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.layers = [True for _ in range(20)] #layers is a list of 20 bools. we put 20xTrue in there.
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.image_settings.color_mode = 'RGBA'
bpy.context.scene.render.resolution_x = resolution.x
bpy.context.scene.render.resolution_y = resolution.y
    output_path = os.path.join(filepath, 'render-{}.png'.format(size))
    if not os.path.exists(output_path):
        bpy.context.scene.render.filepath=output_path
        bpy.ops.render.render(animation=False, write_still=True)

Thank you very much. I think this is a good boostrap for me. Thanks