Skip to content

Instantly share code, notes, and snippets.

@sambler
Created April 5, 2018 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sambler/fd9e1d11f2328a7e8d70a78a9d31d914 to your computer and use it in GitHub Desktop.
Save sambler/fd9e1d11f2328a7e8d70a78a9d31d914 to your computer and use it in GitHub Desktop.
Start background instances of blender to do rendering
#!/usr/bin/env python3
import os
import subprocess as sp
# this script is sent to each instance to do the render
# use str.format() to fill in the {}'s
render_scr = """import bpy
scn = bpy.context.scene
scn.render.use_sequencer = False
scn.render.engine = 'CYCLES'
scn.cycles.samples = 2
scn.frame_end = 5
scn.use_nodes = True
for node in scn.node_tree.nodes:
scn.node_tree.nodes.remove(node)
renderNode = scn.node_tree.nodes.new('CompositorNodeRLayers')
# Depth map
depthOutputNode = scn.node_tree.nodes.new('CompositorNodeOutputFile')
depthOutputNode.format.file_format = 'OPEN_EXR'
depthOutputNode.format.color_depth = '32'
depthOutputNode.format.color_mode = 'RGB'
depthOutputNode.format.exr_codec = 'ZIP'
depthOutputNode.base_path = '//somepath/{}/'
depthOutputNode.file_slots[0].path = 'fileNameDepth#'
# Link
scn.node_tree.links.new(renderNode.outputs[2], depthOutputNode.inputs[0])
bpy.ops.import_scene.obj(filepath="{}")
bpy.ops.render.render(write_still=True, animation=True)
"""
# adjust this if blender isn't in your $PATH
blender_cmd = 'blender'
objPaths = ['obj1.obj', 'obj2.obj', 'obj3.obj', 'obj4.obj']
for op in objPaths:
# sp.Popen will start the process and leave it running in the background
# sp.run can be used to wait for completion before going to next file
sp.Popen([blender_cmd,
'--factory-startup', '--background', 'pystart.blend',
'--python-expr', render_scr.format(op,op)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment