import bpy
# Create a new cube object
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0)) # Default location
# Get a reference to the newly created object
obj = bpy.context.active_object
# Set the start and end frames for the animation
start_frame = 1
end_frame = 20
# Set the starting position of the object
obj.location = (0, 0, 0)
# Insert a keyframe for the starting position at the start frame
obj.keyframe_insert(data_path="location", frame=start_frame)
# Move the object to the ending position
obj.location = (2, 4, 1) # Example ending position
# Insert a keyframe for the ending position at the end frame
obj.keyframe_insert(data_path="location", frame=end_frame)
Related