Check if frame has a key?

How can I check if a frame has a key with python?
I’m making a custom floating panel when running a modal operation which shows the current frame, but need to change text color(yellow) whenever the object has a keyframe on the active frame.

Is it possible?

Okay I found this code by Codeman, but this is for bones. What I am trying to do is switch to yellow only if the active frame has a keyframe.

ob = bpy.context.active_object
    
for fcu in ob.animation_data.action.fcurves:
    #if fcu.data_path.startswith('pose.bones.["%s"]' % BONE_NAME):
    for pt in fcu.keyframe_points:
        frame = str(bpy.context.scene.frame_current)
        if pt.co[0] == frame:
            
            
            bgl.glColor4f(1, 1, 0, 1)
           
            
        else:
            
            bgl.glColor4f(0.8, 0.8, 0.8, 1.0)


Solved it myself.
Here is the solution if anyone finds it useful:
basically don’t convert current frame to string

ob = bpy.context.active_object
    
for fcu in ob.animation_data.action.fcurves:
    #if fcu.data_path.startswith('pose.bones.["%s"]' % BONE_NAME):
    for pt in fcu.keyframe_points:
        frame = bpy.context.scene.frame_current #FIXED
        if pt.co[0] == frame:
            
            
            bgl.glColor4f(1, 1, 0, 1)
            #redraw 
            
        else:
            
            bgl.glColor4f(0.8, 0.8, 0.8, 1.0)


4 Likes