1
$\begingroup$

I was trying to add a shortcut for the Auto Depth toggle because I use it frequently. Sometimes, I feel the need to open or close the Preferences window quickly, but the process is too slow right now.

Blender Navigation Panel

I found a similar thread on this site, but I believe it's too old for Blender 4.1 or 4.2. Can anybody help me add it to the shading pie menu?

$\endgroup$
1
  • $\begingroup$ Hello and welcome. Use preferences.inputs.use_mouse_depth_navigate $\endgroup$
    – Harry McKenzie
    Commented Jul 4 at 7:22

1 Answer 1

1
$\begingroup$

Similar instructions as Can I assign a toggle shortcut to the Cursor Depth feature in User Preferences? except that in Context Attributes, you you have to use preferences.inputs.use_mouse_depth_navigate

Follow these steps:

  • Go to Keymap tab in Edit > Preference or press Ctrl+,

  • Under 3D View > 3D View (Global), scroll down to the end of the list until you find Add New button.

  • Click the Add New button

  • Click the arrow to the left of the newly created hotkey entry and replace the text input value none with wm.context_toggle.

  • A new box appears labeled Context Attributes where you should input the value preferences.inputs.use_mouse_depth_navigate

  • Give it a hotkey that does not conflict with any existing hotkey in 3D View.

You can easily create a Pie Menu using the templates in the Scripting Tab with menu Templates > Python > UI Pie Menu:

enter image description here

Then replace the green underlined text with this line:

pie.operator("wm.context_toggle", text="Toggle Auto Depth").data_path = "preferences.inputs.use_mouse_depth_navigate"

Run the script and test the Pie Menu.

enter image description here

You can also put this into an Operator that invokes this Pie Menu like in this example where your operator defined as WM_OT_CallPieMenu calls bpy.ops.wm.call_menu_pie. Run the script and press F3 in the 3D Viewport and find the operator name "Call Pie Menu" then press enter.

import bpy

class VIEW3D_MT_PIE_template(bpy.types.Menu):
    bl_idname = "VIEW3D_MT_PIE_template"
    bl_label = "Select Mode"

    def draw(self, context):
        layout = self.layout

        pie = layout.menu_pie()
        pie.operator("wm.context_toggle", text="Toggle Auto Depth").data_path = "preferences.inputs.use_mouse_depth_navigate"

class WM_OT_CallPieMenu(bpy.types.Operator):
    bl_idname = "wm.call_pie_menu"
    bl_label = "Call Pie Menu"
    bl_description = "Invoke a custom pie menu"

    def execute(self, context):
        bpy.ops.wm.call_menu_pie(name=VIEW3D_MT_PIE_template.bl_idname)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(VIEW3D_MT_PIE_template)
    bpy.utils.register_class(WM_OT_CallPieMenu)


def unregister():
    bpy.utils.unregister_class(VIEW3D_MT_PIE_template)
    bpy.utils.unregister_class(WM_OT_CallPieMenu)


if __name__ == "__main__":
    register()

Note that wm.call_pie_menu is the bl_idname you define yourself for your custom operator which is different from the built-in operator bpy.ops.wm.call_menu_pie.

Additionally you can define a Keymap for this operator so you can invoke it with a keyboard shortcut. Same as the above steps but instead, replace none with your operator's bl_idname name called wm.call_pie_menu then add the keyboard shortcut.

enter image description here

In this example I can invoke the Pie Menu using Shift+, but you can customize it with any shortcut so as long it does not conflict with existing shortcuts.

But I just figured out that's redundant when you can directly call the Pie Menu without defining any new Operators by replacing none with the built-in operator wm.call_menu_pie and then adding the Pie Menu's name VIEW3D_MT_PIE_template in the input box below:

enter image description here

If you want to add your "Toggle Auto Depth" menu option to the Shading Pie Menu (shortcut Z), you will have to open the file:

C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\startup\bl_ui\space_view3d.py

Find the class VIEW3D_MT_shading_pie and append the following line at the end of the draw function of this class:

pie.operator("wm.context_toggle", text="Toggle Auto Depth").data_path = "preferences.inputs.use_mouse_depth_navigate"

So your class looks something like this:

class VIEW3D_MT_shading_pie(Menu):
    bl_label = "Shading"

    def draw(self, context):
        layout = self.layout
        pie = layout.menu_pie()

        view = context.space_data

        pie.prop(view.shading, "type", expand=True)
        pie.operator("wm.context_toggle", text="Toggle Auto Depth").data_path = "preferences.inputs.use_mouse_depth_navigate" # add this line

Then save/overwrite it and restart Blender and press Z to open the Shading Pie Menu. Now you have your custom menu item:

enter image description here

Be careful when making modifications to the default Blender files because you can end up messing up the files and not be able to open Blender again. So make backups if you are unsure of what you are doing.

$\endgroup$
2
  • 1
    $\begingroup$ I appreciate your assistance. Could you recommend any reliable sources where I can learn more about this topic? $\endgroup$ Commented Jul 4 at 11:17
  • $\begingroup$ About python? Here's a good series youtube.com/…. $\endgroup$
    – Harry McKenzie
    Commented Jul 4 at 11:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .