1
$\begingroup$

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: <unknown location>:-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
$\endgroup$

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Thank you very much for explaining the best use of poll (hiding panel) and other stages to check that object is present and then show selectively only what makes sense. This removed all errors so far in my complete script. $\endgroup$ Commented Feb 2, 2016 at 16:58
  • $\begingroup$ I've asked a follow-up question blender.stackexchange.com/questions/124683/… , maybe you can shed some light on it? $\endgroup$
    – aliasguru
    Commented Dec 9, 2018 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .