Rename selected objects (ultra-noob)

2,496 views
Skip to first unread message

are...@gmail.com

unread,
Jan 5, 2014, 5:45:16 PM1/5/14
to python_in...@googlegroups.com
Hey All,

I am just getting started out trying to understand Python (and programming in general). I'm trying to make a simple script that renames all my selected objects. Obviously this should be super simple, but when I run what I have so far, it only renames one object regardless of what is selected.

import maya.cmds as cmds

def renameFun ():

mySel = cmds.ls (selection = True)
newName = "Test_Name"

for i in range (len(mySel)):
cmds.rename(newName + "_%d" % i)

I am calling the function when I run it in Maya. Any help would be very appreciated!

Aren

aevar.gu...@gmail.com

unread,
Jan 5, 2014, 5:57:30 PM1/5/14
to python_in...@googlegroups.com
Try this:

import maya.cmds as cmds

def renameFun ():

    for i in xrange ( len ( cmds.ls (selection = True) ) + 1 ):
        cmds.rename("Test_Name_%s" % i)

    return True

# Python starts counting at zero so you will miss the last object without adding a one
# range has been proven to show consistent memory drains, use xrange instead
# That’s a string, not a digit you are adding into the name
# Not sure why it would only ever rename one item, but hope this helps
# Always return at least True, according to PEP8, but if you are new to python don’t worry about that just yet.

Justin Israel

unread,
Jan 5, 2014, 6:12:12 PM1/5/14
to python_in...@googlegroups.com
If your goal is to rename each item in the selection to an incrementally new name, you probably want to specify each original name as you do it:

import maya.cmds as cmds

def renameFun ():

    mySel = cmds.ls(selection=True)
    newName = "Test_Name"

    for i, orig in enumerate(mySel):
        cmds.rename(orig, "%s_%d" % (newName, i))

"enumerate" is convenient way of automatically getting an incremented counter in addition to each item of your list. It would be equivalent do doing something like:

i = 0
for orig in mySel:
    ...
    i += 1




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/52c9e4c4.08eac20a.6b33.ffffbebf%40mx.google.com.

For more options, visit https://groups.google.com/groups/opt_out.

Aren Voorhees

unread,
Jan 5, 2014, 9:36:51 PM1/5/14
to python_in...@googlegroups.com
Thank you both for your reply - I was able to get it working.  I'm glad you mentioned enumerate - I had heard of it, but not thought to use it in this way.

860323

unread,
Jan 5, 2014, 10:26:08 PM1/5/14
to python_in...@googlegroups.com
Hi;
for i in range (len(mySel)):
cmds.rename(mySel[i],newName + "_%d" % i)

I think you didn't define the object you need to rename, mySel[i] is you need rename object!


Sent from my iPhone
> --
> You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3de1aeeb-93d6-4d26-a15d-d94e0d43eb0a%40googlegroups.com.

Aren Voorhees

unread,
Jan 6, 2014, 10:06:51 AM1/6/14
to python_in...@googlegroups.com
EagleZ, you're right!  It works now...now I understand why my original attempt wasn't working.  Thanks!


On Sunday, January 5, 2014 4:45:16 PM UTC-6, Aren Voorhees wrote:
Reply all
Reply to author
Forward
0 new messages