How to get the tooltip information of whatever UI element the mouse is over when I run my operator from a shortcut?

I’m writing an addon which needs to ascertain the function of the UI element the mouse is over when one of the addon’s operators is executed.

For general information I can use:

print('ui area is', bpy.context.area.ui_type)
        print('space type is', context.space_data.type)
        print('workspace is',context.window.screen.name)
        try:

            spaceproperties = context.space_data.context
            print('properties tab is',spaceproperties)
        
        except:
            print('no space data context')
        #print('mode is',context.workspace.object_mode) not sure what this was
        print('mode is',context.mode)

For some operators and properties I can use:



        #try to copy the button data path, this will work if it's a property
        try:
            bpy.ops.ui.copy_data_path_button(full_path=False)
            print('copy data path:',bpy.context.window_manager.clipboard)

        except Exception as e:
            print('could not copy the button data path\n',e,'\n')
            

        
            #try to copy the python command
        try:
            bpy.ops.ui.copy_python_command_button()
            print('copy python command button:',bpy.context.window_manager.clipboard)
        except Exception as e:
            print('could not get python command to clipboard\n',e, '\n')
            
                
        

But the real issue is that a lot of elements don’t work with the copy commands, for example if it’s a UI element which only has a description, or a menu item.

In other scenarios running the copy commands from the addon will return an incorrect context error message, whereas using ctrl C directly will successfully copy the command. For example pressing ctrl C whilst hovered over the 3d viewports object menu will return:

bpy.ops.wm.call_menu(name="VIEW3D_MT_object")

but running the copy command from the addon will return:

could not copy the button data path
 Operator bpy.ops.ui.copy_data_path_button.poll() failed, context is incorrect 

could not get python command to clipboard
 Operator bpy.ops.ui.copy_python_command_button.poll() failed, context is incorrect 

So what I think I need to do is somehow gather all of the tooltip information from the item under the mouse when my operator is executed (description, shortcut, and python command).

Does anyone know how to do this, or perhaps have an alternative approach for finding the function of the UI element the mouse is over when the operator is executed?

I did start brushing up my C++ skills which have been out of service for 20 odd years (and weren’t that great to begin with), so that I could try and create an API function to return the info I need, but it was taking too much time to try and figure out what exactly was going on under the hood. Perhaps if anyone could help with that?

1 Like

Here’s my full operator (it’s being called from a shortcut with the space set to empty and the name set to window.

class WEB_OT_test1(bpy.types.Operator):
    bl_idname = "web.test1"
    bl_label = "test 1"
    

   
    
    def execute(self, context):
       
        os.system("cls")
        print('\n\n\nstart\n\n\n')

       
        success = True

        #try to copy the button data path, this will work if it's a property
        try:
            bpy.ops.ui.copy_data_path_button(full_path=False)
            print('copy data path:',bpy.context.window_manager.clipboard)

        except Exception as e:
            print('could not copy the button data path\n',e,'\n')
            

        
            #try to copy the python command
        try:
            bpy.ops.ui.copy_python_command_button()
            print('copy python command button:',bpy.context.window_manager.clipboard)
        except Exception as e:
            print('could not get python command to clipboard\n',e, '\n')
            success = False
                
        
        if success:
            print('clipboard contains',bpy.context.window_manager.clipboard)

       
        
        print('ui area is', bpy.context.area.ui_type)
        print('space type is', context.space_data.type)
        print('workspace is',context.window.screen.name)
        try:

            spaceproperties = context.space_data.context
            print('properties tab is',spaceproperties)
        
        except:
            print('no space data context')
        
        print('mode is',context.mode)

        
        return {'FINISHED'}

here’s the code which is making the keymap entry and setting the permissible context:

#a list of dictinaries.  Each dictionary contains the necessary information to create a keymap
keymaps_to_register = (
    {'operator':'web.test1','name':'Window', 'space_type' : 'EMPTY', 'prop_map' : '', 'key' :'F1', 'action' : "PRESS", 'ctrl' : False, 'shift' : False, 'alt' : True},
   )
keys = []


def create_keys(maps,kc):
    '''
    registers a new keymap\n
    pass in a dictionary of values and a keyconfig'''
    
    km = kc.keymaps.new(name = maps['name'], space_type = maps['space_type'])
    kmi = km.keymap_items.new(maps['operator'], maps['key'], maps['action'], ctrl= maps['ctrl'], shift=maps['shift'],alt = maps['alt'])
    if maps['prop_map'] != "":
        kmi.properties.name = maps['prop_map']
    
    keys.append((km, kmi))
    
def register_keymap():
    
    wm = bpy.context.window_manager
    addon_keyconfig = wm.keyconfigs.addon
    kc = addon_keyconfig

    for maps in keymaps_to_register:
        create_keys(maps,kc)

def unregister_keymap():

    for km, kmi in keys:
        print(km,kmi)
        km.keymap_items.remove(kmi)

    keys.clear()