Make Layers invisible through python! Help!

I want to make any layer that has a specific object named “cube.001” visible,

while at the same time toggle all the layers that don’t have “cube.001” invisible.

how do i do this? it is driving me bonkers!

You could do it this way:


obj = bpy.data.objects['cube.001']
cubeLayer = [i for i in range(len(obj.layers)) if obj.layers[i] == True]
for i in range(len(C.scene.layers)):
     if i not in cubeLayer:
         C.scene.layers[i] = False

This way will hide any layer without cube.001.

Ok! that looks! I could just move the object to whatever layer and then run that command…

one problem, the :

for i in range(len(C.scene.layers))

console is saying ‘C’ is not defined and it is erroring out.

Sorry,


obj = bpy.data.objects['cube.001']
cubeLayer = [i for i in range(len(obj.layers)) if obj.layers[i] == True]
for i in range(len(bpy.context.scene.layers)):
     if i not in cubeLayer:
         bpy.context.scene.layers[i] = False
     else:
         bpy.context.scene.layers[i] = True

You shouldn’t have to move the object with that.

OK wow that works, you are a genius.
One problem is that that the previous visible layer is now also selected. so basically ------->

bpy.data.scenes[‘Scene’].layers[2]

and then I run the code to detect the cube.001 on say…

bpy.data.scenes[‘Scene’].layers[6]

then the both 2 and 6 remain selected. It’s a total pain that I have been trying to automate for two days now.

Try this:


obj = bpy.data.objects['cube.001']
activeLayer = bpy.context.scene.active_layer
cubeLayer = [i for i in range(len(obj.layers)) if obj.layers[i] == True]
for i in range(len(bpy.context.scene.layers)):
     if i not in cubeLayer:
         bpy.context.scene.layers[i] = False
     else:
         bpy.context.scene.layers[i] = True
if activeLayer != cubeLayer:
     bpy.context.scene.layers[activeLayer] = False

Hmm seems like active_layer isn’t in the console commands. I see what you are doing. Its says the scene has no attribute “active _layer”

“Yeah, well, some people meditate, some people get massages. I blend.” -Will Smith in Enemy of the State

I laughed so hard when I saw this in the movie, everyone probably thought I was nuts lol.

should be there, make sure that you don’t have any typos

HAhaha :slight_smile: saw the same. Sqealed.

Yea its in there.

wow you are a genious! Sorry I was working with blender 2.61- that was my problem. I need to install new version.

THANKS! <3 <3 <3 <3 <3<3 <3<3 <3<3

I hope it’s not too much trouble, but I have another question:

How do I make it so it can support multiple layers? For example if cube.001 is on both layers 1 and 3 I need those to be visible. Right now it is binary right? so THIS layer and no other layer.

No, it builds a list of all the layers that cube.001 is on, then hides the layers not in the list. But you want to change:


if activeLayer != cubeLayer:

to:


if activeLayer not in cubeLayer:

… so if cube is on the previously active layer, it won’t get hidden(should have been that way to begin with).

ooh Thx I see. != means make a list. I thought == meant equal to, and != mean not equal to.

The part that makes the list is:


cubeLayer = [i for i in range(len(obj.layer)) if obj.layers[i] == True]

!= does mean not equal to. But it needs to be changed because its comparing one integer to a list, which will always false. ‘not in’ checks if that integer is not in the list.