PDA

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


Darius1989
07-23-2012, 12:33 PM
Heya

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
cmds.ls(type='light', r=True)
# specify lit for lights selection
lit = cmds.ls(type='light', r=True)

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



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.

djx
07-23-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 = cmds.ls(type='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
David

Darius1989
07-23-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 = cmds.ls( 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
cmds.rename(objectTransform[0],'atjgf_%i'%i)

rgkovach123
07-23-2012, 03:57 PM
instead of using enumerate, you can use:

lights = cmds.ls(type='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 input...you 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 = cmds.ls(selection=True)
MeshShape = cmds.ls(MeshShape, type='mesh')

Or

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

CGTalk Moderation
07-23-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.