I'm trying to get keymaps working in my script, but it's not working. I want grid size of the current view3D to double if I press Ctrl-LeftArrow and halve if I press Ctrl-RightArrow. Right now I'm just trying to print debug messages in response to keypresses.
Below is my code, but my operator is not being called no matter what key map items I add. Am I doing this correctly?
import bpy
class GridRescale(bpy.types.Operator):
"""Change grid scale"""
bl_idname = "kitfox.grid_rescale"
bl_label = "Grid Rescale"
bl_options = {'REGISTER'}
def invoke(self, context, event):
print("evt type %s" % (event.type))
def execute(self, context):
scale = bpy.context.space_data.overlay.grid_scale
print("execute")
self.report({'INFO'}, "Grid scale %f" % (scale))
return {'FINISHED'}
# store keymaps here to access after registration
addon_keymaps = []
def register():
bpy.utils.register_class(GridRescale)
# handle the keymap
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
# kmi = km.keymap_items.new(GridRescale.bl_idname, 'NUMPAD_0', 'PRESS', ctrl=False, shift=False)
kmi = km.keymap_items.new(GridRescale.bl_idname, 'LEFT_ARROW', 'PRESS', ctrl=True, shift=False)
addon_keymaps.append(km)
# kmi = km.keymap_items.new(GridRescale.bl_idname, 'NUMPAD_0', 'PRESS', ctrl=False, shift=False)
kmi = km.keymap_items.new(GridRescale.bl_idname, 'RIGHT_ARROW', 'PRESS', ctrl=True, shift=False)
addon_keymaps.append(km)
def unregister():
bpy.utils.unregister_class(GridRescale)
# handle the keymap
wm = bpy.context.window_manager
for km in addon_keymaps:
wm.keyconfigs.addon.keymaps.remove(km)
# clear the list
del addon_keymaps[:]
if __name__ == "__main__":
register()
```