Python event handling

@clement
It’s been a while, sorry about the very late reply and thank you alot for your helpful answer! I took your advise on setting a global flag and have atleast started considering undos, new and closed documents etc. There are probably a bunch of things to improve still, but atleast it seems to work out decently by now. Thank you!

@AndersDeleuran
Finally found the time to get this working, atleast on an acceptable level. Code below if you’re interested!

@piac
So far I’m only using the component for experimental purposes, so I don’t think it will cause any harm as of now. I’ll remember your feedback if i’ll release it. Thanks!

if 'events' not in sc.sticky:
    sc.sticky['events'] = {}
sc.sticky['flag'] = False

def subscribe_to(event, func, key):
    if key not in sc.sticky['events']:
        sc.sticky['events'][key] = event
    if key not in sc.sticky:
        sc.sticky[key] = func
        event += sc.sticky[key]

def unsubscribe_all():
    for key in sc.sticky['events']:
        if key in sc.sticky:
            sc.sticky['events'][key] -= sc.sticky[key]
            sc.sticky.Remove(key)
    sc.sticky['events'] = {}

# Set global flag to 'True' when event is fired
def event1(sender, e):
    sc.sticky['flag'] = True

# ExpireSolution if global flag is 'True' and set it back to 'False'
def event2(sender, e):
    if sc.sticky['flag'] == True:
        ghenv.Component.ExpireSolution(True)
        sc.sticky['flag'] = False

def event3(sender, e):
    unsubscribe_all()

subscribe_to(doc.BeforeTransformObjects, event1, 'BeforeTransformObjects')
subscribe_to(Rhino.RhinoDoc.DeleteRhinoObject, event1, 'DeleteRhinoObject')
subscribe_to(Rhino.RhinoApp.Idle, event2, 'Idle')
subscribe_to(Rhino.RhinoDoc.CloseDocument, event3, 'CloseDocument')
subscribe_to(Rhino.RhinoDoc.NewDocument, event3, 'NewDocument')

Any thoughts on this? Thankful for any feedback and suggestions!
Also, i’ve noted that the documentation at: http://developer.rhino3d.com/api/RhinoCommonWin/html/N_Rhino.htm now targets Rhino 6. Do you know where I can find complete documentation for Rhino 5 (latest SR)?

Best
/Jakob

2 Likes