# Why are there no key_frames created with Python code?

Thanks to the answer on my question Python convert curve to object, I can now extrude SVG curves and move and rotate the objects in Python.

Now I want to start with animation in Python, and I was hoping to insert some keyframes in a similar fashion as done in the example below which I found here

import bpy

positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1)
start_pos = (0,0,0)

ob = bpy.context.active_object

frame_num = 0

for position in positions:
# bpy.context.scene.frame_set(frame_num)
ob.location = position
ob.keyframe_insert(data_path="location", index=-1, frame=frame_num)
frame_num += 10


This works and lets a sphere move around. For my project with movement and rotation I came up with the following code:

import bpy
from math import *

context = bpy.context
scene = context.scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.import_curve.svg(filepath = "~/tmp/barkruk.svg")
newcurves = [c for c in context.scene.objects if not c.select]
# deselect all
bpy.ops.object.select_all(action='DESELECT')
# create an empty make it the active  object
empty = bpy.data.objects.new("Holder", None) # use filename maybe
scene.objects.active = empty
empty.select = True
# loop thru the curve objects
for c in newcurves:
print("Curve: %s", c.name)
# make it selected
c.select = True
# make the parent the empty if not already set
if not c.parent:
c.parent = empty
# change some curve details
curve = c.data
curve.dimensions = '2D'
curve.extrude = 0.003
# sane object names
c.name = curve.name
# location
#c.location.x += 0.332

empty.scale *= 2

bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
scene.objects[1].location.x=-1
scene.objects[3].location.x=1

bpy.context.scene.frame_set(0)
scene.objects[3].keyframe_insert(data_path="rotation_euler", frame=10)
scene.objects[1].keyframe_insert(data_path="rotation_euler", frame=20)
scene.objects[2].location.z=0.12
scene.objects[2].keyframe_insert(data_path="location", frame=30)
scene.objects[1].location.x=0
scene.objects[1].keyframe_insert(data_path="location", frame=40)
scene.objects[3].location.x=0
scene.objects[3].keyframe_insert(data_path="location", frame=50)


But this doesn't do anything more than the answer in my previously asked question. At least I don't see the keyframes appearing on the timeline. Where do I go wrong and how can I generate keyframes?

Each curve object is still in the newcurves collection, hence newcurves[index] can be used rather than scene.objects[index]

The behaviour in the example sphere script you posted appears to rely on obj.location = (x,y,z) being set as opposed to obj.location.x = x etc.

My advice would be call scene.frame_set(f) update your props, insert keyframe, repeat. The frame argument of keyframe_insert defaults to scene.frame_current. Also to keyframe in only the x location pass index=0.

# set initial locs / rots here
newcurves[0].location.x=-1
newcurves[2].location.x=1

bpy.context.scene.frame_set(0)
# keyframe in all at frame 0
for curve in newcurves:
curve.keyframe_insert(data_path="rotation_euler")
curve.keyframe_insert(data_path="location")

# call frame set (updates the scene as well)
scene.frame_set(10)
# update the properties
# insert the keyframes
newcurves[2].keyframe_insert(data_path="rotation_euler")

scene.frame_set(20)
newcurves[0].keyframe_insert(data_path="rotation_euler", frame=20)

scene.frame_set(30)
newcurves[1].location.z=0.12
newcurves[1].keyframe_insert(data_path="location", index=2)

scene.frame_set(40)
newcurves[0].location.x=0
newcurves[0].keyframe_insert(data_path="location", index=0)

scene.frame_set(50)
newcurves[2].location.x=0
newcurves[2].keyframe_insert(data_path="location", index=0)