Script won't run in a button, but works fine in editor

Hi @stevebaer

Do you have any idea why this works fine in the editor, but won’t work in a button with -_RunPythonScript () ?
Is



import rhinoscriptsyntax as rs
import scriptcontext


def Draw_Hatch(pts):

    while True:
        rs.Sleep(10)
        newPt=rs.GetPoint()
        if newPt:
            try:
                rs.DeleteObject(tmpCrv)
            except:
                rs.Sleep(10)
            pts.append(newPt)
            if len(pts)>1:
                tmpPts=list(pts)
                if len(pts)>2:
                    tmpPts.append(tmpPts[0])
                tmpCrv=rs.AddPolyline(tmpPts)

        else:
            return tmpCrv
            break      #get out of the loop

if __name__ == "__main__":

    pts=[]
    crv=Draw_Hatch(pts)
    if rs.IsCurve(crv):
        if rs.IsCurveClosed(crv):
            newHatch=rs.AddHatch(crv)
            rs.DeleteObject(crv)
            rs.SelectObject(newHatch)

It seems like it doesn’t support “definitions” because this works just fine in a button:

-_RunPythonScript (

import rhinoscriptsyntax as rs
import scriptcontext


pts=[]



while True:
    rs.Sleep(10)
    newPt=rs.GetPoint()
    if newPt:
        try:
            rs.DeleteObject(tmpCrv)
        except:
            rs.Sleep(10)
        pts.append(newPt)
        if len(pts)>1:
            tmpPts=list(pts)
            if len(pts)>2:
                tmpPts.append(tmpPts[0])
            tmpCrv=rs.AddPolyline(tmpPts)

    else:
        break      #get out of the loop




if rs.IsCurve(tmpCrv):
    if rs.IsCurveClosed(tmpCrv):
        newHatch=rs.AddHatch(tmpCrv)
        rs.DeleteObject(tmpCrv)
        rs.SelectObject(newHatch)

)

Jusr remove the if __name__ == "__main__": and de-dent what’s below it, it will run fine.

You only need the if __name__ == "__main__": structure if you have a series of modules and you may want to use any of them as a stand-alone. Personally, I don’t use it for any of my scripts, and I found it doesn’t work with toolbar buttons and ! _-RunPythonScript().

–Mitch

3 Likes

Thanks Mitch!

Normally if I have a series of definitions that comprise a script and that the main “wrapper” that executes everything is also in encapsulated in a definition - necessary if you want to be able to escape gracefully when the user aborts - I just do this:


def XYZ():
    bla
    bla
    return x

def ABC(x):
    bla
    bla
    return y

def MainBody():
    ui0=#some user input
    ui1=#some user input
    a=XYZ(ui0)
    b=ABC(ui1)
    #do something with a and b
   
MainBody()

Calling MainBody at the end will run the MainBody definition which will call the others.
This works inside the script editor, as well as with a toolbar button:

! _-RunPythonScript (
<paste whole script in here>
)

or, if you have a path set and the script stored as a .py file in the path directory and named MyScript.py:

! _-RunPythonScript "MyScript"

–Mitch

1 Like

That’s great to know Mitch, as always thanks for sharing!