Run a script when saving

script
event_handling
autorun

#1

Hi,
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


#2

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.

EventEndSaveDocument.py (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.

_
c.


#3

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?


#4

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:

_
c.


#5

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


#6

Hi,
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 :

rs.Command("_CopyToClipboard") 

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


#7

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…


#8

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.

_
c.


#9

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]
    scriptcontext.sticky.Remove(key)
else:
    print "adding event:", key
    scriptcontext.sticky[key] = MyBeginSaveDocumentEvent
    scriptcontext.doc.BeginSaveDocument += MyBeginSaveDocumentEvent

_
c.