Blender 2.8 Python Help

I want to get some assistance in combining 2 functions into one keymap and I was told that python addon is the best way to do it. I initially tried to make them have the same keyboard shortcut but that didn’t work.

Also is there an alternative to wm.context.toggle? I want to make it based on holding down and releasing a button rather than toggling between values

To combine the two commands into one keymap, you can create either a macro that calls both functions at once, then assign that to the hotkey, or assign a custom operator that does the same thing.

You could lay it out like this for an operator, and then add it to a script with your defined arguments;

import bpy
from bpy.types import Operator

class CursorMacro(Operator):
    bl_idname = "cursor.macro"
    bl_label = "Macro for Cursor"

    def execute(self, context):
        # The context toggle may not work as the arguments are not defined, you will need to determine what they should be
        bpy.ops.wm.context_toggle(data_path=space_data.overlay.show_cursor, module="")
        bpy.ops.wm.tool_set_by_id(name="builtin.cursor")
        return {"FINISHED"}

addon_keymaps = []

def register():
     # Registering the function to be used by Blender
     bpy.utils.register_class(CursorMacro)

     # Assigning the custom macro to the keymap
     wm = bpy.context.window_manager
     if wm.keyconfigs.addon:
         km = wm.keyconfigs.addon.keymaps.new(name="Object Mode")
         kmi = km.keymap_items.new(CursorMacro.bl_idname, '\', 'PRESS')
         addon_keymaps.append((km, kmi))

if __name__ == "__main__":
    register()

With a macro, you can add in pre-defined functions in blender to be called all at once;

import bpy
from bpy.types import Macro 

class CursorMacro(Macro):
    bl_idname = "cursor.macro"
    bl_label = "Macro for Cursor"

addon_keymaps = []
def register():
    bpy.utils.register_class(CursorMacro)
    function_1 = CursorMacro.define("WM_OT_context_toggle")
    # Will need to determine the correct attribute for the function as well as data_path not being the correct identifier
    function_1.data_path = space_data.overlay.show_cursor
    function_2 = CursorMacro.define("WM_OT_tool_set_by_id")
    function_2.name = "builtin.cursor"
    wm = bpy.context.window_manager
    if wm.keyconfigs.addon:
        km = wm.keyconfigs.addon.keymaps.new(name="Object Mode")
        kmi = km.keymap_items.new(CursorMacro.bl_idname, '\', 'PRESS')
        addon_keymaps.append((km, kmi))

if __name__ == "__main__":
    register()

Helpful links about these concepts;
https://blender.stackexchange.com/questions/44971/how-to-use-bpy-ops-macro-defining-macro-steps
https://blender.stackexchange.com/questions/79102/how-can-i-pass-arguments-to-an-operator-inside-a-macro-operator

Hi @Nick0!

I was actually able to create the script earlier with someone on blender.chat similar to what you made but the keymap script part is something I haven’t added in, I’m just referencing the operator in the keymap. It’s definitely something to add in. Thanks! :+1:

I was also able to create another operator onto another keymap for toggling on and off with an if statement because I realised that you can’t make manual adjustments to edit the 3D Cursor’s rotation when it’s hidden.

I want to make it based on if i press a shortcut it shows itself and allows you to make modification but when i press a different keyboard shortcut fo a different tool it hides itself. Do you know how i can do this?

I would really appreciate the help!