2
$\begingroup$

I want to check if the active object is a mesh before I UV unwrap it. Here's my code and for some reason the execute runs even when a mesh is not selected. What am I missing?

@classmethod
def poll(cls, context):
    our_case = False
    selected = context.selected_objects
    object = context.active_object
    if object is None: return False
    if object.mode == "OBJECT" and all(obj.type == "MESH" for obj in selected):
        return True 
    return our_case

def execute(self, context):                  

    bpy.ops.uv.smart_project(angle_limit=30, island_margin=0.06)
    return {'FINISHED'}
$\endgroup$
2
  • $\begingroup$ because 'all()' returns true if the iterable is empty? $\endgroup$
    – lemon
    Commented Aug 25, 2019 at 11:19
  • 1
    $\begingroup$ additionally, an object can be active despite it is not selected $\endgroup$
    – lemon
    Commented Aug 25, 2019 at 11:25

1 Answer 1

3
$\begingroup$

Here's what I ended up with that worked:

@classmethod
def poll(cls, context):
    obj = context.active_object
    objs = context.selected_objects
    if len(objs) == 0: return False
    if obj.type == 'MESH': return True
    return False
$\endgroup$
1
  • 1
    $\begingroup$ Why the variable when using the reference one time? $\endgroup$
    – brockmann
    Commented Feb 14, 2020 at 9:31

You must log in to answer this question.

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