Help: Python Check for ESC to work in plugin and button

Hi guys,
I still can not figure out how to get the ESC test to work when the script is compiled or ran in a button.

I have tried to use both break and exit()
I also tried replacing “While True:” with “While run==True:” and then have the esc test alter “run=False” but then it just ignored the ESC and continued to run.

This works well if ran from the pythonscriptcomplier:


# Help: How to set this up to run propperly as a plugin

import rhinoscriptsyntax as rs
import scriptcontext

Total=100
nr=0

def ESC_test(nr,Total):
    while True:
        rs.Sleep(100)
        rs.Prompt("Running cycle "+str(nr)+" of "+str(Total))
        nr=nr+1
        if nr==Total:
            break

    #check for esc press
        if scriptcontext.escape_test(False):
            print "ESC pressed "
            break      #get out of the loop

if __name__ == "__main__":

    ESC_test(nr,Total)

What do you mean by ‘compiled’??? in a RunCommand() function? Or a button linked to a -RunPythonScript specifying the file path?

Or compiled as an .rhp using the rhinoscript compiler?

Yes, compiled with the RhinoScriptCompiler.exe, into a .rhp plugin.

Edit: And in a button with !_-RunPythonScript( ‘script pasted here’ )

Works here as a command… not a .rhp plugin…

Yeah, it is as plugin or from button that casues the issues.
And I need it to work as a plugin.

@John_Brock can you help me out here?
I really need ESC to break a While True loop when compiled as a plugin.

Guys, I really need some help here!
What is the proper way to terminate a compiled python script by using ESC?

-Jørgen

@stevebaer is this something you can help with?

1 Like

Eventually, but definitely not today. I’ve started the mega-merge project and am focusing all of my effort there. From what I can tell, there is some subtle bug in which we will need to fix.

OK, and any ideas when you can fix it?
And is it a bug in the compiler, or in Rhino? (in other words, do I need to wait for the next SR?)

I had to release Holomark with out letting users terminate it, and the project I was going to release now relies on it.

Thanks for understanding.

Sorry, but I don’t know when or where this will be fixed. If I knew the cause of the bug, it would probably be easy to fix. I understand this is not the answer you want, but it is the truth. I’m in the middle of a massive project that will most likely take at least the next month of my focused time to complete.

Full respect for your foucs, frustrating though, but are there any workarounds you can point me to? It needs to work as a plugin.

And please share some info on the project you are working on when you can. Would love to know just out of pure curiosity :slight_smile:

I need a small help on a 90% working workaround:

I can use the right mouse button to stop the script with:

if mb==System.Windows.Forms.Control.MouseButtons.Right:
    break

But this also repeats the last command, so Rhino is also listening to the mouse buttons… can I work around that?

Thanks.

Hi Jørgen.
If the command that must not be repeated is the same command that you have just stopped by the RMB, then what about usind the DocumentUserText rhinoscriptsyntax functions ?

When you catch the mouse button event to stop the command, you can store a DocumentUserText entry (kind of a flag).
When your command starts, first it has to check if the DocumentUserText entry is set.
If it is set, then the command was run by the RMB event and the command must stop immediately and reset the DocumentUserText entry.
Otherwise it can run regularly (the command was run intentionally by the user).

Does this make any sense ?
I have not tried that, sorry … just an idea …
Cheers

Thanks emilio.
That’s a creative idea, I’ll look into it.

@stevebaer why does sys.exit() close Rhino and not only the python process?

I am still trying to solve the esc issue with plugins since not being able to use esc to quit a script renders Python useless to some of the tasks I need to solve.

Can it be solved in Rhino common or in VB instead?
PLEASE help in finding a workaround if you still don’t have time to fix the bug.

Thanks.

I’ve been working on a project we call around here the ‘mega-merge’ since early December. This is the process of merging all of the changes in the source for Mac Rhino since it began with the current V6 Window code. This will let us develop on a single code stream for both platforms in the future and will hopefully allow us to release future Rhino versions on both platforms around the same time. This is a pretty big undertaking and I’m still trying to finish it up. It does look like I’m getting very close.

Rhino and python run in the same process which is why sys.exit shuts everything down. That is what it is designed to do.

Were you able to implement RMB the idea that Emilio had earlier in this thread?

Sorry, I still haven’t had time to investigate this issue. I understand that is frustrating and not the answer you are looking for, but it is the truth.

Hi Steve,
Thanks for the feedback. It’s frustrating, yet very understandable…

Yes, I got it working after a bit of tweaking, and it seems to do the job very well also in a plugin.
But the user interface isn’t great, as ESC is an established Canceler and RMB is used in Rhino to navigate the views, so it works, but is a bit counter intuitive.

Again thanks for understanding and helping out. Much appreciated.

I’m pretty sure this won’t be fixed for SR11 since we are getting very close to releasing that. The bug is on my list now at
http://mcneel.myjetbrains.com/youtrack/issue/RH-29456

Oh, That’s a shame.
So… if not in SR11, does that mean not in Rhino 5? Or do you think there will be a SR12 as well?