Frage

I am trying to do a simple Maya renaming UI but I am stuck at a part - replacing he initial characters in the current naming with other characters

For example; 3 items in the Outliner(irregardless of what they are):- pCube1, - pSphere1, - nurbsSphere1

So far I am able to write up to the point where it can selects and rename 1 or more objects, see code below

objects = []
objects = cmds.ls(sl=True)
for obj in objects:
    test = []
    test = cmds.rename(obj, "pSphere" )
    print objects
    # Results: pSphere, pSphere2, pSphere3 #

However, suppose now I am selecting nurbsSphere1 and pSphere1, and I just wanted to replace the word 'Sphere' in them with 'Circle', instead of getting the results as: nurbsCircle1, pCircle1, I got a error message # TypeError: Too many objects or values. #

charReplace = "test"
if charReplace in objects:
    newName = []
    newName = cmds.rename(objects, "Circle" )

Any advices on it?

War es hilfreich?

Lösung

  1. As per the documentation rename command takes only strings as input parameters. You are providing a list named objects while trying to rename the filenames.
  2. Moreover, you are searching for string "test" in objects list. Instead you should search for string "test" in each filename which is present in objects list.
  3. rename command renames the old string with the newer one. It does not replace the substring within a string (e.g. "sphere" in "nurbsSphere"). In order to achieve this you should create the new filenames separately and then use them to rename the files.

You can try this:

charReplace = "test"
for filename in objects:
    if charReplace in filename:
        newFilename = filename.replace(charReplace, "Circle")
        cmds.rename(filename, newFilename)

I do not have Maya installed so code is not tested.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top