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.

enter image description here

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

    def execute(self, context):
        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.

  • $\begingroup$ But then why does the interface button work when you are in edit mode? That is what I find confusing. $\endgroup$ Jan 24 '16 at 17:11
  • $\begingroup$ There is no Shade Smooth button in Edit mode's interface, only the shading buttons for faces, edges, and vertices in the shading/UV panel. $\endgroup$
    – cmomoney
    Jan 24 '16 at 18:37
  • $\begingroup$ I am not talking about the shade smooth button - I am talking about the autosmooth button inside the buttons data menu. (image in the post). this one works in each mode! $\endgroup$ Jan 24 '16 at 18:44
  • 1
    $\begingroup$ But that is not the part of your code that is failing, its the line before that, as stated in the error report. $\endgroup$
    – cmomoney
    Jan 24 '16 at 18:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.