1

I've created an operator that uses a keymap to activate it. While it works in object mode, it is not being called in edit mode. Is there a way to get this to run in edit mode too?

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):
    scale = bpy.context.space_data.overlay.grid_scale

    if event.type == "LEFT_ARROW":
        scale /= 2
    elif  event.type == "RIGHT_ARROW":
        scale *= 2

    bpy.context.space_data.overlay.grid_scale = scale
    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, 'LEFT_ARROW', 'PRESS', ctrl=True, 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()

brockmann
  • 12,613
  • 4
  • 50
  • 93
kitfox
  • 1,616
  • 5
  • 26
  • 40
  • 1
    You can set the name to '3D View', demo here: Create keyboard shortcut for an operator using python? if that helps....? Otherwise you would have to declare another one for Edit Mode. Also note: some of your references are wrong, replace: bpy.context.space_data.overlay.grid_scale by context.space_data.overlay.grid_scale -> context is already passed by the invoke method invoke(self, >context<, event) – brockmann Feb 18 '21 at 19:21
  • km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D') seems to fix the problem. – kitfox Feb 18 '21 at 20:42

0 Answers0