Insert a keyframe on the property given, adding fcurves and animation data when necessary.

Parameters:

data_path (string) – path to the property to key, analogous to the fcurve’s data path.

index (int) – array index of the property to key. Defaults to -1 which will key all indices or a single channel if the property is not an array.

frame (float) – The frame on which the keyframe is inserted, defaulting to the current frame.

group (str) – The name of the group the F-Curve should be added to if it doesn’t exist yet.

options – Some optional flags:
‘NEEDED’: Only insert keyframes where they’re needed in the relevant F-Curves.
‘VISUAL’: Insert keyframes based on ‘visual transforms’.
‘XYZ_TO_RGB’: Color for newly added transformation F-Curves (Location, Rotation, Scale) and also Color is based on the transform axis.

Returns:

Success of keyframe insertion.

Return type:

boolean

This is the most simple example of inserting a keyframe from python.

importbpyobj=bpy.context.object# set the keyframe at frame 1obj.location=3.0,4.0,10.0obj.keyframe_insert(data_path="location",frame=1)

Note that when keying data paths which contain nested properties this must be
done from the ID subclass, in this case the Armature rather
then the bone.

importbpyfrombpy.propsimportPointerProperty# define a nested propertyclassMyPropGroup(bpy.types.PropertyGroup):nested=bpy.props.FloatProperty(name="Nested",default=0.0)# register it so its available for all bonesbpy.utils.register_class(MyPropGroup)bpy.types.Bone.my_prop=PointerProperty(type=MyPropGroup,name="MyProp")# get a boneobj=bpy.data.objects["Armature"]arm=obj.data# set the keyframe at frame 1arm.bones["Bone"].my_prop_group.nested=10arm.keyframe_insert(data_path='bones["Bone"].my_prop.nested',frame=1,group="Nested Group")