Operator for toggling checkboxes

The “wm.context_toggle” operator is used to toggle checkboxes in the Blender interface. We can configure fast switching of any checkboxes in any windows and panels with this operator.

As an example, let’s configure the checkbox which enables/disables displaying the viewport wireframe.

The “wm.context_toggle” operator takes a text string as its first argument – a pointer to the property described by the checkbox in the Blender Python API.

For the viewport “Wireframe” checkbox this path looks like this:

We can access it with the “wm.context_toggle” operator:

The operator is contextual so to work properly it must be called from the 3D Viewport window context. Also, there is no need to specify the full path with the context specification as “bpy.context.space_data.overlay.show_wireframes”, it is enough to specify only a part after specifying the context.

This operator can be assigned, for example, to a button in a user panel. However, it is much more convenient to use this operator to assign key combinations to quickly switch the desired checkbox.

To associate switching wireframe with a keyboard keycode, in the “Preferences” window in the “KeyMap” section open the “3D View – Object Mode – Object Mode (Global)” list, create a new shortcode with a convenient key combination.

In the “Identifier” field specify the “wm.context_toggle” operator call, and in the field that appears for the “Context Attribute” argument, type the API path to the required property – “space_data.overlay.show_wireframes”.

Now, when pressing the specified keycode in the viewport window, the display of the wireframe on objects will be toggled.

5 1 vote
Article Rating
Subscribe
Notify of
guest

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Rombout Versluijs
2 years ago

I always leanr something from your tuts 🙂 I was looking to get a toggle for the enum 3 button toggle. Was testing a custom operator, but once it was one. I would react because i used left mouse click for the toggle. This toggle also didnt work in your case, but now a shortcut alt shift 1 toggles it. I was wanted to be able to use ALT so i can apply settings to everything select. This wont work when enum 3 mouse button is on