Keyframe a modifier value with python

I am trying to keyframe the strength value in the displace modifier using python. I want to keyframe the strength value so that I can bake sound to f-curves and recreate my music tutorial with an addon. So how to I keyframe values in modifiers using python in Blender?

Let’s suppose the object is selected and the modifier already set. I wanna create 2 keyframes with 2 different values on the strength value of the displace modifier.

#Set the first keyframe
bpy.context.object.modifiers['Displace'].strength = 0
bpy.context.object.modifiers['Displace'].keyframe_insert("strength",frame=1)

#Set the second one
bpy.context.object.modifiers['Displace'].strength = 10
bpy.context.object.modifiers['Displace'].keyframe_insert("strength",frame=42)

In a general case, you should have the “keyframe_insert” method available basically anywhere (not necessary on modifiers) where you can add a keyframe on the variable selected on it.

Thanks! Works great. :slight_smile: