0
$\begingroup$

I'm trying to automate calibration of mocap data, matching it to rig bone locations.

As part of this project, I need to use Python to scan bone animation f-curves for minimum and maximum values. I know I can reference object f-curves with e.g.:

mt_curve = mt_obj.animation_data.action.fcurves[0]

But I can't find how to reference the f-curves of individual bones.

I found this method to get the index of a bone:

 rig_obj.pose.bones.find("my_bone")

In the console, this code returned 221, so I tried:

bone_curve = rig_obj.animation_data.action.fcurves[221]

... but this gave the error:

IndexError: bpy_prop_collection[index]: index 221 out of range, size 21

So I guess that 'animation_data.action.fcurves' must only refer to the rig object itself - not the bones.

I also tried:

bpy.data.objects["my_rig"].pose.bones["my_bone"].fcurves[1]

... and similar, but get the error:

'PoseBone' object has no attribute 'fcurves'

So can anyone tell me a way to reference a bone's f-curves in the same way as I can reference an object's f-curves?

As always, any hints, ideas or vague suggestions are more than welcome, as is, of course, the actual answer ;-)

$\endgroup$

1 Answer 1

2
$\begingroup$

An FCurve that animates eg. a bone's Y Location, ie. that targets arma_ob.pose.bones["Bone Name"].location[1], will have

>>> fcurve.data_path
'pose.bones["Bone Name"].location'
>>> fcurve.array_index
1

You can find all FCurve's that affect a bone named bone_name with

path = f'pose.bones["{bpy.utils.escape_identifier(bone_name)}"]'
fcurves = armature.animation_data.action.fcurves
bone_fcurves = [fcu for fcu in fcurves if fcu.data_path.startswith(path)]
$\endgroup$
5
  • $\begingroup$ Thanks for the answer @scurest. There is a missing ) in the line path = f'pose.bones["{bpy.utils.escape_identifier(bone_name}"]' $\endgroup$ Nov 21, 2022 at 8:08
  • $\begingroup$ BTW, the second section is exactly what I needed so thanks again. I can use that code to reference the various curves; e.g. bone_fucurves[1] for the X location f-curve. $\endgroup$ Nov 21, 2022 at 8:17
  • $\begingroup$ I've corrected that line now. $\endgroup$ Nov 21, 2022 at 8:22
  • $\begingroup$ Thanks. Note that you can't depend on any particular order, so if you want the X Location curve, you have to search for the FCurve with the right data_path and array_index. $\endgroup$
    – scurest
    Nov 21, 2022 at 14:51
  • $\begingroup$ Good to know. I was assuming that the index order followed the appearance of the transforms in the GUI - LOCX = 0, LOCY = 1 etc. $\endgroup$ Nov 21, 2022 at 15:54

You must log in to answer this question.

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