RhinoScript doesn't work with sc.escape_test (nor print to status)

Please try this in WIP:

#! python3

import rhinoscriptsyntax as rs

import System
import System.Collections.Generic
import Rhino

import scriptcontext as sc

for i in range(20):
    rs.Sleep(100)
    print(sc.escape_test())
    if sc.escape_test(True):
        print("True")
    if sc.escape_test(False):
        print("False")

And see that it won’t print to the command line nor does it react on ESC press.

Hi @Holo , I can confirm, logged as RH-72484 Rhinocode scriptcontext.escape_test() not working

2 Likes

Can you please verify that rs.Command() isnt working either?

#! python3

import rhinoscriptsyntax as rs

import System
import System.Collections.Generic
import Rhino

print(Rhino.RhinoDoc.ActiveDoc)

rs.Command("print")

Hi Holo,
I tweaked this line to print i as well, to see what happened through the iterations and got this outcome:

As you can see, it is printing to the command line and pressing the escape key has been acknowledged. Note that as documented I had to press the escape key twice.

I suspect you may be under a misapprehension about escape_test.
Firstly, it takes two optional parameters:

def escape_test( throw_exception=True, reset=False ):

so the True/False parameters you are supplying control whether an exception is thrown or not. The second parameter is passed to the host environment.
Secondly, it returns a boolean which indicates whether the escape key was pressed.

Your script causes escape_test to be run three times in each iteration. The second and third times are so close after the first that the chance of getting an escape press between them is negligible so their outcomes will be false, meaning that the associated print statements will not be executed.

Only the first test is likely to register a press. Up to that point the function returns false and that is what we see printed in the command line. When escape is detected the first print statement would print True, but the function is using the default throw_exception=true setting so an exception is thrown and, as there is no exception handling in your script, it stops before getting to the print statement.

(Thus, @Gijs, escape_test() is actually working correctly and RH-72484 is redundant.)
[EDIT: @Gijs the above relates to Iron Python - I missed Holo’s RhinoCode tag - and escape_test does not work in Python 3 in my alternative code anymore than in Holo’s, whereas Dale’s solution below does. Sorry for muddying the waters.]

If you want to halt the script without setting up an exception handling framework you could do this, turning off the exception throw and adding a break statement:

for i in range(20):
    rs.Sleep(100)
    if sc.escape_test(False):
        print(i, "Escape pressed")
        break

HTH
Jeremy

Rhino - Canceling a Python script in Rhino (rhino3d.com)

Seems OK here:

Regards
Jeremy

That’s strange. What version of Wip are you on?
I suddenly had issues with adding a point too, and had to restart Rhino for it to work.
Print isnt printing to command either, only to the RhinoCode terminal.

(You tested with RhinoCode, right?)

Er, no… Let me give that a go…

1 Like

OK, In RhinoCode there’s a bug in the application.py file at

%userprofile%\.rhinocode\python3.9-1.26\site-rhinopython\rhinoscript\application.py

It is missing an import Rhino statement and this prevents Rhino.RhinoDoc.ActiveDoc completing.

If that is sorted then your “command” script runs correctly. I imagine the same or a similar error is causing your other difficulties.

WIP version 8.0.23010.12305, 2023-01-10

Sorry I missed the Rhinocode tag before.

HTH
Jeremy

1 Like

Hey @Holo.

See if this works for you:

import Rhino

class EscapeKeyHelper:
    # Constructor
    def __init__(self):
        self.escape_key_pressed = False
        Rhino.RhinoApp.EscapeKeyPressed += self.OnEscapeKeyPressed;
    # Destructor
    def __del__(self):
        Rhino.RhinoApp.EscapeKeyPressed -= self.OnEscapeKeyPressed;
    # Event handler
    def OnEscapeKeyPressed(self, sender, e):
        self.escape_key_pressed = True
    # Gets the 'escape key pressed' status
    @property
    def EscapeKeyPressed(self):
        return self.escape_key_pressed

def TestCancel():
    
    helper = EscapeKeyHelper()
    
    for i in range(0, 10090):
        # Keep Windows message pump alive
        Rhino.RhinoApp.Wait()
        # Check escape key helper
        if (helper.EscapeKeyPressed):
            print("Escape key pressed")
            break
            
        msg = "Hello {0}".format(i)
        Rhino.RhinoApp.WriteLine(msg)
    
if __name__ == "__main__":
    TestCancel()

– Dale

1 Like

Thanks I’ll test that out!

What does @property do, I have not seen @ used before.

Are there any ways you can turn off these horrible “typo warning things”? They make it so much more difficult to read the code.

image

Hi @Holo,

From the Edit Menu choose Toggle Diagnostics to turn them off and on.

Regards
Jeremy

1 Like

Thanks!!! That made a world of difference!
So what does the diagnostics try to tell me? It seems like everything I type is wrong when I have that on :wink:

I think it is indicating where your layout differs from the PEP 8 style guide for Python (and maybe PEP 257).

But it also tells you useful stuff like where you code a reference to a library which you don’t have an import for.

1 Like

Thanks @dale, this works.

One proviso that should be made public: if you are running the code in the RhinoCode editor you need to click in the Rhino window to give it focus before pressing the escape key.

Regards
Jeremy

1 Like

Another thing, I just made a plugin with three tools made with RhinoCode but none of the commands work when I drop that plugin into Rhino8.

Isn’t this hooked up yet?

I see that I can’t put the script on a toolbar button with ! _RunPythonScript (“script”) either.
That throws this error:
image

I presume it is caused by trying to run the python script through IronPython.

Is the only way right now to run Python 3 scripts through RhinoCode?

I found another strange thing.
If I use rs.GetObjects(“”,preselect=True) then the command I used took 30 seconds to complete IF objects were preselected. But only 2 seconds if I selected them after the command was started.

Pretty strange.

both these issues have been fixed and will be availabe in next WIP

2 Likes