Blender script keyframe in loop doesn't affect first element Here is mine area with objects (frame 0):


I'm trying to make objects move with python script. Every object on list should move into center of my area. I'm using for loop. Unfortunately first element doesn't want to keep it's initial position on 0 frame. It looks like it is replacing 0 frame position with 20 frame position and then it works properly but it makes every move 20 frames before rest of them. I don't have idea why it is not working properly. Can you see my code and try to find out what is wrong with it?

Code below:

new_loc = [0,0,0]

frame_num = 0

for obj in creatures_names:

    ob = bpy.data.objects[obj.name]
    scene.frame_set(frame_num)
    ob.keyframe_insert(data_path='location', index = -1)

new_radius = radius - 4

while frame_num &lt; settings['frames']:
    frame_num += 20
    for obj in creatures_names:

        ob = bpy.data.objects[obj.name]

        loc = obj.location
        x = float(loc[0])
        y = float(loc[1])
        z = float(loc[2])
        creature_angle = math.atan(y/x)
        print(creature_angle)
        new_x = new_radius * math.cos(creature_angle)
        new_y = new_radius * math.sin(creature_angle)
        new_loc[2] = z
        new_loc[0] = new_x
        new_loc[1] = new_y

        ob.location = new_loc
        ob.keyframe_insert(data_path='location', index = -1)
        frame_num = 20
        scene.frame_set(frame_num)

    new_radius -= 4    


Here is frame 20:


And frame 40:
It looks like you have more than one problem:
One try using obj.keyframe_insert(data_path='location', frame=frame_num)

instead of 

scene.frame_set(frame_num)
    ob.keyframe_insert(data_path='location', index = -1).

Also, you set frame_num = 20 inside your while loop... which should cause an infinite loop.

How to keyframe_insert without moving the object