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 = cmds.ls( type='displayLayer')
for l in layers[1:]:
type = cmds.nodeType('%s' %l)
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’treally 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
layers = cmds.ls(long=True, 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.