agudmund

Savior 0.2.0

May 25th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. import os
  2. import re
  3. import ctypes
  4. import shutil
  5. import maya.cmds
  6.  
  7. __version__ = '0.2.0'
  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 , version=1):
  38.         """Returns a valid version number for a given path"""
  39.        
  40.         __version__ = '1.0.0'
  41.        
  42.         versions = set()
  43.         scenename , ext = os.path.splitext( name )
  44.         pattern = re.compile(r'^%s%s%s(\d{%d})%s$' % ( scenename, self.seperator, self.prefix, self.padding, ext ) )
  45.        
  46.         [versions.add(int(m.group(1))) for f in os.listdir(path) for m in [pattern.match(f)] if m]
  47.    
  48.         if versions:
  49.             version = sorted(versions)[-1] + 1
  50.            
  51.         return '%s%s' % ( self.prefix , str( version ).zfill( self.padding ) )
  52.    
  53.     def notes( self , version='' ):
  54.         """Writes out comments adjacent each version"""
  55.  
  56.         notes = maya.cmds.promptDialog(    
  57.                                             title=version,
  58.                                             ma='center',
  59.                                             sf=True,
  60.                                             message='Comments and notes?',
  61.                                             button=[ 'Ok' , 'Cancel' ] ,
  62.                                             defaultButton='Ok',
  63.                                             cancelButton='Cancel',
  64.                                             dismissString='Cancel',
  65.                                             )
  66.        
  67.         return maya.cmds.promptDialog( query=True , text=True )
  68.        
  69.  
  70.     def save( self , filerule='offlineEdit' , type='mayaBinary' , name=False ):
  71.         """Places a saved out scene on disk
  72.  
  73.             -filerule argument sets the location of the backups based off a given workspace filerule.
  74.             -Type argument can be mayaBinary or mayaAscii.
  75.             -Use name to set our scene name, if no name is given and it's the first save, the default save dialog opens up.
  76.            
  77.         """
  78.  
  79.         __version__ = '0.3.0'
  80.        
  81.         root = maya.cmds.workspace( query=True , rootDirectory=True )
  82.         filerule = maya.cmds.workspace( filerule , query=True , fre=True )
  83.         path = os.path.join( root , filerule )
  84.        
  85.         if not name:
  86.             name = ''.join( os.path.splitext( maya.cmds.file( query=True , shortName=True , sceneName=True ) )[ :-1 ] )
  87.  
  88.         name = self.clean_seperator( name )
  89.         version = self.get_version( '.'.join([name,self.extension[type]]) , path )
  90.         maya.cmds.fileInfo( 'notes' , r'%s' % self.notes( version ) )
  91.  
  92.         maya.cmds.file( rename=name )
  93.         current = maya.cmds.file( save=True , type=type )
  94.  
  95.         destination = '%s%s%s.%s' % ( os.path.join( path , name ) , self.seperator , version , self.extension[ type ] )
  96.  
  97.         ctypes.windll.kernel32.CreateHardLinkA( str(destination) , str(current) , 0 )
  98.        
  99.         maya.cmds.headsUpMessage('--[ Version %s saved ]--' % version)
  100.        
  101.         return current , destination , version
  102.  
  103. if __name__ == '__main__':
  104.     __oo__ = Backup()
  105.     current , backup , version = __oo__.save()
  106.     print ( '--[ File saved:' , current )
  107.     print ( '--[ Version:' , version )
Advertisement
Add Comment
Please, Sign In to add comment