[python] Debugging Events

How to debug events other than:

  • “If it works, it works”
  • “if it doesn’t work,…
    – …nothing happens”
    – …Rhino crashes"

:stuck_out_tongue_winking_eye:

You could put your calls inside a try/except statement and catch the exception.

import math
import rhinoscriptsyntax as rs
import sys
#rs.EnableRedraw(False)
try:
    points = []
    for i in range(0,5): #change to (0,25) to work correctly
        for j in range(0,25):
            points.append([i,j,0])
            
    for pt in points: 
        pt[2] = (math.sin(pt[1])+math.cos(pt[0]))
        rs.AddPoint(pt)
    rs.AddSrfControlPtGrid((25,25),points)
except Exception, ex: #various bits of the error
    #print(ex) 
    #print sys.exc_info()
    #print sys.exc_info()[0]
    #print sys.exc_info()[1]
    #print sys.exc_info()[2]
    print sys.exc_info()[3]
1 Like

Set breakpoints and watching what happens in the debugger

1 Like

When the event leads to crash breakpoints are useless :smiley:

Main reason I asked was this:

def object_added_event(sender, e):
    print "Object Added, Id =", e.ObjectId

def object_selected_event(sender, e):
    print "Object Selected Id =", e.ObjectId


def event_helper_func(event,sticky_key,event_name):
    #Event trigger helper function
    if sc.sticky.has_key(sticky_key):
        print "removing the callback"
        stky = sc.sticky[sticky_key]
        #Rhino.RhinoDoc.AddRhinoObject -= stky
        exec("Rhino.RhinoDoc."+event_name+" -= stky")
        sc.sticky.Remove(sticky_key)
    else:
        print "adding the callback"
        stky = event
        sc.sticky[sticky_key] = stky
        #Rhino.RhinoDoc.AddRhinoObject += stky
        exec("Rhino.RhinoDoc."+event_name+" += stky")




event_helper_func(object_added_event,"object_added_event","AddRhinoObject")
event_helper_func(object_selected_event,"object_selected_event","SelectObjects")

For some reason this crashes rhino.

Not the creation part, but the selection part

I assume it is trying to display everything that is currently selected, instead of just triggering upon adding to selection.

I could not find an event for added to selection

That’s because you need to set your breakpoint before it crashes… :stuck_out_tongue_closed_eyes:

This won’t tell me why it crashes it I will only see what makes it crash, and I already know that in such a simple script :rofl:

Set a breakpoint somewhere where you are sure it hasn’t got to the crash point. The use the Step into / Step over / Step out buttons to step through till it does crash. Then go back and stop just before that point and look at what your data is and what it is going to do next… That should help.

1 Like

Thanks,

Actually it was my bad I put the try: except: inside the event that was working :man_facepalming:

I figured what was the problem now:
I’m trying to access property of the RhinoObjectSelectionEventArgs that doesn’t exist. And that causes Rhino to crash.

Outside of proper debugging with breakpoints and raising exceptions etc. When debugging something that can crash whatever is running the code, I tend to write debug information to a file on disk. Also, with red Grasshopper scripting components, a neat tip is to use the record component to, well, record the out debug information.

How do you do that?
Do you just have with open("",w) as f: something something. inside the except: or is it something more advanced?