Toggling between flat and smooth shading

To quickly toggle geometry shading from flat mode to smooth and back, we need to change the value of the “smooth” property for each polygon of the mesh.

Just to enable smooth shading for the active object, we can execute the following code:

Specifying False instead of True, we will enable the flat shading mode.

To make toggling – switching shading between these two modes and back, we can use the “not” operator for the “smooth” property:

Every time this code is executed, the shading will switch from the current mode to the opposite.

Let’s define an operator, and place this code for toggling shading modes in its execute function:

For each of the selected objects, we loop through its polygons and reverse shading.

To see the changes in the 3D viewport immediately, we execute the update command for the object data:

Define a custom panel in the 3D viewport area with the “SHADE” tab label:

In its draw function, we define a “Toggle Shade” button that invokes the toggle shading operator we defined earlier.

Don’t forget to register the operator and panel classes in the Blender Python API:

Now, each time we press the button on our panel, the shading of all selected objects will toggle to the opposite one.

0 0 votes
Article Rating
Notify of

0 Comment
Inline Feedbacks
View all comments