0

I'm running my script in blender, and I want to detect Ctrl + c as to close the script,
but before I do close my script I want to run a function to do some cleanup

So I tried to follow this solution to listen for Ctrl + c as such:

import signal
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
# insert time taking code

but the problem is that sys.exit(0) completely exists blender, whereas I need it to only stop the script execution.

So how do I invoke whatever the default behaviour of Ctrl + c so that that only the script stops executing?
(or am I looking in the wrong direction?)

cak3_lover
  • 1,440
  • 5
  • 26

1 Answers1

0

Found the solution

import signal

default_handler = None

def handler(num, frame):    
    # Do something that cannot throw here (important)
    print("Hello World")

    return default_handler(num, frame) 

if __name__ == "__main__":
    default_handler = signal.getsignal(signal.SIGINT)

    # Assign the new handler
    signal.signal(signal.SIGINT, handler)
cak3_lover
  • 1,440
  • 5
  • 26