Toggle Autosmooth and Doublesmooth at the same time

Hey guys,

I’m currently working on a Hard Surface Project and constantly have to toggle between the Mesh with 2 Subsurf modifiers activated + “Auto smooth” turned off versus the 2 Subsurf modifiers deactivated + “Autosmooth” turned on.

Can anyone help me to get this script work?

I’ve got this script that toggles only 1 Subsurf modifier of the active Object


bl_info = {    "name": "Subsurf Viewport Toggle",
    "description": "Adds toggle button in property region for subsurf viewport visibility",
    "author": "Aditia A. Pratama, Greg Zaal",
    "version": (0, 2),
    "blender": (2, 66, 1),
    "location": "3D View > Property Region (N-key)",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"}


import bpy


class SubsurfToggle(bpy.types.Operator):
    'Toggle subsurf visibility'
    bl_idname='subsurf.toggle'
    bl_label='Subsurf Toggle'
       
        
    
    def execute(self,context):
        state = bpy.context.active_object.modifiers['Subsurf'].show_viewport
        for e in bpy.context.selected_objects:
            try:
                if state==False:
                    e.modifiers['Subsurf'].show_viewport = True
                else:
                    e.modifiers['Subsurf'].show_viewport = False
            except KeyError:
                print ("No subsurf on "+e.name+" or it is not named Subsurf")
        return {'FINISHED'}
            
class Visibility(bpy.types.Panel):
    bl_label = "Subsurf Viewport"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
       
    def draw (self, context):
        layout=self.layout
                                                   
        col=layout.column()
        col.operator("subsurf.toggle",  text="On/Off", icon="MOD_SUBSURF")
        
        col.separator()            
        
        view = context.scene.render
          
        col.prop(view, "use_simplify", text="Simplify")
        sub = col.column()
        sub.active = view.use_simplify
        sub.prop(view, "simplify_subdivision", text="Subdivision")


def register():
   bpy.utils.register_module(__name__)
   
def unregister():
    bpy.utils.unregister_module(__name__)
 
if __name__ == "__main__":
    register()

Also I know that the following lines turn “Smooth Shading” On & Off


bpy.context.object.data.use_auto_smooth = True
bpy.context.object.data.use_auto_smooth = False


Unfortunately I’ve got no clue how to get this all together.

Cheers Christian

Try this:

bl_info = {
    "name": "Subsurf / Auto-smooth Viewport Toggle",
    "description": "Toggle button for subsurf viewport visibility and auto-smooth",
    "author": "Aditia A. Pratama, Greg Zaal, CoDEmanX",
    "version": (0, 3),
    "blender": (2, 66, 1),
    "location": "3D View > Property Region (N-key)",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"}


import bpy

class SubsurfToggle(bpy.types.Operator):
    """Toggle subsurf visibility"""
    bl_idname = "subsurf.toggle"
    bl_label = "Subsurf Toggle"
    
    @classmethod
    def poll(cls, context):
        return (context.object is not None and
                context.object.type == 'MESH')
    
    def execute(self, context):
        ob = context.object
        for mod in ob.modifiers:
            if mod.type == 'SUBSURF':
                state = mod.show_viewport
                break
        else:
            state = not ob.data.use_auto_smooth
            
        for ob in context.selected_objects:
            ob.data.use_auto_smooth = state
            for mod in ob.modifiers:
                if mod.type == 'SUBSURF':
                    mod.show_viewport = not state
                    
        return {'FINISHED'}


class Visibility(bpy.types.Panel):
    bl_label = "Subsurf Viewport"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
       
    def draw (self, context):
        layout = self.layout
        render = context.scene.render
                                                   
        col = layout.column()
        col.operator("subsurf.toggle", text="On/Off", icon="MOD_SUBSURF")
        
        col.separator()            
        
        col.prop(render, "use_simplify", text="Simplify")
        sub = col.column()
        sub.active = render.use_simplify
        sub.prop(render, "simplify_subdivision", text="Subdivision")


def register():
   bpy.utils.register_module(__name__)
   
def unregister():
    bpy.utils.unregister_module(__name__)
 
if __name__ == "__main__":
    register()

Thank you so much!!
Works so great and saves me a lot of clicks :slight_smile:
Best regards,
Christian