What is probably happening is that the example assumes where will be a wildcard * character in the name to replace. If there is not, such as name == "name", then it will always resolve to: "name" (nothing to replace). So each time it does the rename to the same thing, maya is incrementing the name to something unique for you.
Also, in your modified version, you are initializing your counter in the same scope and resetting it each time. You probably wanted: num=1 for s in sel: newName = name + str(num) s.rename(newName) num+=1 On Wed, Aug 8, 2012 at 10:33 AM, j00ey <[email protected]> wrote: > ps this is what I've come up with, seems to work, though it's a bit more > long winded than your version > > def renamer(sel=None): > sel = sel or ls(sl=1) > if not sel: > informBox('ERROR','Nothing selected','OK') > return > else: > name = promptBox('Renamer','Enter new name','OK','Cancel') > if not name: > return > else: > for s in sel: > num=1 > newName = name + str(num) > s.rename(newName) > num+=1 > > On Wednesday, August 8, 2012 1:20:38 AM UTC-5, Geordie Martinez wrote: > >> whoops change this line of code: >> >> FROM: >> name = promptForName() >> TO: >> name = pm.promptBox('Enter Naming Scheme', "Enter:", "Okay" , "Cancel") >> >> On Tue, Aug 7, 2012 at 11:18 PM, Geordie Martinez >> <[email protected]>wrote: >> >>> Here is a pymel version of what you're trying to do. Maybe this can help >>> you. >>> >>> import pymel.core as pm >>> def renameChain(items=None): >>> """renames a joint chain based on asterisk as a wildCard """ >>> items = items or pm.selected() >>> name = promptForName() >>> if not name: >>> return >>> >>> num = 1 >>> for item in items: >>> newName = name.replace("*","%s"%num) >>> pm.rename(item, newName) >>> num += 1 >>> >>> >>> On Tue, Aug 7, 2012 at 3:48 PM, j00ey <[email protected]> wrote: >>> >>>> Hi >>>> >>>> I'm learning some python, coming from a MEL background [though I'm no >>>> expert in that either] and I'm trying to write a simple script to rename a >>>> number of objects according to a user input name. I've got it to work but I >>>> want it to test if there is no user input and if not [and the user didn't >>>> press the cancel button] loop back and re-prompt for input. >>>> >>>> I was thinking something along the lines of this : >>>> 1 - declare a procedure to do the renaming >>>> 2 - prompt dialog >>>> 3 - if the input is valid run the procedure, if not pop up a message >>>> and ask whether to retry or cancel >>>> 4 - if retry, run the procedure again >>>> >>>> but I came unstuck and I can't find anything to do with declaring a >>>> procedure in pymel. Can anyone help? Here's what I've written so far. >>>> Thanks in advance >>>> >>>> ##############################**##############################** >>>> ##############################**#################### >>>> >>>> #get selection >>>> >>>> sel=ls(sl=1) >>>> >>>> #get user input >>>> input = promptDialog(t='New >>>> Name',b=['OK','Cancel'],db='**OK',cb='Cancel',ds='no >>>> input') >>>> >>>> #set base number to 1 >>>> >>>> x=1 >>>> >>>> #test user input and if not empty string, rename selected objects >>>> >>>> if input=='OK': >>>> name=promptDialog(q=1, t=1) >>>> if len(name) != 0: >>>> for s in sel: >>>> s.rename(name + str(x)) >>>> x+=1 >>>> else: >>>> #if user has not input a valid string prompt to retry >>>> message = confirmDialog(message ='Please enter a >>>> name',b=['Retry','Cancel'],db=**'Retry',cb='Cancel',ds='**Cancel') >>>> >>>> ##############################**##############################** >>>> ##############################**#################### >>>> >>>> -- >>>> view archives: >>>> http://groups.google.com/**group/python_inside_maya<http://groups.google.com/group/python_inside_maya> >>>> change your subscription settings: http://groups.google.com/** >>>> group/python_inside_maya/**subscribe<http://groups.google.com/group/python_inside_maya/subscribe> >>>> >>> >>> >> -- > view archives: http://groups.google.com/group/python_inside_maya > change your subscription settings: > http://groups.google.com/group/python_inside_maya/subscribe > -- view archives: http://groups.google.com/group/python_inside_maya change your subscription settings: http://groups.google.com/group/python_inside_maya/subscribe
