Scripcontext.escape_test crashes Rhino

I made a small branching exercise in python that can lead to a near-infinite loop. Is very light but I used escape_test to be able to break it.
My problem is that it sometimes breaks the scripts, leaving the lines and flowers created by the script on Rhino, and sometimes it just shuts the whole program without warning.

Is it normal?

I wound not say this is normal. But is certainly possible for script of plug-in code to bring Rhino down.

If you want help diagnosing the problem, feel free to post your code here.

– D

Hi, Dale.

I didn`t share the specific script as it has happened (randomly, as I said before) with several different ones. Though this might be because I’m using “escape_test” in a wrong way.

Just to be sure, here it is:

import rhinoscriptsyntax as rs
import random
import scriptcontext as sc

p = 0,0,0 #rs.GetPoint("pick point")
h = 3 #rs.GetInteger("set height")
t = 0,h,0
def palm(trunk):
    t = rs.CurveEndPoint(trunk)
    o = rs.CurveStartPoint(trunk)
    v = rs.VectorCreate(t,o)
    n = random.randint(1,5)
    s = random.uniform(0.3,1.2)
    for i in range(n):
        alfa = random.randint(-30,30)
        w = rs.VectorRotate(v,alfa,(0,0,1))
        w = rs.VectorScale(w,s)
        long = rs.VectorLength(w)
        u = rs.PointAdd(t,w)
        sc.escape_test()
        branch = rs.AddLine(t,u)
        rojo1 = random.randint(10,55)
        verde1 = random.randint(100,215)
        azul1 = random.randint(40,55)
        rs.ObjectColor(branch,(rojo1,verde1,azul1))
        if (long>=1):
            palm(branch)
        elif sc.escape_test(False):
            print "ESC pressed "
            break      #get out of the loop
        else:
            f= rs.AddCircle(u,.2)
            rojo = random.randint(100,185)
            verde = random.randint(100,135)
            azul = random.randint(100,155)
            rs.ObjectColor(f,(rojo,verde,azul))
tr = rs.AddLine(p,t)
palm(tr)

Well, as far as I understand, if you are in a “tight” loop, the escape_test never actually gets picked up by Rhino because it’s too busy. You need to actually tell it to slow down and wait for the “message pump” to catch up. You do this by inserting a Rhino.RhinoApp.Wait() line just before the sc.escape_test(). (you will need to import Rhino at the top)

In your script, you are also having it decide between the condition long>=1 and escape_test(False). If long is never >=1, you will never hit the escape_test(). I might put it outside that condition. The following seems to react to escape fairly well:

import rhinoscriptsyntax as rs
import Rhino, random
import scriptcontext as sc

p = rs.coerce3dpoint([0,0,0]) #rs.GetPoint("pick point")
h = 3 #rs.GetInteger("set height")
t = 0,h,0
def palm(trunk):
    t = rs.CurveEndPoint(trunk)
    o = rs.CurveStartPoint(trunk)
    v = rs.VectorCreate(t,o)
    n = random.randint(1,5)
    s = random.uniform(0.3,1.2)
    for i in range(n):
        alfa = random.randint(-30,30)
        w = rs.VectorRotate(v,alfa,(0,0,1))
        w = rs.VectorScale(w,s)
        long = rs.VectorLength(w)
        u = rs.PointAdd(t,w)
        sc.escape_test()
        branch = rs.AddLine(t,u)
        rojo1 = random.randint(10,55)
        verde1 = random.randint(100,215)
        azul1 = random.randint(40,55)
        rs.ObjectColor(branch,(rojo1,verde1,azul1))
        Rhino.RhinoApp.Wait()
        if sc.escape_test(False):
            print "ESC pressed "
            break      #get out of the loop
        if (long>=1):
            palm(branch)
palm(rs.AddLine(p,p+rs.coerce3dvector([0,h,0])))

–Mitch

1 Like

Thanks! Will try it!