Skip to content

Instantly share code, notes, and snippets.

@mxamber
Last active January 9, 2016 15:03
Show Gist options
  • Save mxamber/6c328f2d1f9c9d701967 to your computer and use it in GitHub Desktop.
Save mxamber/6c328f2d1f9c9d701967 to your computer and use it in GitHub Desktop.
This script adds a "backup your work"-button to the object properties. When clicked, the object will be copied to layer 20 and renamed <object name>_Backup_<date and time>
import bpy
import datetime
class BackupPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Backup"
bl_idname = "OBJECT_PT_backup"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text="Backup your work", icon='OBJECT_DATA')
row = layout.row()
row.label(text="Active object is: " + obj.name)
row = layout.row()
row = layout.row()
row.operator("backup.backup")
class BackupButton(bpy.types.Operator):
bl_idname = "backup.backup"
bl_label = "Backup your object"
def execute(self, context):
now = datetime.datetime.now()
try:
backup(bpy.context.object)
except:
print("Error" + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second))
return{'FINISHED'}
def register():
bpy.utils.register_class(BackupPanel)
bpy.utils.register_class(BackupButton)
def unregister():
bpy.utils.unregister_class(BackupPanel)
bpy.utils.unregister_class(BackupButton)
def backup(obj):
now = datetime.datetime.now()
new_name = obj.name + "_Backup_" + str(now.year) + "-" + str(now.month) + "-" + str(now.day) + "-T-" + str(now.hour) + "-" + str(now.minute) + "-" + str(now.second)
obj.select = True
bpy.ops.object.duplicate(linked=False)
obj.select = False
dupl_obj = bpy.context.selected_objects[0]
dupl_obj.name = new_name
dupl_obj.select = True
bpy.ops.object.move_to_layer(layers=(False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True))
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment