Print out current shortcuts

I had an idea and I was wondering if it would fly.
A simple addon script that prints out all the current shortcuts.
This should be pretty easy to do and it would allow people to maintain a current list.
Has this been done already??
Thanks!
Greg

You can view the shortcuts under user preferences, right? There is an export key configuration button as well.

That is correct but that just writes out the python to change any you have different to the default

I was thinking of using getattr to get the current ones and save that to a text file/csv /image etc.

A vanilla blender just gives this.


import bpy
import os

def kmi_props_setattr(kmi_props, attr, value):
    try:
        setattr(kmi_props, attr, value)
    except AttributeError:
        print("Warning: property '%s' not found in keymap item '%s'" %
              (attr, kmi_props.__class__.__name__))
    except Exception as e:
        print("Warning: %r" % e)

wm = bpy.context.window_manager
kc = wm.keyconfigs.new(os.path.splitext(os.path.basename(__file__))[0])


Something like this but actually for all keys and contexts with their relevant properties



import bpy
local_window_manager = bpy.context.window_manager
for local_key_map in local_window_manager.keyconfigs.user.keymaps:
    print(local_key_map.name)
    for local_key_map_item in local_key_map.keymap_items:
        hotkey = []
        if local_key_map_item.ctrl: hotkey.append("Ctrl")
        if local_key_map_item.shift: hotkey.append("Shift")
        if local_key_map_item.alt: hotkey.append("Alt")
        hotkey.append("%s (%s)" % (local_key_map_item.type, local_key_map_item.value))
        print("%s    %s"  % (local_key_map.name, " + ".join(hotkey)))

 

OK I got this far(see below).

My current problem is simply how to get the tool tip info (or the description) of each command.
Any ideas??
Thanks

save a blend file (the file will be written into the .blend directory)
then paste the code in the console and run it.
outputs html which will cut and paste into a spreadsheet or other doc for your own formating

OK I’ve cleaned up the code a bit
It now prints tables per context and names and keys.

However it would be good to get the tooltips when they are set.
Any ideas would be greatly appreciated



import bpy
import os
import string

filename = os.path.join(os.path.dirname(bpy.data.filepath), "key_codes.html")
header = "<html><head></head><body><table>
"
try:
    outfile = open(filename, 'w')
    outfile.write(header)
    local_window_manager = bpy.context.window_manager
    for local_key_map in local_window_manager.keyconfigs.user.keymaps:
        outfile.write("</table><h3><font color=\"green\">%s</font></h3><table border=1>
" % local_key_map.name)
        for local_key_map_item in local_key_map.keymap_items:
            hotkey = []            
            if local_key_map_item.oskey: hotkey.append("OS Key")
            if local_key_map_item.shift: hotkey.append("Shift")
            if local_key_map_item.ctrl: hotkey.append("Ctrl")
            if local_key_map_item.alt: hotkey.append("Alt")
            hotkey.append("%s</font></td><td>%s" % ( string.capwords(local_key_map_item.type,"_"), string.capwords(local_key_map_item.value)))
            outfile.write("<tr><td><b>%s</b></td><td><font color=\"blue\">%s</td></tr>
"  % (local_key_map_item.name," + ".join(hotkey)))
    outfile.write("</table></body></html>
")
    outfile.close()
    print("File written as %s" % filename) 
except Exception as e:
    print("Warning writing file %s caused this error: %r" % (filename,e))
else:
    if outfile: outfile.close()



Ok here’s a pretty version.

It still needs more info like tooltips and context - any ideas ??

Please try it and let me know.

Instructions (shortcut these as you like)

Start a new blend file and save it somewhere

Open the console window (the one with the screen icon)

copy the code below into the console and press enter a couple of times to ensure that the code executes

error message or file name should be returned at the end

copy and paste the filename into your browser

in the browser copy the tables your interested in and paste them straight into a spread sheat or document to make them how you like them.



import bpy
import os
import string

filename = os.path.join(os.path.dirname(bpy.data.filepath), "key_codes.html")
header = "<html><head></head><body><table>
"
try:
    outfile = open(filename, 'w')
    outfile.write(header)
    wm = bpy.context.window_manager
    for local_key_map in wm.keyconfigs.user.keymaps:
        outfile.write("""</table><h3 style=\"color: #FF0000;\">%s</h3><table border=1>
        <thead><tr>
          <th>Name</th>
          <th style=\"color: #0000FF;\">Keys</th>
          <th>Action</th>
          <th>Description</th>
        </tr></thead>
""" % local_key_map.name)
        for key_map_item in local_key_map.keymap_items:
            hotkey = []            
            if key_map_item.shift: hotkey.append("Shift")
            if key_map_item.oskey: hotkey.append("OS Key")
            if key_map_item.ctrl: hotkey.append("Ctrl")
            if key_map_item.alt: hotkey.append("Alt")
            hotkey.append("%s</td><td>%s" % ( string.capwords(key_map_item.type.replace("MOUSE","_MOUSE").replace("TRACKPAD","TRACKPAD_"),"_").replace("_"," ").replace("Ndof","NDOF "), string.capwords(key_map_item.value)))
            idname = key_map_item.idname
            description = ""
            if idname:
                mod, opname = idname.split(".")
                idname_c = "%s_OT_%s" % (mod.upper(), opname)
                idtype = getattr(bpy.types, idname_c)
                description = idtype.bl_rna.description
            outfile.write("<tr><td><b>%s</b></td><td style=\"color: #0000FF;\">%s</td><td>%s</td></tr>
"  % (key_map_item.name," + ".join(hotkey),description))
    outfile.write("</table></body></html>
")
    outfile.close()
    print("File written as %s" % filename) 
except Exception as e:
    print("Warning writing file %s caused this error: %r" % (filename,e))
else:
    if outfile: outfile.close()


#   -----8<-----------

1 Like

Here’s an example regarding the tooltips:

idname = "view3d.viewnumpad"

mod, opname = idname.split(".")
idname_c = "%s_OT_%s" % (mod.upper(), opname)
idtype = getattr(bpy.types, idname_c)

print(idtype.bl_rna.description)

# For this operator, you would get the text of the enum entry 'BACK' like:
print(idtype.bl_rna.properties['type'].enum_items['BACK'].description)

Thanks CoDEmanX
I have been hoping you would check this out.
You definitely are the Code MAN X
What would we do without you.

Thanks again again

I will finish this and stick it in the released scripts.
I’m not sure where in the UI to put this though it would only need to be run once or twice.

It should probably have a save file popup.

BTW are there any other things that would be useful to add in the output tables

I have updated post #4 this now works with basic descriptions.
If anyone is interested let me know and I will improve it.

I will one day add the extra details available by the second part of CoDEmanX’s code but for now I’d rather be blending than coding.
This will do me for now.