PDA

View Full Version : Can this be done w/list comprehension?


EightBit
11-20-2011, 10:21 PM
lowRezLyrs = ['ref:Proxy_lyr', 'floor_lyr']
for layr in lowRezLyrs:
cmds.setAttr( layr+'.visibility',0)

Thanks.

NaughtyNathan
11-20-2011, 11:34 PM
of course, list comprehensions are just a way of putting a for loop on one line inside [list braces].
if it's a for loop with an operation on the element, it can be "comprehended" ;)
[cmds.setAttr(layr+'.visibility',0) for layr in lowRezLyrs]:nathaN

EightBit
11-21-2011, 03:49 AM
Thanks as always Nathan.
It was my guess that it was doable, but I couldn't figure out the syntax.

EightBit
11-22-2011, 12:59 AM
Post Removed...

EightBit
11-22-2011, 02:32 AM
It's my guess that because of the assignment statement, that this can't be done w/List Comprehension, is that correct?
renderLyrsOn = ['defaultRenderLayer','renderLayr2_rLayr']
renderLyrsOff = ['renderLayr3_rLayr']

preRndrCmd = '"'
for lyr in renderLyrsOn:
preRndrCmd += 'setAttr "'+ lyr +'.renderable" 1; '

Azrail
11-22-2011, 02:01 PM
join() is your friend :)

preRndrCmd = '; '.join(['setAttr "%s.renderable" 1' % lyr for lyr in renderLyrsOn])

zoharl
11-22-2011, 05:08 PM
I find the initial versions more readable.

EightBit
12-02-2011, 01:19 AM
Thanks Azrail:
It took me awhile to understand it.
I appreciate learning more advanced coding.

CGTalk Moderation
12-02-2011, 01:19 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.