2
$\begingroup$

I am new to both Python and Blender. I am trying to add or change a keyframe on a specific geometry node within Blender. I found a code snippet that demonstrates how to keyframe a general property within Geometry Nodes, but I am unsure how to apply this to a specific node, such as a Value node.

Here’s the code snippet I found:

bpy.data.objects["Cube"].modifiers["GeometryNodes"]["Input_2"] = 2
bpy.data.objects["Cube"].modifiers["GeometryNodes"].keyframe_insert(data_path='["Input_2"]', frame=1)

However, I need to keyframe a specific node like the Value node for "Sound to Sample" or what used to be referred to as "bake to f-curve". I'm struggling to understand how to target this specific geometry node and apply a keyframe to it.

$\endgroup$

1 Answer 1

5
$\begingroup$

this is the only way I know how to do this programmatically. from my understanding, keyframes for individual nodes are handled at the node tree level.

first, you have to find your node tree:

bpy.data.node_groups contains them all, find which element of this list is the node tree in question, for example:

node_tree = bpy.data.node_groups['GeometryNodes']

Then, there is something tricky about this step. You can access the keyframe data using:

node_tree.animation_data

BUT, it will be None unless there are existing keyframes. Easy way around this is to just quickly manually add a keyframe to your node (there has to be a way to do this step in Python). Then, when you call node_tree.animation_data it will not be None.

Then, you can view the keyframe fcurves like so:

node_tree.animation_data.action.fcurves

this is also a list, there must be a better way of doing this, but you can iterate through each one and look at it's data_path to determine if it belong to the node in question:

node_tree.animation_data.action.fcurves[0].data_path

you can then edit your dummy keyframe value like this:

node_tree.animation_data.action.fcurves[0].keyframe_points[0].co = (frame, value)

you can also add new keyframe_points and edit them using the same approach, but first calling

node_tree.animation_data.action.fcurves[0].keyframe_points.add(npoints) to add them first.

doing this approach, you can add and edit as many keyframes as you want and they will be reflected in the node.

Hope it helps.

$\endgroup$
3
  • $\begingroup$ I added a keyframe to the value and used 'sound to sample' on it, but the f-curves node_tree.animation_data.action.fcurves[0] are empty. Is there something else I need to do? $\endgroup$ Commented Apr 25 at 11:29
  • $\begingroup$ @DerekEden answer is very useful and correct for this discussion. I'd like to add that while testing the solution, I discovered that if you have only one keyframe, it works perfectly when using bpy.ops.graph.sound_to_samples(filepath=mp3Path) for single instances. However, this method isn't suitable for multiple instances. $\endgroup$ Commented Apr 25 at 13:05
  • $\begingroup$ @DenizBöttcher to be honest, I'm not familiar with sound to sample, but I can take a look later and see if I'm able to get it working - will report back if I make progress $\endgroup$
    – Derek Eden
    Commented Apr 25 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .