简体   繁体   中英

Using maya python api - how do I change selected object name into upper case?

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.

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.

“改变”字符串的正确方法是使用切片和串联通过从旧字符串的某些部分进行复制来构建新字符串。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM