Autosmooth script fails in edit mode but works in object mode Here is a script I did to automate autosmooth and set the degree.
Pretty simple. It works in object mode great, however in edit mode it fails.
What I do not understand is why. It does not matter if I am in edit or object mode when I use the regular buttons in Blenders UI.



class SetAutoSmooth(bpy.types.Operator):
    bl_idname ="set.autosmooth"
    bl_label ="Set Autosmooth"

    def execute(self, context):
        bpy.ops.object.shade_smooth()
        bpy.context.object.data.use_auto_smooth = True
        bpy.context.object.data.auto_smooth_angle = 0.523599

        return {'FINISHED'}


Here is the error I get. It states that context is incorrect. 


  Traceback (most recent call last):
        File "C:\Users\info\Desktop\Coding.blend\class Pie.py", line 14, in execute
        File "C:\Users\info\Desktop\blender-2.76.0-git.d330162-AMD64\2.76\scripts\modules\bpy\ops.py", line 189, in call
          ret = op_call(self.idname_py(), None, kw)
      RuntimeError: Operator bpy.ops.object.shade_smooth.poll() failed, context is incorrect 
  
  location: :-1
bpy.ops.object.shade_smooth() is only accessible from Object Mode (poll). To use it, you would have to toggle into Object Mode first. To shade in Edit Mode (using ops) you would have to use bpy.ops.mesh.faces_shade_smooth(). This will only work on selected faces though.