Automate Saving Screenshots with Incremental Saves in Grasshopper

Hi everyone,

I often use incremental saves in Grasshopper, such as filename_01, filename_02, and so on. Sometimes, I need to revert to a previous version, but it can be tricky to remember what each version contains. To make it easier, I wanted to save screenshots of both the Grasshopper and Rhino canvases along with each save. The thumbnail of gh files in R8 is already useful while opening a file from inside grasshopper, but having it directly in windows as jpg’s would be useful.

Any thoughts? Obviosly I can create a python node that is triggered by button, but i’d like to be a bit more flexible

dummy code

import scriptcontext as sc
import Rhino
import Grasshopper
import os
import System.Drawing

def capture_screenshots():
    # Get the Grasshopper file path and name
    gh_file_path = Grasshopper.Instances.DocumentServer.CurrentlyOpenDocuments[0].FilePath
    gh_file_name = os.path.splitext(os.path.basename(gh_file_path))[0]
    save_directory = os.path.dirname(gh_file_path)

    # Create subdirectory for screenshots
    screenshot_directory = os.path.join(save_directory, "Screenshots")
    if not os.path.exists(screenshot_directory):
        os.makedirs(screenshot_directory)
    
    # Define file paths for screenshots
    rhino_screenshot_path = os.path.join(screenshot_directory, f"{gh_file_name}_rhino.png")
    grasshopper_screenshot_path = os.path.join(screenshot_directory, f"{gh_file_name}_grasshopper.png")
    
    # Capture Rhino screenshot
    view = sc.doc.Views.ActiveView
    if view:
        bitmap = view.CaptureToBitmap()
        bitmap.Save(rhino_screenshot_path, System.Drawing.Imaging.ImageFormat.Png)
    
    # Capture Grasshopper screenshot
    canvas = Grasshopper.Instances.DocumentEditor
    if canvas:
        rect = canvas.ClientRectangle
        bitmap = System.Drawing.Bitmap(rect.Width, rect.Height)
        canvas.DrawToBitmap(bitmap, rect)
        bitmap.Save(grasshopper_screenshot_path, System.Drawing.Imaging.ImageFormat.Png)
    
    return rhino_screenshot_path, grasshopper_screenshot_path

# Trigger the screenshot capture
if __name__ == "__main__":
    rhino_screenshot, grasshopper_screenshot = capture_screenshots()
    print(f"Saved Rhino screenshot to: {rhino_screenshot}")
    print(f"Saved Grasshopper screenshot to: {grasshopper_screenshot}")