Switching 3D viewport modes programmatically using Blender Python API

When developing add-ons for Blender, we may need to programmatically switch 3D viewport modes – for example, enable the WIREFRAME grid display mode.

To switch the 3D viewport to the desired mode, we need to change the “shading.type” property of the viewport area.

List of modes and values ​​to enable it:

  1. Wireframe grid mode – “WIREFRAME” value
  2. Common mode for working with meshes – “SOLID” value
  3. Display of textures and materials – “MATERIAL” value
  4. The same as in the final render – “RENDERED” value

For example, to enable the wireframe grid display mode in the 3D viewport, we need to set the “WIREFRAME” value to the “shading.type” property.

Don’t forget about the need to redefine the context if we execute this code in another area, for example, in the Text Editor.

We can get which display mode is currently enabled in the 3D viewport by checking this property:

We can place the buttons for switching the 3D viewport display modes on a custom user’s panel.

First define a test operator that will enable, for example, the wireframe grid mode.

Here, context overriding is not required, since the operator will be executed in the context of the 3D viewport itself.

Now, define a test panel on which we will place the button to call our operator.

And don’t forget to register both of our classes in Blender Python API.

After executing this code, a custom panel with a “WIREFRAME MODE” button will be created in the N-panel, by clicking on which the viewport mode will be switched to the wireframe grid display mode.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments