Bpy.context.selected_editable_keyframes is of type "None"

I wanted to get the keyframes selected in the graph editor, but I failed: bpy.context.selected_editable_keyframes is of type None, and, accordingly, I can’t do anything with it, while I had selected keyframes. I do not know if this is a bug, or if I am doing it wrong, and selected_editable_keyframes is not designed for this. there is also a video demonstration

you can’t access selected keyframes via the python console. keep in mind that context changes all the time and in ways you may not expect. context is only reliable for that specific moment in time when it is accessed. the python console doesn’t use keyframes so the python console’s context doesn’t have that data. If you access context.selected_editable_keyfram
s from a timeline, you’ll have keyframe data.

for example, here’s the Simple Operator template- run this operator from the timeline:

import bpy


def main(context):
    print(context.selected_editable_keyframes)


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_operator()

results:

[bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe, bpy.data.actions[‘Cube.001Action’]…Keyframe]

@testure , thanks for the answer! I knew about the context change, but I didn’t think that different editors have different contexts (I thought that the context is one and global for the whole blender)

well, it’s not so much editors having different contexts as it is the context itself is changing. You can usually get away with using the python console to test some things out, but ultimately where context accuracy is concerned it’s best to test things out in the context you intend them to be used in. There are a lot of weird ‘gotchas’ with context like this- for example if you’re in an Outliner, context will have an attribute called selected_ids, but if you try to access this attribute anywhere else you’ll get an AttributeError exception.