1

I have the following code:

import maya.cmds as cmds

list = cmds.ls(sl=True)

my_list = [object.upper() for object in list]

print(my_list)

However, it doesn't change the name of the objects even though it prints out upper case names in the print statement at the end.

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
epadilla
  • 21
  • 4

3 Answers3

2

What you want is

import maya.cmds as cmds

list = cmds.ls(sl=True)
for n in list:
    cmds.rename(n, n.upper())

new_list = cmds.ls(sl=True)
print(new_list)

This is documented here with an example.

This code will rename all objects in list you can also work with whichever you have selected with cmds.select() if you want to.

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
0

The proper way to “mutate” a string is to use slicing and concatenation to build a new string by copying from parts of the old string.

Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • Thanks. For Example i have different objects selected and i want to change the name of the objects to upperCase. – epadilla Feb 10 '16 at 23:30
0

It's quite hackish, and I honestly wouldn't recommend it, but if you REALLY want to do that, you could do:

def global_var_to_upper(var):
    for _ in globals():
        if var is globals()[_]:
            globals()[str(_).upper()] = globals()[_]
            del globals()[_]
            break

abc = 123
global_var_to_upper(abc)
print(ABC) # prints 123
Goodies
  • 4,439
  • 3
  • 31
  • 57
  • 1
    Whilst I appreciate the ingenuity - I don't think that's what the OP is after (changing the actual variable name). I think they're talking about object names within `maya`. Could be wrong, of course. – J Richard Snape Feb 10 '16 at 23:35
  • I was basing this off of the "It doesn't change the name of the objects only the results" which, I assume (albeit certainly don't know), is referring to the NAME of the object. Of course, you could very well be right. OP is fairly vague. – Goodies Feb 10 '16 at 23:38
  • Yeah - based on OP comment on my answer, I've edited the Q to be more specific. TBH - I'm impressed with your trick to rename the actual object name! – J Richard Snape Feb 10 '16 at 23:40
  • Great. I'll try this. Many Thanks! – epadilla Feb 10 '16 at 23:41
  • @JRichardSnape I hope this isn't condescending, but it just creates a new reference to the original object and deletes that original one. It is a neat trick though. – Goodies Feb 10 '16 at 23:43