Adding animation data to an object

I’m working on a script that adds animation keys to objects (read form an external file).
Before animation keys can be created, it’s necessary to create animation data - actions and F-curves.
I found some code online that does that, and also managed to generate similar code using ChatGPT.
Both methods seem to work and generate f-curves that Blender can use to add keys to.
However, when comparing the objects I created animation data for using this code to objects that I added animation to using the UI, there is a difference.
In the objects for which Blender itself created the animation data, the nine f-curves for location, rotation and scale are nested inside a channel called “object transforms”. in the ones created with code, they are nested directly under the action.
Is this extra nesting important? and even if not, just to be safe, how do I create it using code?

Action groups are logical grouping of fcurves. Not having one isn’t an issue - it only makes the management of channels and keyframes less convenient.

This creates an Object Transforms group if it doesn’t exist and dumps all fcurves into it

action = obj.animation_data.action
group = action.groups.get("Object Transforms")
if not group:
    group = action.groups.new("Object Transforms")

for fc in action.fcurves:
    fc.group = group

Note that an fcurve can be assigned to at most 1 group and an empty group doesn’t show up in the ui until it has at least one fcurve.

1 Like