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 = km.keymmap_items.new('wm.call_menu', 'N', 'PRESS', ctrl=True)
 kmi = km.keymmap_items.new('wm.call_menu', 'O', 'PRESS', ctrl=True, shift=True)
 kmi = km.keymmap_items.new('wm.open_mainfile', 'O', 'PRESS', ctrl=True)
 kmi = km.keymmap_items.new('wm.save_mainfile', 'S', 'PRESS', ctrl=True)
 kmi = km.keymmap_items.new('wm.save_as_mainfile', 'S', 'PRESS', ctrl=True, shift=True)
 kmi = km.keymmap_items.new('wm.quit_blender', 'Q', 'PRESS', ctrl=True)
 kmi = km.keymmap_items.new('wm.call_menu', 'Q', 'PRESS')
 kmi = km.keymmap_items.new('screen.space_type_set_or_cycle', 'F1', 'PRESS', shift=True)
 kmi = km.keymmap_items.new('screen.space_type_set_or_cycle', 'F2', 'PRESS', shift=True)
 kmi = km.keymmap_items.new('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.

QUESTION:

How can modify my script so it also outputs the attributes of the keymaps that have any, along with their respective values, such as 'view3d.select' 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

NameForFile=open("C:\\Users\\Alpha\\Desktop\\keymap_converts.txt","w")

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

            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 = km.keymmap_items.new(" + 
                            "'" + kmi.idname + "', " + 
                            "'" + kmi.type + "', " + 
                            "'" + kmi.value + "'" + 
                            anyModifiers +")\n")

searchForKeymaps()

#Save and close out the file
NameForFile.close()

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:
        print(repr(kmi.idname))
        for p in kmi.properties.bl_rna.properties:
            if p.identifier != 'rna_type':
                if p.type == 'COLLECTION' or p.subtype in {'XYZ'}:
                    val = list(getattr(kmi.properties, p.identifier))
                else:
                    val = getattr(kmi.properties, p.identifier)

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

Got it, thanks!