How to get any Attributes and their Values from existing keymaps?

I was making a script that grabs the current keymaps and outputs them as a textfile.

I’m aware you can just export a keymap preset from the preferences and I’m also aware that you don’t need every pre-existing keymap just to add some of your own. This is me just having some fun.

CURRENT RESULT AFTER RUNNING SCRIPT (only part of it because it’s longer than I thought):

 kmi ='wm.call_menu', 'N', 'PRESS', ctrl=True)
 kmi ='wm.call_menu', 'O', 'PRESS', ctrl=True, shift=True)
 kmi ='wm.open_mainfile', 'O', 'PRESS', ctrl=True)
 kmi ='wm.save_mainfile', 'S', 'PRESS', ctrl=True)
 kmi ='wm.save_as_mainfile', 'S', 'PRESS', ctrl=True, shift=True)
 kmi ='wm.quit_blender', 'Q', 'PRESS', ctrl=True)
 kmi ='wm.call_menu', 'Q', 'PRESS')
 kmi ='screen.space_type_set_or_cycle', 'F1', 'PRESS', shift=True)
 kmi ='screen.space_type_set_or_cycle', 'F2', 'PRESS', shift=True)
 kmi ='screen.space_type_set_or_cycle', 'F3', 'PRESS', shift=True)

It seems to be working so far but I’m missing the attributes/properties from ceratin Keymaps.


How can modify my script so it also outputs the attributes of the keymaps that have any, along with their respective values, such as '' which has attributes like 'extend' which can be set to True or False in different keymaps.

Same situation with the keymaps that call menus like 'wm.call_menu_pie' which has the attribute 'name' and the name of the pie menu it’s calling.

This is my current script:

import bpy
import os


wm = bpy.context.window_manager
def searchForKeymaps():
    kc = wm.keyconfigs["blender"]
    for km in kc.keymaps:
        for kmi in km.keymap_items:

            anyModifiers = ""
            if kmi.ctrl:
                anyModifiers += ", ctrl=" + str(kmi.ctrl)
            if kmi.alt:
                anyModifiers += ", alt=" + str(kmi.alt)
            if kmi.shift:
                anyModifiers += ", shift=" + str(kmi.shift)

            NameForFile.write("kmi =" + 
                            "'" + kmi.idname + "', " + 
                            "'" + kmi.type + "', " + 
                            "'" + kmi.value + "'" + 
                            anyModifiers +")\n")


#Save and close out the file

You can get the blueprint of most blender objects by checking its bl_rna. The below example prints the default keymap items from 3D View and its properties to the console.

Note that if you’re storing the text as a python file, the value should be reproduced so that python can evaluate them when the file is read. Also note that some values are non-coerced blender objects, (<bpy_boolean>, <bpy_float>, <bpy_prop_array> etc.) which need to be converted either to a tuple or a list.

import bpy

wm = bpy.context.window_manager
kc = wm.keyconfigs.default
km = kc.keymaps['3D View']

for kmi in km.keymap_items:

    if kmi.idname:
        for p in
            if p.identifier != 'rna_type':
                if p.type == 'COLLECTION' or p.subtype in {'XYZ'}:
                    val = list(getattr(, p.identifier))
                    val = getattr(, p.identifier)

                print(f"    {repr(p.identifier)}: {repr(val)}")

Got it, thanks!