Error Handling (Pressing Esc) using Rhino Script Syntax in Python

Been learning Python though automating some of the tasks we do in the office on the project I am on, and would like to give the tools to the rest of the team.

Worked how how to write a simple script for moving Geometry up and down the shift vector but if Esc is pressed part way though the process it runs to the end and returns an Error. I have read up on Event Handling/Checking if escape been pressed but can’t get it to notice this when I run the code.
Current Code is below I someone could give me some pointers.

import rhinoscriptsyntax as rs
import RhinoPython.Host as __host

#Shift Model from Acad Origin to Rhino Origin or visa versa.

acadOrigin = (5100,55370,0)
rhOrigin = (0,0,0)

#check for esc press
def escape_test( throw_exception=True, reset=False ):
    "Tests to see if the user has pressed the escape key"
    rc = __host.EscapePressed(reset)
    if rc and throw_exception:
        raise Exception('escape key pressed')
        return rc

def moveMTC ():
    objs = rs.GetObjects('Select Objects to Move',0,True,True)
    strDir = rs.GetString('Select Direction','Import',['Import', 'Export'])
    if strDir == 'Import': shiftVec = rs.VectorCreate(rhOrigin,acadOrigin)
    else: shiftVec = rs.VectorCreate(acadOrigin,rhOrigin)
    m_objs = rs.MoveObjects(objs,shiftVec)
    if m_objs: rs.ZoomSelected(None,True)

if __name__=="__main__":

Thanks Matt

Don’t use RhinoPython.Host; here’s a post that describes how to work with the escape key

Hi Steve,
Yeah had found that post but what ever I tried I could not get it to cut out of the code if Escape was pressed. So i researched the other one I posted. As it not a loop but a list of commands do I need it more than once as the user could realise some things is wrong and press escape at anytime while its running though the code.

Thanks Matt

Sorry, I didn’t read your sample close enough. You shouldn’t have to test for escape at all. You need to test the results of rs.GetObjects and rs.GetString and if those results are None, then return from the function

Ah that’s Perfect. Think I was jumping the gun a bit, trying to get clever with error handling.
Thank you