Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import ctypes
- import shutil
- import maya.cmds
- __version__ = '0.2.0'
- class Backup( object ):
- def __init__( self , prefix='v' , seperator=' ' , padding=3 ):
- """Saves out an incremented backup file of your scene
- -Prefix sets the prefix before the versioning increment name.
- -Seperator is set to determine the naming convention seperator.
- -Padding sets the number of digits to use in the version name.
- """
- self.name = 'Saviour'
- self.prefix = prefix
- self.padding = padding
- self.seperator = seperator
- self.extension = { 'mayaBinary' : 'mb' , 'mayaAscii' : 'ma' }
- def clean_seperator( self , name ):
- """Replaces seperators within a file name"""
- __version__ = '1.0.0'
- name , ext = os.path.splitext( name )
- if ext:
- return '.'.join( [ re.sub( r'[^a-zA-Z0-9]+' , self.seperator , name ) , ext ] )
- else:
- return re.sub( r'[^a-zA-Z0-9]+' , self.seperator , name )
- def get_version( self , name , path , version=1):
- """Returns a valid version number for a given path"""
- __version__ = '1.0.0'
- versions = set()
- scenename , ext = os.path.splitext( name )
- pattern = re.compile(r'^%s%s%s(\d{%d})%s$' % ( scenename, self.seperator, self.prefix, self.padding, ext ) )
- [versions.add(int(m.group(1))) for f in os.listdir(path) for m in [pattern.match(f)] if m]
- if versions:
- version = sorted(versions)[-1] + 1
- return '%s%s' % ( self.prefix , str( version ).zfill( self.padding ) )
- def notes( self , version='' ):
- """Writes out comments adjacent each version"""
- notes = maya.cmds.promptDialog(
- title=version,
- ma='center',
- sf=True,
- message='Comments and notes?',
- button=[ 'Ok' , 'Cancel' ] ,
- defaultButton='Ok',
- cancelButton='Cancel',
- dismissString='Cancel',
- )
- return maya.cmds.promptDialog( query=True , text=True )
- def save( self , filerule='offlineEdit' , type='mayaBinary' , name=False ):
- """Places a saved out scene on disk
- -filerule argument sets the location of the backups based off a given workspace filerule.
- -Type argument can be mayaBinary or mayaAscii.
- -Use name to set our scene name, if no name is given and it's the first save, the default save dialog opens up.
- """
- __version__ = '0.3.0'
- root = maya.cmds.workspace( query=True , rootDirectory=True )
- filerule = maya.cmds.workspace( filerule , query=True , fre=True )
- path = os.path.join( root , filerule )
- if not name:
- name = ''.join( os.path.splitext( maya.cmds.file( query=True , shortName=True , sceneName=True ) )[ :-1 ] )
- name = self.clean_seperator( name )
- version = self.get_version( '.'.join([name,self.extension[type]]) , path )
- maya.cmds.fileInfo( 'notes' , r'%s' % self.notes( version ) )
- maya.cmds.file( rename=name )
- current = maya.cmds.file( save=True , type=type )
- destination = '%s%s%s.%s' % ( os.path.join( path , name ) , self.seperator , version , self.extension[ type ] )
- ctypes.windll.kernel32.CreateHardLinkA( str(destination) , str(current) , 0 )
- maya.cmds.headsUpMessage('--[ Version %s saved ]--' % version)
- return current , destination , version
- if __name__ == '__main__':
- __oo__ = Backup()
- current , backup , version = __oo__.save()
- print ( '--[ File saved:' , current )
- print ( '--[ Version:' , version )
Advertisement
Add Comment
Please, Sign In to add comment