Last active
August 29, 2015 14:16
-
-
Save atvKumar/142607fe006e9d380a1a to your computer and use it in GitHub Desktop.
Increment and Save in Maya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.cmds as cmds | |
from os.path import join as joinpath, split as splitpath | |
class FileNotExist(IOError): | |
pass | |
def hasNumbers(inputString): | |
return any(char.isdigit() for char in inputString) | |
def saveAs(new, type='mayaBinary'): | |
cmds.file(rename=new) | |
cmds.file(f=True, save=True, op='v=1', type=type) | |
def extractNumbers(inputString): | |
result = dict() | |
for i, char in enumerate(inputString): | |
if char.isdigit(): | |
result.update({i:char}) | |
return result | |
def splitNumbers(inputDict): | |
number_position_list = sorted(inputDict.keys()) | |
result = [] | |
temp = '' | |
for i, num in enumerate(number_position_list): | |
if i < len(number_position_list)-1: | |
if number_position_list[i+1] == num+1: | |
temp += inputDict[num] | |
else: | |
result.append(inputDict[num]) | |
else: | |
if number_position_list[i-1] == num-1: | |
temp += inputDict[num] | |
result.append(temp) | |
return result | |
def saveIncrement(ignore=True, major=False, minor=True): | |
ftype = 'mayaBinary' | |
if cmds.file(q=True, ex=True): | |
fpath, fname = splitpath(cmds.file(q=True, sn=True)) | |
ext = fname.split('.')[1] | |
if ext == 'mb': | |
ftype = 'mayaBinary' | |
elif ext == 'ma': | |
ftype = 'mayaAscii' | |
if hasNumbers(fname): | |
numStrList = splitNumbers(extractNumbers(fname)) | |
if len(numStrList) > 1: | |
if major and not minor: | |
mj = numStrList[0] | |
new_filename = fname.replace(mj, str(int(mj)+1).zfill(len(mj)), 1) | |
saveAs(joinpath(fpath, new_filename), ftype) | |
elif not major and minor: | |
mi = numStrList[-1] | |
new_filename = fname.replace(mi, str(int(mi)+1).zfill(len(mi)), 1) | |
saveAs(joinpath(fpath, new_filename), ftype) | |
elif major and minor: | |
mj = numStrList[0] | |
mi = numStrList[-1] | |
new_filename = fname.replace(mj, str(int(mj)+1).zfill(len(mj)), 1) | |
new_filename = new_filename.replace(mi, str(int(mi)+1).zfill(len(mi)), 1) | |
saveAs(joinpath(fpath, new_filename), ftype) | |
else: | |
new_filename = fname.split('.')[0]+'_v001.' + fname.split('.')[1] | |
saveAs(joinpath(fpath, new_filename), ftype) | |
elif ignore: | |
saveAs('untitled_v001', ftype) | |
else: | |
raise FileNotExist("Please save once!!!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment