# Baking Sound to F-Curve with Python on Pre-Existing Objects

Ok, so I've been loosely following this tutorial:

However, my model is very different and because I'm not making a simple stretchy rectangle, I modeled the mesh I want to animate, (It's still only being animated by changing the Z scale), beforehand. It's actually a series of identical meshes, but each will represent a different set of frequencies from the sound, very similar to the tutorial above. The meshes are names as follows: "Fence.000", "Fence.001", "Fence.002", ... "Fence.014".

I'd like to point out that I have programming experience in Java, PHP, HTML, and JavaScript, however this is my first time with Python. Here's my script:

import bpy;

count = 15;
lo = 0;
hi = 15000;
step = (hi - lo) / count;

for i in range(0, count):
name = "Fence.0" + ("0" if (i < 10) else "") + str(i);
bpy.context.scene.objects.active = bpy.data.objects[name];

bpy.context.active_object.animation_data.action.fcurves[0].lock = True;
bpy.context.active_object.animation_data.action.fcurves[1].lock = True;

bpy.context.area.type = "GRAPH_EDITOR";
bpy.ops.graph.sound_bake(filepath="/Users/Andrew/Desktop/Trap.mp3", low=(step*i), high=(step*(i+1)));

bpy.context.active_object.animation_data.action.fcurves[2].lock = True;

bpy.context.area.type = "TEXT_EDITOR";


What I'm struggling with is getting an object that exists already and setting it up in a way that the line

    bpy.ops.anim.keyframe_insert_menu(type = "Scaling");


can access it. After googling and lots of copy and paste, I've ended up with the above. However, it still complains about an error, and the error message coming out is:

Traceback (most recent call last):
File "/Users/Andrew/Graphic Design/dancing fence.blend/Text", line 15, in <module>
File "/Applications/Blender/Blender.app/Contents/MacOS/2.71/scripts/modules/bpy/ops.py", line 188, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: No suitable context info for active keying set

Error: Python script fail, look in the console for now...


I know to anyone experienced the above code probably looks like it was written by a 5 year old, but I would love it if someone could explain to me how blender works with python, (The documentation I've been able to find has all been worse than terrible.), and maybe even explain to me how my own code works. (Eek, that's never a good sign. :( )

Thanks.

What I'm struggling with is getting an object that exists already...

You get the objects from bpy.data.objects:

obj = bpy.data.objects[obj_name] #where obj_name is the key / object's name

#in this case
for i in range(count):
obj = bpy.data.objects['Fence.%04d' % i] #formatted string

#set the keyframe
obj.keyframe_insert(data_path="scale", frame=1)