Advertisement
iszotic

Change_Back_Directory

May 14th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. bl_info = {
  2.     "version": (1, 3),
  3.     "blender": (2, 68, 0),
  4.     "name": "Move Backup Save Files To Directory",
  5.     "author": "Jonathan Stroem",
  6.     "description": """Move the backup save files to a directory instead of the Blender-file's current directory. A new folder called "backup.blend" will be created and all save-files will be moved there instead of the default folder.""" ,
  7.     "category": "System",
  8.     "location": """The backup folder will be named "backup.blend". Under 'File > User Preferences > File > Save Versions', you can change how many backups that'll be used.""",
  9.     }
  10.  
  11. #IMPORTS
  12. import bpy, os
  13. import shutil
  14. from bpy.app.handlers import persistent
  15.  
  16. import time
  17. from stat import *
  18.  
  19. #When enabling the addon.
  20. def register():
  21.     bpy.app.handlers.save_post.append(move_handler)
  22.  
  23. #When disabling the addon.
  24. def unregister():
  25.     bpy.app.handlers.save_post.remove(move_handler)
  26.  
  27. #So that Blender recognize it as an addon.
  28. if __name__ == "__main__":
  29.     register()
  30.  
  31. @persistent
  32. def move_handler(dummy):
  33.  
  34.     #fullpath = Full path to the data file.
  35.     #directory = Directory the data file is in.
  36.     #name = Name of file, without extension.
  37.     #extension = Extension of files.
  38.     #backup_amount = Max amount of backups to store.
  39.     #backup_directory = The name of the backup directory. (Where the files should be moved.) So change this if you want to change where the backups are saved.
  40.  
  41.     file = {"fullpath":bpy.data.filepath, \
  42.             "directory":"/tmp", \
  43.             "name":bpy.path.display_name_from_filepath(bpy.data.filepath), \
  44.             "extension":".blend", \
  45.             "backup_amount":bpy.context.user_preferences.filepaths.save_version, \
  46.             "backup_directory":"backup.blend" \
  47.             }
  48.  
  49.     #Create backup directory if it doesn't exist.
  50.     if file["backup_directory"] not in os.listdir(file["directory"]) :
  51.         os.chdir(file["directory"])
  52.         os.mkdir(file["backup_directory"])
  53.  
  54.     #Get current save files from the backup directory.
  55.     currentFiles = []
  56.     for f in [ f for f in os.listdir(file["directory"] + "/" + file["backup_directory"]) for c in range(1, int(file["backup_amount"]) + 1) if f == file["name"] + file["extension"] + str(c) if os.path.isfile(os.path.join(file["directory"] + "/" + file["backup_directory"], f)) ] :
  57.         currentFiles.append(f)
  58.  
  59.     #All this is moving the correct file.
  60.     if len(currentFiles) < 1 : #If no files, then no need to check.
  61.         if os.path.isfile(file["fullpath"] + "1") :
  62.             shutil.move(file["fullpath"] + "1", file["directory"] + "/" + file["backup_directory"] + "/" + file["name"] + ".blend1")
  63.     else : #If the max backup amount has been reached, then check for the oldest file and overwrite that one.
  64.         if file["backup_amount"] <= len(currentFiles) :
  65.             replaceFile = {"modified_date":None, \
  66.                            "fullname":""}
  67.             for f in currentFiles :
  68.                 stats = os.stat(file["directory"] + "/" + file["backup_directory"] + "/" + f) #Get attributes from a file.
  69.                 if replaceFile["fullname"] == "" : #This will happen only the first time.
  70.                     replaceFile["fullname"] = f
  71.                     replaceFile["modified_date"] = time.asctime(time.localtime(stats[ST_MTIME]))
  72.                 else : # Is the previous file older or newer? If it's older, then you'd want to overwrite that one instead. Go through all backup-files.
  73.                     temp_modified = time.asctime(time.localtime(stats[ST_MTIME]))
  74.                     if replaceFile["modified_date"] > temp_modified :
  75.                         replaceFile["fullname"] = f
  76.                         replaceFile["modified_date"] = temp_modified
  77.  
  78.             #When the loop is finished, the oldest file has been found, and will be overwritten.
  79.             shutil.move(file["fullpath"] + "1", file["directory"] + "/" + file["backup_directory"] + "/" + replaceFile["fullname"])
  80.         else : #If the max backup amount hasn't been reached, and the folder isn't empty.
  81.             #Then check for the next number, and then just move the file over with the correct number.
  82.             replaceFile = "" #Location.
  83.             for f in currentFiles :
  84.                 for c in range(1, int(file["backup_amount"]) + 1) :
  85.                     if not os.path.isfile(file["directory"] + "/" + file["backup_directory"] + "/" + file["name"] + file["extension"] + str(c)) :
  86.                         shutil.move(file["fullpath"] + "1", file["directory"] + "/" + file["backup_directory"] + "/" + file["name"] + file["extension"] + str(c))
  87.                         replaceFile = f
  88.                         break
  89.                 if replaceFile != "" : #File to replace has been found, break out.
  90.                     break
  91.  
  92.  
  93. #This document is licensed according to GNU Global Public License v3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement