# How to create and assign a group to an fcurve with python script?

I am attempting to make an addon/script with blender python that works with mocap files (.bvh), my problem is that when the files are imported the group collection on fcurve.group is blank

for fcurve in bpy.context.active_object.animation_data.action.fcurves:
if fcurve.group is not None:
print(text='Source object fcurve group ' + fcurve.group.name)


How do I create and assign a group to the fcurve with python script?

fcurve.group = bpy.types.FCurve.new("LocRot") #what i tried first


Explanation of why: When i import the files they have rotation mode XZY Euler on all the bones, I am attempting to use this script to convert the rotation mode to Quaternion however it fails due to the fact that the bones have no groups on them.... (hence my question)

Ultimately I plan to make my own pipeline which handles the small problems I have with the .bvh files when imported, such as converting FK -> IK

My full code can be found here and here if that helps at all

• Why is this a problem? What do you do with the BVH files that Blender's importer doesn't do already? – dr. Sybren Jan 21 '18 at 22:43
• dr Sybren: all i am trying to do is assign a new group to an fcurve, do you know how to do it? fcurve.group = bpy.types.FCurve.new("LocRot") looked promising however i don't think i used it right cause it didn't work here – Alpha Jan 21 '18 at 23:02

Both the fcurves themselves, and the action groups are part of the action object. You have to create new groups through the action and then assign that group to any fcurves you want inside it.

armature = bpy.context.scene.objects.active
anim_data = armature.animation_data
action = anim_data.action

# create a new group
group_1 = action.groups.new("group_1")

# assign the first three fcurves to the new group
for fcurve in anim_data.action.fcurves[:3]:
fcurve.group = group