Skip to content

Instantly share code, notes, and snippets.

@show0k
Last active October 5, 2016 22:06
Show Gist options
  • Save show0k/03c4a87282279669aed0425ec84f5e19 to your computer and use it in GitHub Desktop.
Save show0k/03c4a87282279669aed0425ec84f5e19 to your computer and use it in GitHub Desktop.
Custom incremental backup for maya. It creates an OLD directory and copy the working file in this directory and increment it version number.
import os
import re
import glob
from shutil import copyfile
import maya.cmds as cmds
BACKUP_NAME = "OLD"
full_filename_uri = cmds.file(save=True, type='mayaAscii')
filename = os.path.basename(full_filename_uri)
base, extension = os.path.splitext(filename)
working_dir = os.path.dirname(full_filename_uri)
backup_dir = os.path.join(working_dir, BACKUP_NAME)
# Create a BACKUP_NAME directory if it doesn't exist
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
print "Directory %s did not exist, creating..."% (backup_dir)
backuped_files = glob.glob(os.path.join(backup_dir, base) + "_v*" + extension)
if not backuped_files:
print "No backuped files found"
# Create first file if it does not exist
copyfile(full_filename_uri, os.path.join(
backup_dir, base + '_v1' + extension))
else:
print "Found files", backuped_files
# Sort file by the integer name to find the last file
sorted_backuped_files = sorted(
backuped_files, key=lambda x: int(re.findall("(?<=v)\d+", x)[0]))
last_file = os.path.basename(sorted_backuped_files[-1])
last_integer = int(last_file.split("_v")[1].split(extension)[0])
new_file = "%s_v%i%s" % (os.path.join(
backup_dir, base), last_integer + 1, extension)
print "Create new file", new_file
copyfile(full_filename_uri, new_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment