1
$\begingroup$

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()
$\endgroup$
3
  • 2
    $\begingroup$ AFAIK the name has to match the area for whatever reason. Setting the name to 'Image' works, see: pasteall.org/dXT0/raw. However I'd expect 'UV Editor' to work as well which doesn't. Not sure why, need to investigate. $\endgroup$
    – brockmann
    Aug 18 at 14:29
  • 1
    $\begingroup$ 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? $\endgroup$ Aug 18 at 21:49
  • $\begingroup$ @brockmann Could you add that as an answer? $\endgroup$
    – Leander
    Sep 9 at 13:12
1
$\begingroup$

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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.