How to make an add-on script that can be launched with a keyboard shortcut?

I’ve written a script based on the io_import_images_as_planes.py add-on. I would like to assign a keyboard shortcut to this script so that I don’t have to go to File -> Import -> (my script name) every time I want to run the script.

How do I do this?

It’s covered here:
http://www.blender.org/documentation/blender_python_api_2_71_release/info_tutorial_addon.html#keymap

Thanks! That gets me part of the way there, but I’m still having difficulty. Specifically, my code differs from the example there in the sense that my class has two parameters presumably sent over from Blender via the File -> Import menu. I can press Ctrl+Shift+Space, but nothing happens. (I was able to get the example to run.)

Here’s my script with partially incorporating the keyboard shortcut code from that example: http://simplecarnival.com/blender/io_import_imageseq_as_plane.py

Any suggestions?

you just call it on the key press
then fill your parameters from the bpy.context which has all the information you should need
the keyboard is only that it knows nothing
try this


import bpy

class ShowInfo(bpy.types.Operator):
    """Click to see description."""
    bl_idname = "in.fo"
    bl_label = "Report Information"
    info = bpy.props.StringProperty()
    #
    def execute(self, context):
        self.info = "Region = {:} Space = {:}".format(context.region.type, context.space_data.type)
        self.report({'INFO'}, "{:}".format(self.info))
        return {'FINISHED'}
    #
    def invoke(self, context, event):
        return self.execute(context)


addon_keymaps = []

def register():
    # Handle the key mapping
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='Window', space_type='EMPTY' ,region_type='WINDOW')
    kmi = km.keymap_items.new(ShowInfo.bl_idname, 'F1', 'PRESS', oskey=True)
    addon_keymaps.append(km)
    bpy.utils.register_module(__name__)

def unregister():
    # Handle the key mapping
    wm = bpy.context.window_manager
    for km in addon_keymaps:
        wm.keyconfigs.addon.keymaps.remove(km)
    addon_keymaps.clear()
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()


In your case if you already know the filepath just hardcode that into the class.
If you want to select the file with a dialog then you will need to
define a filepath attribute in your class then add
context.window_manager.fileselect_add(self)
in the invoke.
There are a few gotchas in the filepath stuff but

That should get you on your way.

Thanks, Yardie!