1
$\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$
  • $\begingroup$ because 'all()' returns true if the iterable is empty? $\endgroup$ – lemon Aug 25 '19 at 11:19
  • 1
    $\begingroup$ additionally, an object can be active despite it is not selected $\endgroup$ – lemon Aug 25 '19 at 11:25
1
$\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
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Why the variable when using the reference one time? $\endgroup$ – brockmann Feb 14 at 9:31

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.