View Full Version : Python - selecting by type and adding prefix + render layers

07 July 2012, 12:33 PM

I'm working on a script that can either select by type or use already selected array to add prefix and then store it for later(if need) to create render layer per selected object using object name...

right this sounds pretty bad and I've been strugling with it from a while now so any hints or code is very helpful ! Not that I'm asking any1 to code it for me but hints/tutorial suggestions/and code will help a lot for me to learn... anyway here is what I was fighting with...

#select all lights in scene by type'light', r=True)
# specify lit for lights selection
lit ='light', r=True)

#function to rename each light in lit selection to lit_%s
for lit in each:
Rename='lit_%s' %i

The code above works but only for 1 lights, it wont redo the loop until it finish objects in scene/selected. I did try to specify a range(30) - very high range so that it will run more times than there is lights but no help either :( it keep stopping on 1 lights arr...

I went over cmiVFX part 1/2 maya tutorial for python - hard stuff and I do try to use his teachings as hard as I can but I still lack bits and bobs here and there arr :/

Thanks, bye.

07 July 2012, 01:32 PM
That code does not look like it would work. I didn't try it so I could be wrong.
Some of you basic python needs a bit of improvement. Try to find some basic tutorials that show you how to iterate (or loop) through lists of objects. Anyway, here is my version of what I think you were trying to do:

lightShapes ='light', r=True)
for i,thisLightShape in enumerate(lightShapes):
lightTransform = cmds.listRelatives(thisLightShape, p=True)
if lightTransform:
cmds.rename(lightTransform[0], 'lit_%i'%i)

I will not try to explain every detail, but you should look up how to use "enumerate" in the "for" loop. Also notice how I'm using "listRelatives" to find the parent (or transform) of the lightShape. Normally you would want to rename the transform and maya will rename the shape to match.

have fun

07 July 2012, 02:37 PM
Hey there

Thanks djx a lot for ur help ! I have no idea how did you come with that conclusion. The enumerate do make sense when using loops. I have to read about it a lot more, somehow I fell like maxscript was a bit easier. You want to index all items of interest and then run edit per each index item. I started working on another branch of the script for practice.

The only think that I can not figure out so far is: ',thisObjectShape', 'objectTransform[0]'(uh I got this one so nvm hehe) and why it does not give me error or the result I expect :D

#specify from which type of object or based on what selection to get the .ls data from
MeshShape = selection=True )
# specify how to store data that we found above - in enumerate order - which is just numerical order per each item
for i,thisObjectShape in enumerate(MeshShape):
#all connections to and from connections that we got from above and specifing which one we want to list- p/parents
objectTransform = cmds.listRelatives(MeshShape, p=True)
#code to use set of index items that we got from listRelatives in which case are shapes and if there are any if no it will probably be ignored?
if objectTransform:
#running rename tool

07 July 2012, 03:57 PM
instead of using enumerate, you can use:

lights ='light')
for each in lights:
# do something

Although this doesn't give you the index.

You can also try:

for i in range(len(lights)):
# do something

Don't make assumptions about your should always validate your inputs. If you are expecting Shape Nodes, make sure they are shapes.

# Filter out non-shape nodes from the active selection.
MeshShape =
MeshShape =, type='mesh')


# Filter out non-shape nodes from the active selection.
MeshShape =
MeshShape = cmds.filterExpand(MeshShape, sm=12)

CGTalk Moderation
07 July 2012, 03:57 PM
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.