Change layer visbility through script

How to chage the layer visibility (or any other property) through Python script

import maya.cmds as cmds
layers = cmds.ls(long=True, 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 = cmds.ls(long=True, type='displayLayer')
	for l in layers[1:]:														
		cmds.setAttr( '%s.visibility' % l, value)



setLayersVisibility(0)
 
If you want to get it to pretty much the same as the mel command (setLayerTo)
 
import maya.cmds as cmds
def setLayerTo(attrib, value, selected=False):
	layers = cmds.ls(long=True, 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)

Leave a Reply

Your email address will not be published. Required fields are marked *