/
ObjectRenamer.py
67 lines (47 loc) · 1.69 KB
/
ObjectRenamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#object Renamer
from maya import cmds
#dictionary
SUFFIXES = {
"mesh" : "geo",
"joint" : "jnt",
"camera" : None,
"ambientLight" : "lgt"
}
DEFAULT_SUFFIX = "grp"
def rename(selection = False):
"""
This function will rename any objects to have the correct suffix
Args:
selection: Whether or not we use the current selection
Return:
A list of all the objects we operated on
"""
objects = cmds.ls(selection = selection, dag = True, long = True) # ls return object name?
# This fuction runs if selection is true, but object is false
if selection and not objects:
raise RuntimeError("You have not select anything")
objects.sort(key = len, reverse = True)
# This function prints all separate and print the last one
for obj in objects:
shortName = obj.split("|")[-1]
# get list, but get empty list if it is none
children = cmds.listRelatives(obj, children = True, fullPath = True) or []
# find the type of elements
if len(children) == 1:
child = children[0]
objType = cmds.objectType(child)
else:
objType = cmds.objectType(obj)
#find all suffixes through dictionary
suffix = SUFFIXES.get(objType, DEFAULT_SUFFIX)
#camera exception
if not suffix:
continue
# if suffix exists already, skip renaming
if obj.endswith("_" + suffix):
continue
newName = "%s_%s" %(shortName, suffix)
cmds.rename(obj, newName)
index = objects.index(obj)
objects[index] = obj.replace(shortName, new)
return objects