Is there a better way, other than looping every object, of finding out if a range of layers are empty?

import bpy

layer_range = [1,2,3,4,11,12,13,14]
layer_range_break = False
for obj in bpy.context.scene.objects:
    for layer_number in layer_range:
        if obj.layers[layer_number]:        
            print("Object on bad layer")
            layer_range_break = True

    if layer_range_break:

Using pythons's any and all

In general, code like in your question can be shortened considerably using ptython's any or all methods. any is True if any item in a list is True, and all returns True if all are True.

In this case using any to determine if any object in the scene is on a designated layer. any(o.layers[l] for l in layer_range) emulates your inner loop break.

import bpy
context = bpy.context
scene = context.scene
layer_range = [1,2,3,4,11,12,13,14]

dirty = any(any(o.layers[l] for l in layer_range)
            for o in scene.objects)        

print("Layer Range is dirty:", dirty)

PS: not sure this is "better", it is IMO more readable.


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.