-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneOrganize.py
161 lines (131 loc) · 5.55 KB
/
SceneOrganize.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
''' Scene organizer
Author: Meg Coleman
Updated: June 13, 2020
A simple script to organize objects in a maya scene.
- rename: renames all of the items of a certain object type with a suffix identifying the given type
- *reorder: (Idea) to reorder all the objects in the outliner of a scene given the type
- groupItems: (WIP) combines all objects of the same type into their own group in the outliner
Other ideas:
- *reorder: reorder all the objects in the outliner of a scene given the type
- rename the materials based on the model they are applied to
- selects all textures in a scene and organizes them into folders depending on the mesh
- merge textures with UDIMs into one single image and reorganize UVs to make use of the texture
'''
from maya import cmds
class CreateWindow(object):
windowName = "Scene Organizer"
def show(self):
# check if window exists
if cmds.window(self.windowName, query=True, exists=True):
cmds.deleteUI(self.windowName)
# create window using custom name
cmds.window(self.windowName)
# build UI elements
self.buildUI()
if not cmds.window(self.windowName, query=True, exists=True):
cmds.showWindow()
def buildUI(self):
column = cmds.columnLayout(rowSpacing=10)
cmds.text(label="Rename: adds object type to end of name. (ie. sphere -> sphere_geo")
cmds.text(label="Select an object in the outliner to rename. If nothing is selected, it will rename all objects.")
cmds.text(label="Organize: reorders outliner by object type")
cmds.text(label="Group: groups objects of the same type")
# set space to put buttons on the same row
cmds.rowLayout(numberOfColumns=3)
# create buttons for UI
cmds.button(label="RENAME", command=self.rename)
cmds.button(label="ORGANIZE", command=self.reorderItems)
cmds.button(label="GROUP", command=self.groupItems)
cmds.setParent(column)
def close(self, *args):
cmds.deleteUI(self.windowName)
def rename(self, *args):
print "Starting rename."
# use selection, fall back to all scene objects
getObj = cmds.ls(selection=True, long=True, objectsOnly=True)
if not len(getObj):
getObj = cmds.ls(long=True, dag=True, objectsOnly=True)
# check if selection made
if len(getObj) == 0:
print "Error. Nothing in scene."
return
#print "objects: ", getObj
# need to set order to shortest name last otherwise it will rename the object's shape before
# the object itself
getObj.sort(key=len, reverse=True)
#print "objects ordered: ", getObj
# run through selections
for obj in getObj:
children = cmds.listRelatives(obj, children=True) or []
if len(children):
child = children[0]
objType = cmds.objectType(child)
else:
objType = cmds.objectType(obj)
if objType == 'mesh':
suffix = '_geo'
elif objType == 'camera':
suffix = '_cam'
elif 'light' in objType.lower():
suffix = '_light'
else:
continue
# set new name
shortName = obj.split('|')[-1]
newName = shortName + suffix
print shortName, '->', newName
try:
cmds.rename(obj, newName)
except RuntimeError:
print 'Renaming ' + obj + " failed."
def reorderItems(self, *args):
pass
def groupItems(self, *args):
print "Start group items."
getObj = cmds.ls(objectsOnly=True)
if not len(getObj):
getObj = cmds.ls(objectsOnly=True, dag=True)
if len(getObj) == 0:
print "Error. No objects in the outliner."
return
'''nGeo = "Geo"
nCam = "Cameras"
nLight = "Lights"
listMesh = cmds.group(empty=True, name=nGeo)
listCams = cmds.group(empty=True, name=nCam)
listLights = cmds.group(empty=True, name=nLight)'''
listMesh = []
listCams = []
listLights = []
for obj in getObj:
children = cmds.listRelatives(obj, children=True) or []
if len(children):
child = children[0]
objType = cmds.objectType(child)
else:
objType = cmds.objectType(obj)
print objType
if objType == 'mesh':
listMesh.append(obj)
#cmds.addAttr(nGeo, longName = obj, dataType = 'mesh')
elif objType == 'camera':
listCams.append(obj)
#cmds.addAttr(nCam, longName = obj, dataType = 'mesh')
elif objType.find('Light') != 0 or objType.find('light') != 0:
listLights.append(obj)
#cmds.addAttr(nLight, longName = obj)
else:
continue
#print listMesh
#emptyGroup = cmds.group(empty=True, name="Geo")
if listMesh == [] or listCams == [] or listLights == []:
print "Some groups might not have been created."
# dictionary of lists, loop over list and check length
cmds.group(listMesh, name="Geo")
cmds.group(listCams, name="Cameras")
cmds.group(listLights, name="Lights")
# put objects of the same type into their own group
# group(name="myGroup", empty=True)
if __name__ == '__main__':
window = CreateWindow()
window.show()