How can I set my scene's visible layers from Python? I'd like to make layers 0 and 4 visible. Here's what I've tried:
for i in range(20): bpy.context.scene.layers[i] = False bpy.context.scene.layers = True bpy.context.scene.layers = True
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.Sign up to join this community
My code may be awful (abeit easily readable), but I think the problem you have is this: it's impossible to turn all layers off. One must be turned on, so a loop to disable them all doesn't work. It seems that Blender is checking anytime you try to disable a layer whether it's the last man standing.
I simply turned it around by first switching the desired layers on and then disabling all others.
import bpy visible = [0,4] for b in visible: bpy.context.scene.layers[b] = 1 a = 0 while a < 20: if a not in visible: bpy.context.scene.layers[a]= 0 a += 1
edit: batFINGER reduced it to two lines, so give his comment an upvote (:
import bpy visible = [3,6,9] scene.layers = [l in visible for l in range(20)]
Creating the full list before assigning it is a smart move.
The layers visibility (for both object and scene) is a list of boolean values, you can generate a list of bools by multiplying a single bool array -
bpy.context.scene.layers = [True]+[False]*2+[True]+[False]*16
You just need to make sure the length of the list is 20.
You can also use list comprehension -
bpy.context.scene.layers = [x in [0,4] for x in range(20)]