4
$\begingroup$

I am getting the Graph Editor's visible FCurves using the following code:

visible = context.visible_fcurves[:]
for fcurve in visible:
    #object code here

How do I get the object from this FCurve? In this example, Cube: Channels

Thank you!

$\endgroup$

2 Answers 2

5
$\begingroup$

Marty has beat me to it, but the way I found is:

found = [o for o in D.objects if o.animation_data and o.animation_data.action is fcurve.id_data]

where fcurve is the iterator variable in your loop, and found is a list of all objects sharing the animation data using this fcurve. Assuming there's exactly one, you could instead do:

found = next(o for o in D.objects if o.animation_data and o.animation_data.action is fcurve.id_data)

And if the length of the oneliner is bothering you, walrus helps a bit:

found = next(o for o in D.objects if (d:=o.animation_data) and d.action is fcurve.id_data)
$\endgroup$
3
  • 1
    $\begingroup$ I like this, as I didn't think to use the id_data to avoid looking through fcurves, but I have seen cases where o has animation_data but for corner case reasons there is no action. (For those playing along at home, D is set in the Python console, but not scripts. It's equal to bpy.data in my script above. $\endgroup$ Jan 17 at 1:14
  • 2
    $\begingroup$ @MartyFouts then it will compare None and fcurve.id_data identities and omit the object from the results. $\endgroup$ Jan 17 at 1:15
  • 1
    $\begingroup$ Doh. I knew that. (Apparently not ;) $\endgroup$ Jan 17 at 1:16
3
$\begingroup$

The only way I know how to do this is to search objects to see if the fcurves are present:

for object in bpy.data.objects:
    if object.animation_data and object.animation_data.action and object.animation_data.action.fcurves:
        for fcurve in object.animation_data.action.fcurves:
            if fcurve in visible:
                print(f"{object.name}")
  • You can replace the for... with other ways of going through objects. You might be able to use context.active_object or limit the search to only selected objects, et cetera.
  • The first clause of the if statement separates objects that have animation data from those that don't.
  • Even if an object has animation data the second clause is for obscure cases when the animation data doesn't have any actions.
  • The third clause in the if statement is probably overkill, but I can't find anything that guarantees that if an object has an action that action will have fcurves.

This will only work for animation data directly associated with objects; which appears to be what you want.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.