Function on_active_layer()?

Is there an already available function somewhere deep in bpy to check if an object is (at least) on one of the active layers of the scene?

If not, here is what I made:

def on_active_layer(scene, obj):
    for j, k in zip(scene.layers, obj.layers):
        if j & k:
            return True
    #
    return False
#

Some opinion, comment, better idea?

EDIT: I forgot to say that it works. :wink:

for a single object, that’s probably a good solution and i don’t know of a built-in replacement.

If you wanted to act on the whole scene, you could use the view3d space’s boolprops:

# assuming C is bpy.context and areas[2] is .type=='VIEW_3D'

>>> C.screen.areas[2].spaces[0].layers[:]
(False, False, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)

>>> C.screen.areas[2].spaces[0].layers_used[:]
(True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)

(objects on layer 1 and 2, visible layers 3 and 4)