For my addon I need a function that simulates just a single ESC button event from the keyboard, using Python. I've tried many things and this seems to be the only way left to abort


operator without killing the whole Blender.

I've found this Ctypes method. This is builtin module unlike win32api or Pynput, so it can be imported without install right from inside the Blender. It seems to be exactly what I need. But importing the whole module and writing all those classes is obviously way too much for just a single ESC keyboard command, and unfortunately my current programming skills don't allow me to get through the whole module to find what I need. Please, can anyone help me with this?

  • $\begingroup$ This is possible in theory, but not suitable for an add-on. You can start Blender with the CLI argument --enable-event-simulate which allows you to simulate events through bpy.context.window.event_simulate("ESC", "PRESS"). However this will only allow to create events through the operator, keyboard input will be ignored. $\endgroup$ Commented Mar 8, 2020 at 14:54
  • 1
    $\begingroup$ A modified version of this answer may be what you're looking for. Please note that calling a modal operator from a modal operator is generally not supported and this may break in future versions of Blender. $\endgroup$ Commented Mar 8, 2020 at 14:54
  • $\begingroup$ Thank you! Unfortunately both suggestions don't work in my case. bpy.context.window.event_simulate() while called from custom operator with overrided context causes "RuntimeError: Error: Not running with '--enable-event-simulate' enabled". Excepting this error doesn't help either and I can't figure out how it suppose to work. The second one allows user to abort render initiated by script, while my goal is opposite: to abort render initiated by user using script and execute another script instead. $\endgroup$ Commented Mar 9, 2020 at 11:50
  • $\begingroup$ Like I said the first option is not suitable for an add-on, it's a debug feature. You would have to start Blender from the command line with the --enable-event-simulate flag. You're right the second variant would only work if you want to stop rendering a sequence, it would not allow you to stop the current render. I don't think it's possible to directly terminate a modal operator through the API. $\endgroup$ Commented Mar 9, 2020 at 12:20

1 Answer 1


Here is the solution for Windows I was looking for:

import ctypes


Beeing written as a function and appended to bpy.app.handlers.render_pre it simulates pressing ESC on the keyboard and aborts the render.

In my addon I used another solution: it turned out to be possible to replace original Blender main top Render menu with custom one which buttons execute addon's operators instead of original Blender's. It was done by recreating inside the script and re-registering original class TOPBAR_MT_render(bpy.types.Menu) which can be found in your Blender current version folder -> scripts -> startup -> bl_ui -> space_topbar.py

Also I replaced operators for original keymap shortcut settings by editing bpy.context.window_manager.keyconfigs.active.keymaps['Screen'].keymap_items, so in the end user uses the same buttons and shortcuts he used to, but they execute addon's operators instead of original ones, and this is suitable for every system, not only for Windows.

  • 1
    $\begingroup$ Cool, do you mind sharing a link to the add-on? For a cross platform solution I think getting pypi.org/project/keyboard via pip might be another option. $\endgroup$
    – brockmann
    Commented Mar 12, 2020 at 9:04
  • $\begingroup$ I will, as soon as at least an alpha-version is ready. "Keyboard" module is cool, but I haven't found a way to use non built-in modules like this in Blender addons. Is it possible somehow? $\endgroup$ Commented Mar 12, 2020 at 13:44
  • $\begingroup$ Yeah, blender comes with pip as of 2.8x. Hadn't the time to test it though... $\endgroup$
    – brockmann
    Commented Mar 13, 2020 at 15:33
  • $\begingroup$ It took more time than I thought but add-on's alpha is ready: github.com/sorecords/true_motion_blur $\endgroup$ Commented Jun 19, 2020 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .