Custom ui operators

There are some UI operators like “ui.reset_default_button” and “ui.copy_data_path_button” which has access to buttons and parameter sliders and can be assigned to shortcuts…
Is this possible to write custom operators like these? i want to create some handy shortcuts for parameters like a shortcut to replace parameter value by negative of that value (replace 1 with -1) or some other mathematics or set a keyboard shortcut to cycle a drop down list property; the behavior of Ctrl + wheel but with keyboard shortcut like arrow up/down.
Is this possible?

1 Like

Ui button context is only available once the hard-coded right-click menu has been invoked (see here). Afaik there’s no way to invoke it through python, but I haven’t really experimented with this.

One way I know of is to copy the data path under the cursor to the clipboard and evaluate it. This, however, doesn’t work for all properties, especially not properties that aren’t exposed to python (i.e vertex location).
Stuff like object location, scale, etc should work, though.

This example tries to invert the property under the cursor. Usage: Run in text editor. Hover over a numeric prop and search for Invert.

import bpy


class UI_OT_invert(bpy.types.Operator):
    bl_idname = "ui.invert"
    bl_label = "Invert"

    def execute(self, context):

        wm = context.window_manager
        __class__.buffer = wm.clipboard

        try:
            bpy.ops.ui.copy_data_path_button(full_path=True)
            eval_path = eval(wm.clipboard)
            exec(f"{wm.clipboard} = -eval_path")
        except (RuntimeError, TypeError) as err:
            print(err)
            return {'CANCELLED'}

        finally:
            # restore clipboard content
            wm.clipboard = __class__.buffer
        return {'FINISHED'}

if __name__ == "__main__":
    bpy.utils.register_class(UI_OT_invert)

I need general operations so this works very well
Thank you very much :pray::pray::rose::rose: