Python keyframes (making my own defenition)

Hi,

firstly, my friend suggested this forum :wink:

I’m trying to make a defenition to make keyframes at the specified frame (with changed location and rotation). so far i came up with this as my best working version:

import bpy

#my defenition
def placeKeyPoint(locatie, rotatie, frame, object):
    #set active object to the correct one
    bpy.context.scene.objects.active = object

    #set the frame to the requested frame
    bpy.context.scene.frame_set(frame)
    #change location and rotation
    bpy.context.object.location.xyz = locatie
    bpy.context.object.rotation_euler = rotatie
    #make a keyframe
    bpy.ops.anim.keyframe_insert(type='LocRot', confirm_success=True)
    # put the frame back to 0
    bpy.context.scene.frame_set(0)
    
#here i make objects and test my defenition
bpy.ops.mesh.primitive_ico_sphere_add()
bpy.ops.mesh.primitive_cube_add()
placeKeyPoint([0, 0, 0], [0,0,0],0, bpy.data.objects['Icosphere'])
placeKeyPoint([3,3,3],[0,0,0],10, bpy.data.objects['Icosphere'])
placeKeyPoint([-3,-3,-3],[0,0,0],20, bpy.data.objects['Icosphere'])

#see note
placeKeyPoint([0,0,0],[0,0,0],0, bpy.data.objects['Cube'])
placeKeyPoint([1,3,5],[0,0,0],10, bpy.data.objects['Cube'])
placeKeyPoint([-1,-3,-5],[0,0,0],20, bpy.data.objects['Cube'])
placeKeyPoint([-3,1,5],[1,1,1],40, bpy.data.objects['Cube'])

At first, I make both of my objects (at [0,0,0]), and then I try to use my defenition on them. In this example, it doesn’t work on the Icosphere but it does on the cube. But if I move the line at wich I create the Cube and move it to the #see note line, then both objects will have keyframes and will be animated. I need to be able to make all objects first, plz help me on how to fix this,

Grtz,

Desam0