agudmund

Saviour

May 19th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. import os
  2. import re
  3. import ctypes
  4. import shutil
  5. import maya.cmds
  6.  
  7. __version__ = '0.1.3'
  8.  
  9. class Backup( object ):
  10.     def __init__( self , prefix='v' , seperator=' ' , padding=3 ):
  11.         """Saves out an incremented backup file of your scene
  12.        
  13.             -Prefix sets the prefix before the versioning increment name.
  14.             -Seperator is set to determine the naming convention seperator.
  15.             -Padding sets the number of digits to use in the version name.
  16.            
  17.         """
  18.        
  19.         self.name = 'Saviour'
  20.         self.prefix = prefix
  21.         self.padding = padding
  22.         self.seperator = seperator
  23.         self.extension = {'mayaBinary' : 'mb'  , 'mayaAscii' : 'ma' }
  24.        
  25.     def clean_seperator( self , name ):
  26.         """Replaces seperators within a file name"""
  27.  
  28.         __version__ = '1.0.0'
  29.        
  30.         name , ext = os.path.splitext( name )
  31.        
  32.         if ext:
  33.             return '.'.join( [ re.sub( r'[^a-zA-Z0-9]+' , self.seperator , name ) , ext ] )
  34.         else:
  35.             return re.sub( r'[^a-zA-Z0-9]+' , self.seperator , name )
  36.  
  37.     def get_version( self , name , path ):
  38.         """Returns a valid version number for a given path"""
  39.  
  40.         __version__ = '0.1.0'
  41.  
  42.         format_ext = '.mb'
  43.         backups = []
  44.         for filename in os.listdir(path):
  45.             rawname , ext = os.path.splitext( filename )
  46.             if format_ext==ext:
  47.                 if filename.startswith( rawname ):
  48.                     if rawname[ -( self.padding ) ].isdigit() and rawname[ -( self.padding ) - len( self.prefix ) ].isalpha():
  49.                         if rawname[-(self.padding)-len(self.prefix):-self.padding] == self.prefix:
  50.                             backups.append( filename )
  51.        
  52.         try:
  53.             last_backup = backups[-1]
  54.         except IndexError as e:
  55.             last_backup=None
  56.  
  57.         if last_backup:
  58.             name , ext = os.path.splitext( last_backup )
  59.             p = self.prefix
  60.             L = len(self.prefix)
  61.             new = int( [ n for n in name.split( self.seperator ) if str( n )[ :L ] == ( p ) ][ -1 ][ L: ] ) + 1
  62.            
  63.             return ''.join( [ self.prefix , str( new ).zfill( self.padding ) ] )
  64.        
  65.         else:
  66.             return ''.join( [ self.prefix , '1'.zfill( self.padding ) ] )
  67.        
  68.     def save( self , filerule='offlineEdit' , type='mayaBinary' , name=False ):
  69.         """Places a saved out scene on disk
  70.  
  71.             -filerule argument sets the location of the backups based off a given workspace filerule.
  72.             -Type argument can be mayaBinary or mayaAscii.
  73.             -Use name to set our scene name, if no name is given and it's the first save, the default save dialog opens up.
  74.            
  75.         """
  76.  
  77.         __version__ = '0.1.0'
  78.        
  79.         root = maya.cmds.workspace( query=True , rootDirectory=True )
  80.         filerule = maya.cmds.workspace( filerule , query=True , fre=True )
  81.         path = os.path.join( root , filerule )
  82.        
  83.         if not name:
  84.             name = ''.join( os.path.splitext( maya.cmds.file( query=True , shortName=True , sceneName=True ) )[ :-1 ] )
  85.  
  86.         name = self.clean_seperator( name )
  87.        
  88.         maya.cmds.file( rename=name )
  89.         current = maya.cmds.file( save=True , type=type )
  90.  
  91.         version = self.get_version( name , path )            
  92.         destination = '%s%s%s.%s' % ( os.path.join( path , name ) , self.seperator , version , self.extension[ type ] )
  93.  
  94.         ctypes.windll.kernel32.CreateHardLinkA( str(destination) , str(current) , 0 )
  95.        
  96.         return current , destination , version
  97.  
  98. if __name__ == '__main__':
  99.     __oo__ = Backup()
  100.     current , backup , version = __oo__.save()
  101.     print ( '--[ File saved:' , current )
  102.     print ( '--[ Version:' , version )
Advertisement
Add Comment
Please, Sign In to add comment