Correct way to detect ESC pressed in houdini python?

   5070   6   1
User Avatar
Member
34 posts
Joined: April 2015
Offline
What is the correct way to detect a user pressing the escape key during the execution of a python script.

In particular I am want to something like:


interrupted = 0
try:
hou.hscript('viewwrite -f 1 100 <view name> cfoo.\$F.jpg')
except:
interrupted = 1


In this case I can interrupt the viewwrite hscript command (and it kills the viewwrite as expected), however it does not seem to be caught by the try: statement..

What is the correct way to do this?
User Avatar
Member
678 posts
Joined: July 2005
Offline
http://www.sidefx.com/docs/houdini15.0/hom/hou/InterruptableOperation [sidefx.com]
User Avatar
Member
34 posts
Joined: April 2015
Offline
Have you ever used that class? I could really use an example for my case. I couldn't figure out how to get it to work. I tried the following and if I use ESC to abort the viewwrite part way through, or just let it complete normally without interrupting, the following code always returns True for aborted:

def doit():
aborted = False
with hou.InterruptableOperation(“doing stuff”, open_interrupt_dialog=True) as operation:
ret = hou.hscript(“viewwrite -f 1 40 Build_gg.panetab1.world.persp1 ctest/foo.\$F.jpg”)
operation.updateProgress()
if hou.OperationInterrupted:
aborted = True
if aborted == True:
print “aborted!”
else:
print “continuing on…”
User Avatar
Member
678 posts
Joined: July 2005
Offline
cruiserandmax
Have you ever used that class?

Read about catching exceptions in Python. https://docs.python.org/2/tutorial/errors.html [docs.python.org]
User Avatar
Member
34 posts
Joined: April 2015
Offline
Thanks again for trying to help. I combed over the python docs for handling exceptions as well as the houdini docs for hou.InterruptableOperation again.

From my understanding this should work:
def doit():
aborted = False
with hou.InterruptableOperation(“doing stuff”, open_interrupt_dialog=True) as operation:
ret = hou.hscript(“viewwrite -f 1 40 Build_gg.panetab1.world.persp1 ctest/foo.\$F.jpg”)
try:
operation.updateProgress()
except hou.OperationInterrupted:
aborted = True
if aborted == True:
print “aborted!”
else:
print “continuing on…”

However when I run that function and hit Escape in the middle of the flipbook, I sometimes get “continuing on…” and sometimes get “aborted!”. If I run the function and let the flipbook complete on it's own I get “continuing on…” every time. So unless something is wrong with my code- it seems that either the hou.InterruptableOperation class is bugged, or there is something weird about how the interrupting of the hscript function is handled through the python hou.hscript() call.

I keep going over and over the details but I just can't figure out what I may be doing wrong. :cry:
User Avatar
Member
34 posts
Joined: April 2015
Offline
Ack! Going bach to Houdini 13 and the code in the post above works perfectly (catches the exception reliably every time). I only get the misbehavior in H14.. ugh.
User Avatar
Member
1904 posts
Joined: Nov. 2006
Offline
I'm surprised it worked in previous versions. hou.hscript() definitely is useful but whenever I consider using it I definitely expect nothing from it in terms of useful return information, error handling and interruptibility. It is still quite sad there is no HOM equivalent to viewwrite. It's only been RFE'd for forever.
Graham Thompson, Technical Artist @ Rockstar Games
  • Quick Links