changing layer visibility through script


Hi, I wrote a script on python to do a bunch of cleaning in some scenes, at the end I want to set all layers display visibility off. In MEL you can issue the following command:

setLayerTo all".visibility" 1;

setLayerTo all".visibility" 0;

but I haven’t been able to figure out how to do it in python. Anyone knows of a way to do it? or is there a way I can have my entire script in python and then just that one bit through MEL?


I guess I’ll just do a loop

layers = type='displayLayer')
for x in layers[1:]:
	cmds.setAttr( '%s.visibility' % x, 0)


what I posted above works to an extent.

the problem I came up with is that maya lets you have multiple things in a scene with the same name. Say you have an object named ‘sphere’, and you have a layer named ‘sphere’ then when the command cmds.setAttr( ‘%s.visibility’ % l, 0) is given, both, the object sphere’s visibility and the layers sphere’s visibility will be affected.

I tried to add a condition according to the type, but the setAttr() func still applied to everything with that name

layers = type='displayLayer')
for l in layers[1:]:														
	print l
	type = cmds.nodeType('%s' %l)
	print type
	if type == 'displayLayer':    # redundant, it should already be 'displayLayer'
		cmds.setAttr( '%s.visibility' % l, 0)

this is where “setLayerTo all”.visibility" 0;" would come in handy since it seems to only affect layers and doesn’t interfere with names. But I still haven’t been able to call it in python.


If you do a whatIs on setLayerTo you’ll discover it’s just a MEL script that does exactly what you’re doing anyway… i.e. setting layer attributes in a for loop.

If you do really want to call MEL scripts or commands in python you can:

import maya.mel as mel
mel.eval(“setLayerTo layers attr value”)

On the subject of names, Maya doesn’t really allow you to have objects with the same name. A node’s BASE name may look the same to you, but in reality they have unique and separate names. You should always refer to these long names in all your scripts to avoid such issues. Use the -l flag for the ls command, the -f flag for listRelatives and -fp flag for filterExpand (yeah, thanks Alias for making all the commands use different flags for the same thing! :rolleyes: )

so, for example, although you may have a layer called “sphere” and a mesh transform called “sphere”, their long names still mark them as separate and unique with their real long names: |sphere and |group1|sphere

hope that clears something up at least!


You are very, very close!

What Nathan said it 100% true… I’d say it’s 1000% true if it were possible. Always refer to objects via their long name!!! Can’t stress that enough!

So if you add the ‘long=True’ to the script you wrote and remove the condition it should work like a charm!

import maya.cmds as cmds
layers =, type='displayLayer')
for l in layers[1:]:														
	cmds.setAttr( '%s.visibility' % l, 1)

wrap it up in a nice little function

import maya.cmds as cmds
def setLayersVisibility(value):
	layers =, type='displayLayer')
	for l in layers[1:]:														
		cmds.setAttr( '%s.visibility' % l, value)


if you want to get it to pretty much the same as the mel command

import maya.cmds as cmds
def setLayerTo(attrib, value, selected=False):
	layers =, type='displayLayer')
	for l in layers[1:]:
		selectionStatus = cmds.layerButton(l, query=True, select=True)
		if(not selected or selectionStatus): 
			cmds.setAttr( '%s.%s' % (l, attrib), value)

setLayerTo('visibility', value=0, selected=True)


ahhhh long names… I didn’t know how to get a long name in maya, thats awesome, makes perfect sense now.


I added a condition to your code:

def setLayersVisibility(value):
	layers =, type='displayLayer')
	for l in layers[1:]:	
		if l.find("defaultLayer") == -1:													
			cmds.setAttr( '%s.visibility' % l, value)

I’m importing some referenced files and turning the display layers off, but everytime something would come in with scene1_defaultLayer, scene2_defaultLayer, etc… the script was turning off the sceneX_defaultLayer and I wasn’t able to turn on/off that defaultLayer through Maya’s GUI. So the condition just ignores anything with defaultLayer.

Thanks for the help!


This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.