Run a script when saving

I am searching for a way to launch a script right after another command.
How can I do that ?

Example : I made a python script to export a CSV file of my model. I’d like it to be launched every time I save my model.

Another question : Can I have a script launched every 10 minutes?

Thank you

See if below script helps. When the event is added it prints a message to the command line after every save. Instead of printing the text you could add your code to launch your script for the CSV export. To disable the event, just run the script again. (600 Bytes)

You might setup an Idle Event and save the time when it did run last. Whenever it fires again check if more than 10 minutes have elapsed run your code. If not exit the event immediately.


1 Like

Hi Clement,
Thank you, it works very well on a small model, but with a bigger one, it doesn’t…
It seems that the model saving takes a long time and then the script is forgotten or can’t be treated in the same process. Do you know a way to check if saving is complete before launching the script?

Hi @BaptisteC,

you could also try to save the CSV before saving the document using BeginSaveDocument. Alternatively you might run your code in the idle event, which gets continously fired after all other events are done. Note that you need to implement some checks to exit the idle event then. Maybe setup the idle event in the BeginSaveDocument event, then let the idle event remove itself once fired.

There is also this interface for the idle event which allows to run a script and define an interval:


You’re right, I’ll try.
Thank you

I set you’re script at the opening of my Rhino session. So every time I save a model, it checks if the model is correct and runs the CSV Export.

But I noticed that the export was also triggered when using other scripts. It seems that the BeginSaveDocument event is raised by other functions.
By debugging, I’ve identified that it comes from this line :


Do you know something about this ? How can I avoid it ?

I found something.
Copy-Paste is an actual Export-Import for Rhino. So the CopyToClipboard command raise the beginSaveDocument event. That makes sens.

I’ll have to find a way to identify rather I want to run the script or not when saving, with a flag or something…

No. I found that when using BeginSaveDocument event the event gets triggered twice after using CopyToClipboard. Next i tried to find the last command used with rs.LastCommandName() from the event code, but that does not seem to work. I guess the command must have been completed to appear in that list. When the first event gets fired, it still runs, so what the LastCommandName returns is the previous command before CopyToClipboard.


Below seems to work by setting up an idle event in the BeginSaveDocument event. You can check the last command name in the idle event, then decide to export your csv or not. At the end of the idle event, let the event remove itself:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def MyBeginSaveDocumentEvent(sender, e):
    print "BeginSaveDocument Event"
    # setup idle event
    Rhino.RhinoApp.Idle += MyIdleEvent

def MyIdleEvent(sender, e):
    print "Idle Event, LastCommand:", rs.LastCommandName()
    # let the idle event remove itself
    Rhino.RhinoApp.Idle -= MyIdleEvent
key = "MyBeginSaveDocumentEvent"
if scriptcontext.sticky.has_key(key):
    print "removing event:", key
    scriptcontext.doc.BeginSaveDocument -= scriptcontext.sticky[key]
    print "adding event:", key
    scriptcontext.sticky[key] = MyBeginSaveDocumentEvent
    scriptcontext.doc.BeginSaveDocument += MyBeginSaveDocumentEvent