mesh=obj.data error when no object is present I experience the following error when no object is present on the layer:

Traceback (most recent call last):
  File "C:\Users\info\Desktop\untitled.blend\studioTools_Shelf_Beta v2.py", line 352, in draw
AttributeError: 'NoneType' object has no attribute 'data'

location: &lt;unknown location&gt;:-1


This is part of the object shading class I wrote.

class ObjectShading(bpy.types.Panel):
    bl_label = "Viewport Shading"
    bl_idname = "ObjectShading"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Shading Toolbox"


    def draw(self, context):
        layout = self.layout

        mesh = obj.data             

        row = layout.row()
        row.prop(mesh, "use_auto_smooth", text="Break Edges")


Do I have to build in a switch that in case no object is present on the layer this class is ignored meaning the object shading tab is not evaluated? Similar to the switch here:
Ray visibility panel

Here is what I learned and this approach works - I am just curious if this is the way one should do it or if in this case a better idea should be used.

class ObjectShading(bpy.types.Panel):
    bl_label = "Viewport Shading"
    bl_idname = "ObjectShading"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Modeling Toolbox"

    def draw(self, context):
        layout = self.layout

        mesh = obj.data
yeah an if / else construct is fine.

def draw(self, context):

    obj = context.object
    if obj and obj.data:
        # do one thing, if it has data
    elif obj:
        # probably an empty, an object without data
    else:
        # show something in the case of no object selected


Study the poll functions of the UI scripts that come with Blender to get an idea of common practices. Though the poll functions are usually to completely prevent panels or menus from showing or stopping Operators from executing. You'll find the same logic in place for hidding sections of a menu / layout.

Also be aware that sometimes you should be checking for context.active_object rather than just context.object.