Making Keyframes Linear

Hi,
So I’ve been scratching my head for quite a while at this. Many posts elsewhere almost answer the question just not quite

I have created an animation (simplified for this post) but despite my best efforts I can’t set the interpolation to be linear

This is what I started with

bpy.context.active_object.animation_data_clear()
moved_bones = bpy.context.active_object.pose.bones

for bone in moved_bones:
    bone.bone.select = True
   
    bone.keyframe_insert(data_path='location', frame=(10))
    bone.location = Vector((0.0, 0.0, 0.0))
    bone.keyframe_insert(data_path='location', frame=(1))

Then as someone suggested elsewhere I try this, where I briefly change the default interpolation type in user preferences

bpy.context.active_object.animation_data_clear()
moved_bones = bpy.context.active_object.pose.bones

for bone in moved_bones:
    bone.bone.select = True

    keyInterp = bpy.context.preferences.edit.keyframe_new_interpolation_type
    bpy.context.preferences.edit.keyframe_new_interpolation_type = 'LINEAR'
    
    bone.keyframe_insert(data_path='location', frame=(10))
    bone.location = Vector((0.0, 0.0, 0.0))
    bone.keyframe_insert(data_path='location', frame=(1))

    bpy.context.preferences.edit.keyframe_new_interpolation_type = keyInterp

but this failed.

Then I started working down into the world of fcurves. I made some progress using other posts and blender docs and came up with this on a simple object

import bpy

obj = bpy.context.object
obj.animation_data_create()
obj.animation_data.action = bpy.data.actions.new(name="MyAction")

fcurve = obj.animation_data.action.fcurves.new(data_path="location")

keyframe_one = fcurve.keyframe_points.insert(frame=0.0, value=5.0)
keyframe_two = fcurve.keyframe_points.insert(frame=10.0, value=15.0)

However:

keyframe_one.interpolation = "LINEAR"
keyframe_two.interpolation = "LINEAR"

doesn’t appear to do anything. Nor does changing the handle type.

And even if I do manage to make this curve linear, how on earth can this be applied to specific pose bones… Because from what I gather you only operate on fcurves at the object level :pensive:

Sincerely appreciate any help on this!

okay by reordering my code like so, so that the interpolation is set immediately after the key-frame, fixes the linear issue

import bpy

obj = bpy.context.object
obj.animation_data_create()
obj.animation_data.action = bpy.data.actions.new(name="MyAction")

fcurve = obj.animation_data.action.fcurves.new(data_path="location")

keyframe_one = fcurve.keyframe_points.insert(frame=0.0, value=5.0)
keyframe_one.interpolation = "LINEAR"

keyframe_two = fcurve.keyframe_points.insert(frame=10.0, value=15.0)

keyframe_two.interpolation = "LINEAR"

can’t say I fully understand why but it works.

Now to try and get this to work on pose bones… :confused:

I’m trying to do something similar. I don’t understand how you’re keyframing location here; you’re only passing a single float to fcurve.keyframe_points.insert(). Is that the X coord? How to set Y and Z? And what about rotation? The doc is hard to figure out on this.