5
$\begingroup$

Is there a way to check if an active object is either an object a curve or a lamp?

Can this be done via a poll function?

The following poll works to checks if an object is object+mesh and then shows the shelf menu, and if not like an empty hides the shelf menu.

@classmethod
def poll(cls, context):
    return context.object and context.object.data

But if I have a curve or lamp selected / present in the layer this does not work because a curve for example has no autosmooth setting.

Empty Selected: enter image description here Object+Mesh Selected: enter image description here Curve Selected: enter image description here

This is the code for object+data show:

# DATA ---------------------------------------------------------------------------
class ObjectData(bpy.types.Panel):
    bl_label = "Object Data and Relationships"
    bl_idname = "ObjectData"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Modeling Toolbox"

    @classmethod
    def poll(cls, context):
        return context.object and context.object.data

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

        obj = context.object

        col = layout.column()
        col.template_ID(context.scene.objects, "active")
        col.template_ID(obj, "data")
#---------------------------------
        row = layout.row()
        row.operator("object.make_links_data", text="Link Mesh Data")
        row.operator("object.make_single_user", text="Make Single User").obdata=True
#---------------------------------    
        def draw_props(labelname, propname):
            c = layout.column()
            row = c.row()
            split = row.split(percentage=0.2)
            c = split.column()
            c.label(labelname)
            split = split.split()
            c = split.column()
            c.prop(obj, propname, text="")

        draw_props("Layers", "layers")
        draw_props("Parent", "parent")             
#---------------------------------
        row = layout.row(align=True)
        if bpy.data.groups:
            row.operator("object.group_link", text="Add to Group")
        else:
            row.operator("object.group_add", text="Add to Group")
        row.operator("object.group_add", text="", icon='ZOOMIN')
        obj_name = obj.name
        for group in bpy.data.groups:
            group_objects = group.objects
            if obj_name in group.objects and obj in group_objects[:]:
                col = layout.column(align=True)

                col.context_pointer_set("group", group)

                row = col.box().row()
                row.prop(group, "name", text="")
                row.operator("object.group_remove", text="", icon='X', emboss=False)
                row.menu("GROUP_MT_specials", icon='DOWNARROW_HLT', text="")
        row = layout.row()
#---------------------------------
        if bpy.context.object.mode == "EDIT":
            row.operator("mesh.separate", text="Separate Mesh into new Object") 
        else :
            row.operator("object.join", text="Join Objects into selected Object")   
        row = layout.row()
#---------------------------------    
        row = layout.row()
        if bpy.context.object.mode == "EDIT":
            self.layout.operator("set.origin")
        else :
            row.operator_menu_enum("object.origin_set", "type", text="Set Origin")

#---------------------------------  
        obj = context.active_object

        if obj.type == 'CURVE':

            curve = context.active_object.data

            col = layout.column()
            col.label(text="Curve Data")
            col.prop(curve, "resolution_u", text="Preview U")
            col.prop(curve, "render_resolution_u", text="Render U")
            if bpy.context.object.mode == "EDIT":
                row = col.row()
                row.prop(context.scene.tool_settings, "normal_size", text="Normal")   
                row.prop(curve, "show_normal_face", text="Normals")
            col.label(text="Extrude Cross Section")
            col.prop(curve, "bevel_object", text="") 

This is the code for the object+shading:

# SHADING ---------------------------------------------------------------------------
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"

    @classmethod
    def poll(cls, context):
        return context.object and context.object.data

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

        obj = context.object
        mesh = obj.data
        mat = obj.active_material
        space = context.space_data

        scene = context.scene
        cscene = scene.cycles
        ob = context.object
        cob = obj.cycles
        visibility = ob.cycles_visibility

        row = layout.row()
        row.prop(mesh, "use_auto_smooth", text="Break Edges")
        row.active = mesh.use_auto_smooth and not mesh.has_custom_normals
        row.prop(mesh, "auto_smooth_angle", text="Angle")
        row = layout.row()


        if bpy.context.object.mode == "EDIT":
            row = layout.row()
            row.operator("mesh.faces_shade_smooth", text="Smooth", icon="SMOOTHCURVE")
            row.operator("mesh.faces_shade_flat", text="Sharp", icon="LINCURVE")
        else :
            row = layout.row()
            row.operator("object.shade_smooth", text="Smooth", icon="SMOOTHCURVE")
            row.operator("object.shade_flat", text="Sharp", icon="LINCURVE")  

        row = layout.row()
        row.prop(obj, "draw_type", text="Draw Type")           
        row = layout.row()
        row.label(text="Draw Option:")
        row.prop(obj, "show_transparent", text="Transparency")
        row.prop(obj, "show_x_ray", text="X-Ray")
        row = layout.row()

