# Stop a long running operator with escape?

Would it be possible to write an operator that would allow the user to interrupt it with the escape key or even when a value gets changed in the redo pannel? Any pointer about how to do this?

This can be done with modal operator (you can find example here, look for 'Modal Execution'). I'm not sure about redo panel but executing some function on Redo can be done with handlers bpy.app.handlers.redo_pre or bpy.app.handlers.redo_post. Just append your cancelling function to them, like this:

import bpy

def my_func(self, context): # must have 2 arguments (3 if called from class)
print('Got Redo!')

bpy.app.handlers.redo_pre.append(my_func)


It will be executed on each redo. Use bpy.app.handlers.redo_pre.remove(my_func) to remove redo handler or bpy.app.handlers.redo_pre.clear() to clear all redo handlers

• Thank you for the answer! Are there not a way to check if a key is down or something. Making it Modal is just a bit too overkill maybe? Apr 11 '20 at 14:59
• I don't think there is another option because the idea of the modal operator seems to be exactly in serving as a listener to the user commands (keyboard, mouse, etc). It can run in the background, you just need to return {'PASS_THROUGH'}  in the modal() method instead of {'RUNNING_MODAL'}. In this way, as I know, it doesn't freeze other operators and waits until some event which you defined in the modal() method happens. Or I guess it can also check some value. By the way, the full list of events references you can find here. Apr 12 '20 at 8:20