2

I am trying to create a keymap to scale the selected verts in the UV Editor to zero in the X direction. The code below works correctly in the UV Editor with F3 and find operator "Scale UV selected verts to X=0"

So that's all good.

When I bind a keymap to the space_type 'IMAGE_EDITOR', it shows up as registered in PREFS, but does not execute when I press Shift+Ctrl+X. What am I doing wrong? FWIW, I've tried numerous different key combinations with no success. Thanks for any help.

import bpy

SCALE X IN UV EDITOR

class OBJECT_OT_scale_xuv(bpy.types.Operator): """scales VERTS to x=0""" bl_idname = "object.vertscale_xuv" bl_label = "Scale UV selected verts to X=0"

def execute(self, context):

    bpy.ops.transform.resize(value=(0, 1, 1), orient_type='VIEW', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), 
        orient_matrix_type='VIEW', constraint_axis=(True, False, False), 
        mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', 
        proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

    return {'FINISHED'}


addon_keymaps = []

def registerKeymaps(): wm = bpy.context.window_manager if wm.keyconfigs.addon:

    # KEY SHORTCUTS FOR SPACE TYPE IMAGE_EDITOR
    km_uv = wm.keyconfigs.addon.keymaps.new(name='Image Editor Tool: Uv, Scale', space_type='IMAGE_EDITOR')

    ## SCALE UV VERTS X
    km_uv_i = km_uv.keymap_items.new('object.vertscale_xuv', 'X', 'PRESS', shift=True, ctrl=True)
    addon_keymaps.append((km_uv, km_uv_i))



def unregisterKeymaps(): for km, kmi in addon_keymaps: km.keymap_items.remove(kmi) addon_keymaps.clear()

def register(): bpy.utils.register_class(OBJECT_OT_scale_xuv) registerKeymaps()

def unregister(): unregisterKeymaps() bpy.utils.unregister_class(OBJECT_OT_scale_xuv)

if name == "main": register()

batFINGER
  • 84,216
  • 10
  • 108
  • 233
chippwalters
  • 600
  • 3
  • 15
  • 2
    AFAIK the name has to match the area for whatever reason. Setting the name to 'Image' works, see: https://pasteall.org/dXT0/raw. However I'd expect 'UV Editor' to work as well which doesn't. Not sure why, need to investigate. – brockmann Aug 18 '21 at 14:29
  • 1
    Well, FWIW, that was the fix. Not sure why 'Image' works and nothing else does. Thanks SO MUCH for your help!!! How can I mark your solution as Answered? – chippwalters Aug 18 '21 at 21:49
  • @brockmann Could you add that as an answer? – Leander Sep 09 '21 at 13:12

1 Answers1

3

Pass 'Image' as the name and 'IMAGE_EDITOR' as space_type to keymaps.new(), and the registration works as expected. CtrlY demo based on the Operator Simple template:

import bpy

class SimpleOperator(bpy.types.Operator): """Tooltip""" bl_idname = "image.simple_operator" bl_label = "Simple Image Operator"

def execute(self, context):
    print(SimpleOperator.bl_idname, "called")
    return {'FINISHED'}

addon_keymaps = [] def register(): bpy.utils.register_class(SimpleOperator) addon_keymaps.clear()

wm = bpy.context.window_manager # handle the keymap
kc = wm.keyconfigs.addon
if kc:
    km = wm.keyconfigs.addon.keymaps.new(name='Image', space_type='IMAGE_EDITOR')
    kmi = km.keymap_items.new(
        SimpleOperator.bl_idname, type='Y', value='PRESS', ctrl=True)
    addon_keymaps.append((km, kmi))

def unregister(): bpy.utils.unregister_class(SimpleOperator)

for km, kmi in addon_keymaps:
    km.keymap_items.remove(kmi)
addon_keymaps.clear()

if name == "main": register()

If you would like to limit the hotkey to only work when editing UV's, just add a poll method to your operator asking for an active UV map or whether there is an image loaded at all:

    @classmethod
    def poll(cls, context):
        sima = context.space_data
        return sima.show_uvedit and sima.image is None

Related: Create keyboard shortcut for an operator using python?

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Cool, is there an exhaustive list for the names? Right now I'm just looking at the keymap titles in the UI Preferences – Leander Sep 09 '21 at 14:22
  • To place in UV Editor keymaps km = wm.keyconfigs.addon.keymaps.new(name='UV Editor', space_type='EMPTY') – batFINGER Sep 09 '21 at 15:46
  • Thanks @batFINGER, Mystery, when passing UV Editor and EMPTY the hotkey does not register properly... also tried to pass Window, no luck. – brockmann Sep 10 '21 at 08:56
  • Not that I know of, I always use the console and shimmy through the default hotkeys @Leander – brockmann Sep 10 '21 at 08:59