How to keyframe_insert without moving the object I tried the following code:

import bpy

obj = bpy.data.objects['Cube']
obj.keyframe_insert(data_path='location', frame=0)
obj.location.z += 5
obj.keyframe_insert(data_path='location', frame=100)


Then, after executing the code, the Cube position moves in the Z direction.

But, I want to insert keyframe and not move the object on the screen.

How can I insert keyframes without moving objects on the screen?
You can surround your code with something like:

current_frame = bpy.context.scene.frame_current

obj = bpy.data.objects['Cube']
obj.keyframe_insert(data_path='location', frame=0)
obj.location.z += 5
obj.keyframe_insert(data_path='location', frame=100)

s.frame_set(current_frame)


So that the current (or the frame you want) is restored once the key frames are inserted.

Though, that does not guaranty that the object will not move, as its movement is dependent on the key frames you have set.