0
$\begingroup$

I created a script to render images with different came poses with Blender 4.0. But I always get the same image, the camera pose is not updated. I tried with the python interpreter, blender script tab, and with command line. Always same results. I can see the camera being updated in the viewport but it is not taken into account for the rendering. I also tried with the bpy.context.view_layer.update() but it does not help.

Also I have two layers, but the camera is activated in both. Here is the code:

import bpy

context = bpy.context
cam = context.scene.camera

camera_locations = {
    0: (-1.3, 4.3, 2.0),
    1: (-1.3, 4.3, 1.0)
}

cam.rotation_euler[0] = 0.0
cam.rotation_euler[1] = 0.0
cam.rotation_euler[2] = 0.0
context.view_layer.update()

for k, v in camera_locations.items():
    cam.location[0] = v[0]
    cam.location[1] = v[1]
    cam.location[2] = v[2]
    context.view_layer.update()

    bpy.ops.render.render(write_still=True)
$\endgroup$

1 Answer 1

0
$\begingroup$

you should use handlers for this task.

example:

import bpy

context = bpy.context


camera_locations = {
    0: (-1.3, 4.3, 2.0),
    1: (-2.3, 4.3, 1.0)
}

cam = context.scene.camera
cam.rotation_euler[0] = 0.0
cam.rotation_euler[1] = 0.0
cam.rotation_euler[2] = 0.0


def my_handler(scene):
    
    cam = scene.camera
    
    cam.location[0] = list(camera_locations.items())[scene.frame_current][1][0]
    cam.location[1] = list(camera_locations.items())[scene.frame_current][1][1]
    cam.location[2] = list(camera_locations.items())[scene.frame_current][1][2]
    
bpy.app.handlers.frame_change_pre.append(my_handler)

for i in range(0,len(camera_locations.items())):
    bpy.context.scene.frame_set(i)
    bpy.context.scene.render.filepath = "\\tmp\pic%0.2d.png"%i
    bpy.ops.render.render(write_still=True)
  
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .