# Toggle Subdivision modifier and smooth shading with one key

Is there a hotkey, script or addon to globally toggle smooth shading and subD?

When modeling with subD's I like having the shading faceted, but obviously want to see it smooth when I turn the subD modifier on. I figured out how to use the Simplify to turn subD on/off on all meshes with one button, but this doesn't change the smoothing (1st picture). So I have to go into every object, select all and adjust smooth shading whenever I want to preview my model.

Is there a way to toggle [smooth shading off/subD off] and [smooth shading on/subD on] with one hotkey? In Modo this happens when pressing Tab (2nd picture).

## You can get it using Python.

I used a template "Operator Simple". And I added subsurf and smooth to an object to know the names of this functions:

Next, I replaced default main() function by this functions:

def main(context):
bpy.ops.object.subdivision_set(level=2)


It gives subsurf and smoth, but never gets back.

To create a toggle, you need to know about programming. I writed this code:

def main(context):
test=True
for md in context.active_object.modifiers:
if(md.type=='SUBSURF'):
if(md.levels==2):
test=False;
break
if(test):
bpy.ops.object.subdivision_set(level=2)
else:
bpy.ops.object.subdivision_set(level=0)


It checks for subsurf level 2, and if finds, removes Subsurf and smooth.

Finally, I can use this function by finding it in spacebar menu:

## UPD: For making it works in Edit Mode

You noted what script doesn't work in Edit Mode. It is because shade_smooth() doesn't work in Edit Mode. So lets try to enter Object Mode, do subsurf and smooth operations and return to edit mode.

Get the current mode, and save:

if(context.mode!='OBJECT'):
returnto=context.mode


if(returnto):
bpy.ops.object.mode_set(mode=returnto)


## Full code

(you can save it as .py file and install it like add-on):

bl_info = {
"name": "Subsmoth",
"author": "Subsmoth",
"version": (1, 0),
"blender": (2, 78, 0),
"location": "",
"description": "Toggle Subsurf and smooth",
"warning": "",
"wiki_url": "",
"category": "3D View",
}
import bpy

def main(context):
test=True
for md in context.active_object.modifiers:
if(md.type=='SUBSURF'):
if(md.levels==2):
test=False;
break
returnto=False
if(context.mode!='OBJECT'):
returnto=context.mode
if(returnto=='EDIT_MESH'):
returnto='EDIT'
bpy.ops.object.mode_set(mode='OBJECT')
if(returnto=='EDIT' or returnto==False):
if(test):
bpy.ops.object.subdivision_set(level=2)
else:
bpy.ops.object.subdivision_set(level=0)
if(returnto):
bpy.ops.object.mode_set(mode=returnto)

class SubsmothOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.subsmooth"
bl_label = "Subsmoth"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
main(context)
return {'FINISHED'}

def register():
bpy.utils.register_class(SubsmothOperator)

def unregister():
bpy.utils.unregister_class(SubsmothOperator)

if __name__ == "__main__":
register()

• Fantastic - thanks a lot of this, it's definitely gets me 90% there. It works great in object mode, but is there also a way to make it work in edit mode? It gives me: RuntimeError: Operator bpy.ops.object.shade_flat.poll() failed, context is incorrect – Hybelkanin Jan 12 '17 at 22:19
• @Hybelkanin Ok, I have improved my script – Crantisz Jan 13 '17 at 6:42
• It works like a charm now. I toggle the modifier about every 10 sec, so all the clicks used to be a hassle. You just ironed out a big wrinkle in my workflow - many thanks! – Hybelkanin Jan 13 '17 at 15:07