Passing an argument to an operator

Thanks. That does provide more context.

To answer your original question regarding hotkeys; when you create a hotkey, the object returned is what’s called an OperatorProperties instance. You can set predefined arguments on these just like you would when defining button operators in layouts.

km = ...  # Sequencer keymap
kmi = km.keymap_items.new("sequencer.settextcolor", "G", "PRESS", ctrl=True, shift=True)
#                         bl_idname                 key  type     modifiers

kmi.properties.color = Colours.GREEN.value

When the hotkey is pressed. It will be as if the operator was called with bpy.ops.sequencer.settextcolor(color=Colours.GREEN.value).

An example that sets up ctrl+shift+g and ctrl+shift+b to set a text strip’s color to green/blue respectively.
The keymap is added and removed automatically when the operator is registered/unregistered.

Granted, the boilerplate is arguably more lines of code than the operator itself :joy:

class SetTextColour(TextSequenceAction):
    """Set colour of text sequence[s]"""
    bl_idname = "sequencer.settextcolor"
    bl_label = "Set Text Colour"

    _keymaps = []
    color: bpy.props.FloatVectorProperty(size=4, default=(1, 1, 1, 1))

    def execute(self, context):
        for strip in bpy.context.selected_editable_sequences:
            if strip.type == "TEXT":
                strip.color = self.color

        return {'FINISHED'}

    @classmethod
    def register(cls):
        # Boilerplate
        wm = bpy.context.window_manager
        km = wm.keyconfigs.addon.keymaps.get("Sequencer")
        if km is None:
            km = wm.keyconfigs.addon.keymaps.new("Sequencer", space_type='SEQUENCE_EDITOR')

        # 'ctrl + shift + g' -> SetTextColour(color=Colours.GREEN.value)
        kmi = km.keymap_items.new(cls.bl_idname, 'G', 'PRESS', ctrl=True, shift=True)
        kmi.properties.color = Colours.GREEN.value
        cls._keymaps.append((km, kmi))

        # 'ctrl + shift + b' -> SetTextColour(color=Colours.BLUE.value)
        kmi = km.keymap_items.new(cls.bl_idname, 'B', 'PRESS', ctrl=True, shift=True)
        kmi.properties.color = Colours.BLUE.value
        cls._keymaps.append((km, kmi))

    @classmethod
    def unregister(cls):
        # Remove keymaps when operator is unregistered
        for km, kmi in cls._keymaps:
            km.keymap_items.remove(kmi)
        cls._keymaps.clear()
1 Like