# Blender scripting loop not working

I'm trying to set the camera to always look the origin of the scene and then I want to rotate the camera about the origin to get a new camera pose (i.e. rotate the empty point the camera is looking at in the origin).

This works when I do it without a loop:

b_empty = scene.objects["Empty"]
b_empty.rotation_euler[2] += math.radians(360/20)
print(cam.matrix_world)


But when I do it in a loop it doesn't work and the camera stays the same (same print for cam.matrix_world)

for i in range(20):
b_empty = scene.objects["Empty"]
b_empty.rotation_euler[2] += math.radians(360/20)
print(cam.matrix_world)


what could be going wrong?

• You don't do anything to cam in your script. Why do you expect it to change? Also why do you need the loop? What are you trying to achieve here? Feb 15 at 7:48
• you need make sure obj.rotation_mode = 'XYZ' and run bpy.context.view_layer.update()  before you access the matrix.
– X Y
Feb 15 at 12:00

## 1 Answer

Ultimately your problem could be described as:

my_var = 3
my_var += 1
print(my_var % 10)  # it changed!

for i in range(10):
my_var += 1
print(my_var % 10)  # it didn't change!


Except in your case you don't use modulo; your equivalent to the modulo is, however, the periodic nature of rotation: rotating by $$360°$$ is the same as not rotating at all. range(20) produces 20 numbers, and for each you rotate by $$360\over20$$, so the resulting rotation is $$20{360\over20}$$ degrees; twenties cancel out and you're left with exactly one full rotation.

If you inspect your object's rotation, it has changed. You just don't see it, because an object rotated by 360° looks the same way as before rotation.