-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartSave.py
146 lines (105 loc) · 5.96 KB
/
smartSave.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
# Author : HYUK KO | [email protected] | github.com/kohyuk91
import os
import re
import maya.cmds as mc
DEFAULT_PADDING = 3
def paddingExistsInBasename(baseName):
result = re.search(r"_v\d*.", baseName)
if result == None:
return False
return True
def paddingExistsInFiles(matchList):
matchListString = " ".join(matchList)
result = re.search(r"_v\d*.", matchListString)
if result == None:
return False
return True
def getPadding( matchList):
pattern = re.compile(r"_v\d*.")
for match in matchList:
version = pattern.findall(match)
versionStripped = version[0][2:-1]
padding = len(versionStripped)
return padding
def newSceneVersion(currentScenePath):
currentSceneDir, currentSceneFile = os.path.split(currentScenePath)
currentSceneBasename, currentSceneExt = os.path.splitext(currentSceneFile)
fileList = os.listdir(currentSceneDir)
fileListString = " ".join(fileList)
# If current scene name has no "_v#"
# e.g. currentSceneFile >> "blasterWalk.ma"
# e.g. currentSceneBasename >> "blasterWalk"
if not paddingExistsInBasename(currentSceneBasename):
# We have to check the extention because files might have same basename but different extention(e.g. ".ma", ".mb")
pattern = re.compile(r"{baseName}_v\d*{ext}".format(baseName=currentSceneBasename, ext=currentSceneExt))
matchList = pattern.findall(fileListString)
padding = getPadding(matchList) if paddingExistsInFiles(matchList) else DEFAULT_PADDING
# Get list of all "blasterWalk_v#.ma" in same directory.
# The current scene does not have "_v#",
# but there might be a file in the same directory that already follows the naming convention "blasterWalk_v###.ma"
versionList = []
for match in matchList:
try:
versionPattern = re.compile(r"_v\d{{{padding}}}.".format(padding=padding))
version = int(versionPattern.findall(match)[0][2:padding+2])
versionList.append(version)
except:
pass
# If there is no file that follows the naming convention "blasterWalk_v#.ma", save v001.
# e.g. blasterWalk_v001.ma
if len(versionList) == 0:
newSceneVersionFile = "{baseName}_v{nextVer:0{padding}d}{ext}".format(baseName=currentSceneBasename, nextVer=1, padding=padding, ext=currentSceneExt)
newSceneVersionPath = os.path.join(currentSceneDir, newSceneVersionFile)
return newSceneVersionPath
# If there is a file that follows the naming convention "blasterWalk_v#.ma", find the next available version and save.
# e.g. Files with same naming convention in directory: [blasterWalk_v001.ma, blasterWalk_v002.ma] >> Result: blasterWalk_v003.ma
# e.g. Files with same naming convention in directory: [blasterWalk_v0001.ma, blasterWalk_v0002.ma] >> Result: blasterWalk_v0003.ma
# e.g. Files with same naming convention in directory: [blasterWalk_v001.ma, blasterWalk_v003.ma] >> Result: blasterWalk_v004.ma | Skips "blasterWalk_v002.ma"
# e.g. Files with same naming convention in directory: [blasterWalk_v0001.ma, blasterWalk_v0003.ma] >> Result: blasterWalk_v0004.ma | Skips "blasterWalk_v002.ma"
maxVersion = max(versionList)
nextVersion = maxVersion + 1
newSceneVersionFile = "{baseName}_v{nextVer:0{padding}d}{ext}".format(baseName=currentSceneBasename, nextVer=nextVersion, padding=padding, ext=currentSceneExt)
newSceneVersionPath = os.path.join(currentSceneDir, newSceneVersionFile)
return newSceneVersionPath
# If current scene name has "_v#"
# e.g. currentSceneFile >> "blasterWalk_v001.ma"
# e.g. currentSceneBasename >> "blasterWalk_v001"
# Get list of all "blasterWalk_v#.ma" in same directory.
# Remove "_v#" from basename
currentSceneBasenameVersionStripped = re.sub(r"_v\d*", "", currentSceneBasename)
# We have to check the extention because files might have same basename but different extention(e.g. ".ma", ".mb")
pattern = re.compile(r"{baseName}_v\d*{ext}".format(baseName=currentSceneBasenameVersionStripped, ext=currentSceneExt))
matchList = pattern.findall(fileListString)
padding = getPadding(matchList)
versionList = []
for match in matchList:
try:
versionPattern = re.compile(r"_v\d{{{padding}}}.".format(padding=padding))
version = int(versionPattern.findall(match)[0][2:padding+2])
versionList.append(version)
except:
pass
# If there is a file that follows the naming convention "blasterWalk_v###.ma", find the next available version and save.
# e.g. Files with same naming convention in directory: [blasterWalk_v001.ma, blasterWalk_v002.ma] >> Result: blasterWalk_v003.ma
# e.g. Files with same naming convention in directory: [blasterWalk_v001.ma, blasterWalk_v003.ma] >> Result: blasterWalk_v004.ma | Skips "blasterWalk_v002.ma"
maxVersion = max(versionList)
nextVersion = maxVersion + 1
newSceneVersionFile = "{baseName}_v{nextVer:0{padding}d}{ext}".format(baseName=currentSceneBasenameVersionStripped, nextVer=nextVersion, padding=padding, ext=currentSceneExt)
newSceneVersionPath = os.path.join(currentSceneDir, newSceneVersionFile)
return newSceneVersionPath
def main():
currentScenePath = mc.file(q=1, sn=1)
if currentScenePath == "":
mc.warning("You must save a scene first.")
return
currentSceneDir, currentSceneFile = os.path.split(currentScenePath)
currentSceneBasename, currentSceneExt = os.path.splitext(currentSceneFile)
newSceneVersionPath = newSceneVersion(currentScenePath)
if currentSceneExt == ".ma": fileType = "mayaAscii"
if currentSceneExt == ".mb": fileType = "mayaBinary"
# Rename and Save
mc.file(rename=newSceneVersionPath)
mc.file(save=True, type=fileType)
print newSceneVersionPath
if __name__ == "__main__":
main()