Backup selected geometry to newly created layer

Hi,

I was looking for a way to save selected geometry every now and then to a separate ‘backup’ layer. I couldn’t find any solutions online → five minutes of ChatGPT, et voilà:

import rhinoscriptsyntax as rs
import datetime

def copy_to_new_layer():
    # Get the selected geometry
    selected_objects = rs.SelectedObjects()
    
    if not selected_objects:
        print "No objects selected. Please select some geometry first."
        return
    
    # Check if the "backup" layer exists, create it if not
    backup_layer_name = "_Backup"
    if not rs.IsLayer(backup_layer_name):
        rs.AddLayer(backup_layer_name)
    
    # Create a new layer with the current date and time as the name
    current_date_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    new_layer_name = backup_layer_name + "::" + "BackupCopy_" + current_date_time
    rs.AddLayer(new_layer_name)
    
    # Set the visibility of the new layer to False (hide it)
    rs.LayerVisible(new_layer_name, False)
    
    # Copy the selected geometry to the new layer
    copied_objects = rs.CopyObjects(selected_objects)
    
    # Change the layer of the copied objects to the new layer
    rs.ObjectLayer(copied_objects, new_layer_name)
    
    print "Objects copied to layer:", new_layer_name

# Run the function
copy_to_new_layer()

Perhaps it will be useful for some of you as well.

-Kevin