If statement not working with object shade attribute.


I am having trouble with a script I’m working on. It is a menu script. I have an “if” statement that checks whether or not the active objects shade is flat, if it is, it will give the user the option to change it to smooth.

Here is the code:

def draw(self, context):
        self = self.layout
        obj = bpy.context.active_object
        self.operator("mesh.primitive_cube_add",icon= "MESH_CUBE") 
        self.operator("mesh.primitive_uv_sphere_add",icon= "MESH_UVSPHERE")
        self.operator("mesh.primitive_plane_add",icon= "MESH_PLANE")
        if obj.shade_flat:

when I run the script, it gives me the error saying that “object has no attribute shade_flat”. I am pretty new to python, though I know some c++ and some c#.


smooth or flat shading is set per face, so you need to check a certain polygon.


In case you didn’t know, you can check each objects attribute and methods with the autocomplete feature in the python console.


Thanks for replying so quick. I attempted to change my code to match what you put, but I could not get it to work. The first part you put is hard for me to understand, but I tried putting it in. I used that line of code, and told it to check if it was false.; but I got an error message. If I remove the false check, the script will run with no errors or warning, but no smooth option will appear.

if bpy.context.object.data.polygons[0].use_smooth = False:

I will keep digging around and see if I can figure it out, if I do, I will post back.

With if you should use ==, not = (thats only for assigning variables). In python if you dont put a == it takes it as True, 0, empty array etc.
So just change that and it will work.

LOL, I fell very silly, that is a very simple syntax error I missed. Thank’s!