title: Python scripting in Blender: how can I save a sequence of rendered images?
body: <p>I'm very new to Python scripting, so I've been trying something simple -- an animation of a bunch of cubes doing a 3D random walk.</p>
  
  <p>I've managed to get the program to render every frame of the process, but I have no idea how to save each frame. Can anyone help?</p>
  
  <p>Here's the code. What I'm missing would come in the def render_and_save function.</p>
  
  <p>Cheers!</p>
  
  <pre><code>import bpy
  import random
  
  number_of_cubes = 10
  animation_length = 10
  
  def create_cubes(number_to_make):
      i = 0
      while i &lt; number_to_make:
          bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 0))
          i += 1
  
  def move_cube():
      bpy.ops.transform.translate(value=(random.randint(-1,1), random.randint(-1,1), random.randint(-1,1)), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
  
  def select_cube(cube_name):
      bpy.ops.object.select_all(action = "DESELECT")
      bpy.ops.object.select_pattern(pattern = cube_name)
  
  def move_all():
      j = 0
      while j &lt; number_of_cubes:
          if j == 0:
              name_of_cube = "Cube"
              print(name_of_cube)
          elif j &lt; 10:
              name_of_cube = "Cube.00" + str(j)
              print(name_of_cube)
          elif j &lt; 100:
              name_of_cube = "Cube.0" + str(j)
              print(name_of_cube)
          select_cube(name_of_cube)
          move_cube()
          j += 1
  
  def render_and_save(moves):
      bpy.ops.render.render(use_viewport = True)
      filename = str(moves)+".png"
      #But what should go here to make it save each image?
  
  create_cubes(number_of_cubes)
  
  moves = 0
  while moves &lt; animation_length:
      move_all()
      render_and_save(moves)
      moves += 1
  </code></pre>