View Full Version : Display Layer Script?

07 July 2012, 01:22 PM
Hey guys,

right now i'm working with huge scenes and references
and all of them got dozens of display layes.

Is there any hack or script for saving the display layers?
In other words, i would like to save the combination of my 150 Display Layers.
( Which are on and off )

Really appreciate your help guys.



P.s Sorry for the double post ;-/

07 July 2012, 02:44 PM
do you mean that when you open a maya file containing references, the display layers don't remember what state they were in last (hidden, template, reference, etc.)?

07 July 2012, 03:09 PM
No not that...

Imagine you have maybe 100 Display Layers.
And now you can configure your object for different modifications with all
the Display Layers. I want something like in Photoshop which is called Layer Combs.
In order to remember which of the 100 Display Layers are on or off for certain configurations
of the object. I hope you get what i mean.

Display Layer V1 = DL 1-10 = on
DL 11-14 = off
DL 15-35 = off

Display Layer V2 = DL 1-5 = off
DL 6-15 = on

and so on...

I hope this example was clear.

cheers mate

07 July 2012, 04:12 PM
Try this (python - be careful of indentation - its often messed up by the forum software): print 'Grab Layer Visibility'
dispLayerStates = [] # Create array to store layer states
dispLayers = = 'displayLayer')# Create array of display layers
for lyr in dispLayers:
#print lyr
# Append lyr,lyrState:
dispLayerStates.append( [lyr,cmds.getAttr(lyr+'.visibility')] )

# Restore layer visibility
for lyr in dispLayerStates:
print lyr[0]
if lyr[0] != 'defaultLayer':
cmds.setAttr(lyr[0]+'.visibility',lyr[1])If you have a layer named 'defaultLayer', then you need to remove the condition - if lyr[0] != 'defaultLayer':
I don't think that was necessary prior to Maya 2013.
If you need to do this frequently, you might consider this approach:
Make a python file (ie with something like this as its content: animalLyrs = [
veggieLyrs = [

def allLayersOff ():
[cmds.setAttr(layr+'.visibility',0) for layr in = 'displayLayer')]

def setAnimalLyrs ():
[cmds.setAttr(layr+'.visibility',1) for layr in animalLyrs]

def setVeggieLyrs ():
[cmds.setAttr(layr+'.visibility',1) for layr in veggieLyrs]
Then in a python tab of the script editor use this:

import myLyrs as mL
reload (mL)


For batch rendering, I use a MEL-based approach, but it has a couple extra steps...

07 July 2012, 05:06 PM
Your are a killer Mitch!

Thanks a bunch for that buddy.
It's a shame that i'm such a noob with python.

As you have expected, i get the indentation error all the way.
I have tried several indentation combination but still the error occurs

"# Error: IndentationError: unexpected indent # "

best regards and thanks a lot for your quick help.

07 July 2012, 05:18 PM
Python is an excellent language, but it is absolutely strict about indentation.
Basically, each time you have a colon :, a new level is defined and everything on that level needs to be indented. Looking at my code, it would have tabs like this:
No tabs:
veggieLyrs = [

def setAnimalLyrs ():
TAB[cmds.setAttr(layr+'.visibility',1) for layr in animalLyrs]

print 'Grab Layer Visibility'
dispLayerStates = [] # Create array to store layer states
for lyr in dispLayers:
TAB#print lyr
TABAppend lyr,lyrState:
TABTABdispLayerStates.append( [lyr,cmds.getAttr(lyr+'.visibility')] )

07 July 2012, 08:26 PM
Thanks again for droppin in Mitch.
This was a perfect explanation.

But still i get this error

# Error: SyntaxError: invalid syntax #

Although i have deleted your # comments.

Am i missing something?

07 July 2012, 08:32 PM
sorry mate, my fault

this is the error when i execute the grab part of your script

# Error: NameError: name 'cmds' is not defined #

07 July 2012, 08:47 PM
okay, i think i have fixed it

import maya.cmds as cmds

i have used the import to bring in the cmds commands.
Now itg works like it should i think.

Thanks for this stuff mate, really appreciate it.
Now i could make different version of the script for different display layers combination right?
Then i would rename it like "GrabLayer_config_v01" and execute it
to bring back my config_v01 display layers.
Gotta test it this way, but i think it should work.
It would be cool to extend your script and maybe create a little window
where you can select the different versions and execute them by clicking the right

best regards


07 July 2012, 10:01 PM
import maya.cmds as cmds
to your file, then you don't have to use it in the script editor. If you don't have a, just make one and put it in your script dir.

As for multiple files, you don't need to do that. Attached is an easy prototype script to follow. Put it in your script path and use this code in your script editor:
import myLayers as mL
reload (mL)

To set any of the layers, just hilite that line of code in the SE and hit keypad/enter. After editing the the script, reload it by hiliting the 'reload' line and hit keypad/enter.

It would be cool to extend your script and maybe create a little window
where you can select the different versions and execute them by clicking the right
You're on your own on that one ;)

07 July 2012, 10:04 PM
If you want to test the script in the previous post, you can create the layers like this (after importing the script in the SE):
Add tabs as need be...
newLayers = [mL.animalLyrs,mL.veggieLyrs,mL.mineralLyrs]
for lyrSet in newLayers:
TABprint lyrSet
TABfor lyr in lyrSet:
TABTABcmds.createDisplayLayer (name = lyr)

07 July 2012, 10:39 AM
Thank you very much buddy for the help.
Gotta test the modifications and get back with my results.

Really appreciate your support Mitch.

Keep it up!

CGTalk Moderation
07 July 2012, 10:39 AM
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.