1
$\begingroup$

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()
```
$\endgroup$
0

1 Answer 1

2
$\begingroup$

Return a status set from invoke.

AFAICT the issue here is as simple runtime error, not returning a valid status set from an operators invoke method. No return, returns None.

Pretty obvious from error message printed to system console on testing a keypress.

evt type RIGHT_ARROW
Python: RuntimeError: class KITFOX_OT_grid_rescale, 
function invoke: incompatible return value , , 
Function.result expected a set, not a NoneType

Possible suggestion.

def invoke(self, context, event):
    print("evt type %s" % (event.type))
    return self.execute(context)

PS in a method where context is passed, common in operators and panels, there is never a need to see bpy.context

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .