Selected_visible_fcurves from the 3D Viewport?

Hi! I’m trying to get the selected visible keyframes in a Graph Editor from a 3D Viewport. If I try context.selected_visible_fcurves in the 3D Viewport, it returns None, but in the Graph Editor it works.

I’m currently doing this:

previous_area_type = context.area.type
context.area.type = 'GRAPH_EDITOR'
fcurves = context.selected_visible_fcurves
context.area.type = previous_area_type

Is there a better way?

Thank you!

Chris on Blender Stack Exchange kindly showed me how to switch context for this:

for window in context.window_manager.windows:
    screen = window.screen
    for area in screen.areas:
        print(area.type)
        if area.type == 'GRAPH_EDITOR':
            with context.temp_override(window=window, area=area):
                fcurves = context.selected_visible_fcurves
                print (context.selected_visible_fcurves)
            break