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](https://cdn.statically.io/img/i.sstatic.net/J5Fasb2C.png)
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](https://cdn.statically.io/img/i.sstatic.net/65lcO3AB.gif)
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](https://cdn.statically.io/img/i.sstatic.net/JfNoQM52.png)
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](https://cdn.statically.io/img/i.sstatic.net/1tujdG3L.png)
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](https://cdn.statically.io/img/i.sstatic.net/ery9qPvI.png)
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.
preferences.inputs.use_mouse_depth_navigate
$\endgroup$