Generate pictures by rotating a scene from python

I’m a blender n00b, and I’m trying to generate a bunch of pictures from a scene to use in a machine learning project. To do that I have made a python script. The python script runs, but all the pictures it produce are identical. It seems that my code (that I stole from https://stackoverflow.com/questions/14982836/rendering-and-saving-images-through-blender-python) isn’t working (full code at the bottom of this message).

To be specific, there is one particular line that is intended to rotate the entire scene a bit, that doesn’t seem to do anything at all:

    origin.rotation_euler[2] = math.radians(angle)

So, does any of you know how to programmatically rotate the entire scene some angle between frames, either by tweaking the script below, or by some other mechanism that actually works, I’d be thrilled to know :slight_smile: I’m using blender 2.90.1

Best wishes

Bjørn

def rotate_and_render(output_dir, output_file_format, rotation_steps = 4, rotation_angle = 360):

  # Setting rendering parameters
  bpy.context.scene.render.image_settings.file_format='JPEG'
  bpy.ops.object.add(type = 'EMPTY')
  origin = bpy.context.object
  origin.rotation_mode = 'XYZ'  

  # Setting resolution
  for scene in bpy.data.scenes:
    scene.render.resolution_x = 48
    scene.render.resolution_y = 27

  for step in range(0, rotation_steps):
    print("in da loop")


    angle = step * (rotation_angle / rotation_steps)
    origin.rotation_euler[2] = math.radians(angle)
    print("Post rotation: ", angle)
    print("Euler angle ",     origin.rotation_euler[2])
    
    filename  = output_file_format.format(step, angle)
    bpy.context.scene.render.filepath = output_dir + filename
    print("Pre rendering")        
    bpy.ops.render.render(write_still = True)

  bpy.ops.object.delete(confirm = False)

Edit I just read your code, it looks like your problem is that the objects in your scene are not parented to the programmically created empty, you’re just spinning an empty around, and empies themselves are not rendered. You must parent all the object to the empty and they will then rotate with it.

1 Like

Thank you for you reply. I tried to set everything to be parented by the “origin” object, but this led to no change in rendered images. Do you have any hints about what else I could do/should do to change the parent/child relationship?

  # Parenting
  for ob in bpy.data.objects:
     if ob != origin:
       print("Setting parent of ", ob, " to ", origin)
       ob.parent = origin

try

if ob.name != ‘origin’:

1 Like

and
ob.parent = bpy.data.objects[‘origin’]

1 Like

This did the trick:

 # Parenting
  for ob in bpy.data.objects:
      if ob != origin and ob.name != 'Camera':
       ob.parent = origin

… which makes sense: If the camera rotates with everything else, the angle between the camera and everything else will not change :slight_smile:

Thank you for your help. It was very useful. Take care :slight_smile:

1 Like

Oh that’s funny, of course!