nukescripts.rollingAutoSave

Package nukescripts :: Module rollingAutoSave |
[hide private]
[frames] | no frames]

# Source Code for Module nukescripts.rollingAutoSave

     1  import nuke
     2  import glob
     3  import time
     4  import os
     5
     6  ### Example that implements a rolling autosave using the autoSaveFilter callbacks
     7  ###
     8  ## autosaves roll from 0-9 eg myfile.autosave, myfile.autosave1, myfile.autosave2...
     9  #
    10  ## To use just add 'import nukescripts.autosave' in your init.py
    11
    12
    13 -def onAutoSave(filename):
    14
    15    ## ignore untiled autosave
    16    if nuke.root().name() == 'Root':
    17      return filename
    18
    19    fileNo = 0
    20    files = getAutoSaveFiles(filename)
    21
    22    if len(files) > 0 :
    23      lastFile = files[-1]
    24      # get the last file number
    25
    26      if len(lastFile) > 0:
    27        try:
    28          fileNo = int(lastFile[-1:])
    29        except:
    30          pass
    31
    32        fileNo = fileNo + 1
    33
    34    if ( fileNo > 9 ):
    35      fileNo = 0
    36
    37    if ( fileNo != 0 ):
    38      filename = filename + str(fileNo)
    39
    40    return filename
    41
    42
    43 -def onAutoSaveRestore(filename):
    44
    45    files = getAutoSaveFiles(filename)
    46
    47    if len(files) > 0:
    48      filename = files[-1]
    49
    50    return filename
    51
    52 -def onAutoSaveDelete(filename):
    53
    54    ## only delete untiled autosave
    55    if nuke.root().name() == 'Root':
    56      return filename
    57
    58    # return None here to not delete auto save file
    59    return None
    60
    61
    62 -def getAutoSaveFiles(filename):
    63    date_file_list = []
    64    files = glob.glob(filename + '[1-9]')
    65    files.extend( glob.glob(filename) )
    66
    67    for file in files:
    68        # retrieves the stats for the current file as a tuple
    69        # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
    70        # the tuple element mtime at index 8 is the last-modified-date
    71        stats = os.stat(file)
    72        # create tuple (year yyyy, month(1-12), day(1-31), hour(0-23), minute(0-59), second(0-59),
    73        # weekday(0-6, 0 is monday), Julian day(1-366), daylight flag(-1,0 or 1)) from seconds since epoch
    74        # note:  this tuple can be sorted properly by date and time
    75        lastmod_date = time.localtime(stats[8])
    76        #print image_file, lastmod_date   # test
    77        # create list of tuples ready for sorting by date
    78        date_file_tuple = lastmod_date, file
    79        date_file_list.append(date_file_tuple)
    80
    81    date_file_list.sort()
    82    return [ filename for _, filename in date_file_list ]
    83
    84
    85  nuke.addAutoSaveFilter( onAutoSave )
    86  nuke.addAutoSaveRestoreFilter( onAutoSaveRestore )
    87  nuke.addAutoSaveDeleteFilter( onAutoSaveDelete )
    88
    89  ### As an example to remove the callbacks use this code
    90  #nuke.removeAutoSaveFilter( onAutoSave )
    91  #nuke.removeAutoSaveRestoreFilter( onAutoSaveRestore )
    92  #nuke.removeAutoSaveDeleteFilter( onAutoSaveDelete )
    93