#---------------------------------  

        if obj and obj.active_material:
            mat = obj.active_material

            if mat:
                layout.template_ID(obj, "active_material", new="material.new")
                row.prop(mat, "diffuse_color", text="Draw Color")
                row.prop(mat, "alpha")
            else:
                row.label('no active_material')
        else:
            row.label('Add a Material')
            layout.template_ID(obj, "active_material", new="material.new")
            #layout.template_ID(space, "pin_id")

        row = layout.row()
        row.operator("material.object", text="Set Object Material")
        row.operator("material.data", text="Set Data Material")
$\endgroup$
  • 1
    $\begingroup$ object.type returns the type of object , is it this ? $\endgroup$ – Chebhou Feb 13 '16 at 18:58
  • $\begingroup$ @Chebhou in the poll function via object.type a switch could be build in? $\endgroup$ – Claas Kuhnen Feb 13 '16 at 19:24
  • $\begingroup$ if you mean a check ( if statement ) then it is return object.type == 'MESH' $\endgroup$ – Chebhou Feb 13 '16 at 19:27
  • $\begingroup$ I have the feeling that the poll function can only check for something and then let you go on with the script. But it cannot check if you have an object, empty, curve, or lamp selected and then for each part only execute a certain part of the script. $\endgroup$ – Claas Kuhnen Feb 13 '16 at 19:30
  • $\begingroup$ for example lamp and curve will cause an py error $\endgroup$ – Claas Kuhnen Feb 13 '16 at 19:31
5
$\begingroup$

The above (self)answer is OK. Using object.type is the way. Still:

  1. I think is recommended/somewhat better using things in this form:

    objType = getattr(obj, 'type', '')
    
    if objType in ['MESH','CURVE']: etc
    elif objType in ... etc
    elif objType in ... etc
    
    • get is the better way to get :)
    • when testing something (type) several times, may wanna have a variable first instead of reading type/ looking for a prop all the time (not a big deal for UI speed, but generally ..)
    • if/elif ensures they are alternatives, not working on top of each other(not the case here, you cannot have many types), generally if/if will make the last one valid even if 2 are valid. And as alternatives, it also means it should stop when the condition is met, while if, if, if will go to test all of them, cause may just be about something else.
  2. You may wanna test for the object if exists or another condition not sure you need this, but if obj is not None and maybe other conditions may do

  3. so the code above would become (without testing the obj):

    class ObjectShading(bpy.types.Panel):
        bl_label = "Transformation"
        bl_idname = "Transformation"
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'TOOLS'
        bl_category = "Modeling Toolbox"
    
        def draw(self, context):
            layout = self.layout
    
            obj = context.object
            unit = context.scene.unit_settings
    
            objType = getattr(obj, 'type', '')
    
            if objType  in ['MESH','CURVE']:            
                row = layout.row()
                row.column().prop(obj, "location")
                row.column().prop(obj, "rotation_euler", text="Rotation")
                row.column().prop(obj, "scale")
                row = layout.row()
                row.operator("object.transform_apply", text="Apply Rot").rotation=True
                row.operator("object.transform_apply", text="Apply Scale").scale=True
                row = layout.row()
                row.column().prop(obj, "dimensions")
    
            elif objType  in ['CAMERA','LAMP']:
                row = layout.row()
                row.column().prop(obj, "location")
                row.column().prop(obj, "rotation_euler", text="Rotation")
                row.column().prop(obj, "scale")
    
            elif objType  == 'EMPTY':
                row = layout.row()
                row.column().prop(obj, "location")
                row.column().prop(obj, "rotation_euler", text="Rotation")
                row.column().prop(obj, "scale")
    
            col = layout.column()
            col.label(text="Unit System")
            col.row().prop(unit, "system", expand=True)
            col.row().prop(unit, "system_rotation", expand=True)
    

hope this is of help here or in other places to "improve" the code :)

| improve this answer | |
$\endgroup$
4
$\begingroup$

So as it seems instead of using a poll function here, best is to check for object type and then based on what type is present/selected render specialized UI parts:

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

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

        obj = context.object
        unit = context.scene.unit_settings

        if obj.type in ['MESH','CURVE']:            
            row = layout.row()
            row.column().prop(obj, "location")
            row.column().prop(obj, "rotation_euler", text="Rotation")
            row.column().prop(obj, "scale")
            row = layout.row()
            row.operator("object.transform_apply", text="Apply Rot").rotation=True
            row.operator("object.transform_apply", text="Apply Scale").scale=True
            row = layout.row()
            row.column().prop(obj, "dimensions")

        if obj.type in ['CAMERA','LAMP']:
            row = layout.row()
            row.column().prop(obj, "location")
            row.column().prop(obj, "rotation_euler", text="Rotation")
            row.column().prop(obj, "scale")

        if obj.type == 'EMPTY':
            row = layout.row()
            row.column().prop(obj, "location")
            row.column().prop(obj, "rotation_euler", text="Rotation")
            row.column().prop(obj, "scale")

        col = layout.column()
        col.label(text="Unit System")
        col.row().prop(unit, "system", expand=True)
        col.row().prop(unit, "system_rotation", expand=True)             
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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