# Adding keyframes to array modifer

I want to write a script that changes the array modifier, and then inserts a keyframe for the new value.

I got it working, but it only does it on the first object. Unselecting all the objects, and then only the one I want to set it to also doesn't work.

Googling some more I found I should use obj.keyframe_insert, but what do I set the data_path to?

Here is an example of what I want to do

obj.modifiers['Array'].count = random.randint(1, 5)
obj.keyframe_insert(data_path=???)


I tried this first. bpy.ops.anim.keyframe_insert_menu(type='Available')
That works, but only sets it for the first object, the one that was selected; and it was iffy to start off with. I had to manually first set a keyframe, else it said 'Available' was not found.

When using keyframe_insert() you want to use it on the object that contains the value you wish to keyframe.

By "object" I am referring to python objects (as in "object" oriented programming) that are defined in a python class, not the visible 3d objects. In your example the object that has the value to be keyed is the modifier.

So to keyframe the count value in the array modifier you want to use -

obj.modifiers['Array'].keyframe_insert('count')


You might also think of it as the class containing the property to be keyframed is the one that gets told to insert the keyframe.

As mentioned by pink vertex you can also go through the parent by using

obj.keyframe_insert('modifiers["Array"].count')


Note the quotes " must be inside ' - swapping them around doesn't work.

• Excellent. Thanks. And also, thanks for the tip on the quotes. Works Perfectly – Xuton Jul 24 '14 at 8:55