rendering and saving images through Blender python rendering and saving images through Blender python python python

rendering and saving images through Blender python


The code below creates a "VR panorama" (a series of pictures of an object, from different perspectives around it).

I ended up with this algorithm:

  1. create or load an object you are going to take pictures of (the subject)
  2. scale it and add some nice lighting (so that the object is visible from directions you want); you can check the lighting by rendering the scene (using the F12 key)
  3. create an Empty object and set its position to the center of the subject and rotation to identity (0, 0, 0)
  4. set your camera view to the starting position (check it with rendering, again)
  5. open interactive Python shell (Shift+F4)
  6. paste & run the script

You shall end up with a number of pictures (defined by rotation_steps) around your object under /Users/myusername/Pictures/VR directory:

def rotate_and_render(output_dir, output_file_pattern_string = 'render%d.jpg', rotation_steps = 32, rotation_angle = 360.0, subject = bpy.context.object):  import os  original_rotation = subject.rotation_euler  for step in range(0, rotation_steps):    subject.rotation_euler[2] = radians(step * (rotation_angle / rotation_steps))    bpy.context.scene.render.filepath = os.path.join(output_dir, (output_file_pattern_string % step))    bpy.ops.render.render(write_still = True)  subject.rotation_euler = original_rotationrotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg')

You will have to select the object you want to render. Alternatively, you can use Blender's Python API to find the object in the scene and pass it as a subject param to the function:

rotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg', subject = bpy.data.objects["Cube"])


something like this:

import bpybpy.context.scene.render.filepath = 'pathToOutputImage'bpy.context.scene.render.resolution_x = w #perhaps set resolution in codebpy.context.scene.render.resolution_y = hbpy.ops.render.render()


You will have to do the following. The i in the second line after the for loop is the loop index of the file loop.

I have verified that this works while running in the console and also from the command line. Don't forget to remove the objects after you render one file. (The remove command is not given here since it is not generic. Some specific arguments will be needed in that command if that object has links)

for area in bpy.context.screen.areas:    if area.type == 'VIEW_3D':        area.spaces[0].viewport_shade = 'RENDERED'bpy.context.scene.render.image_settings.file_format='JPEG'bpy.context.scene.render.filepath = ".pic%0.2d.jpg"%ibpy.ops.render.render(use_viewport = True, write_still=True)