bpy.app.timers.5.py 855 B

1234567891011121314151617181920212223242526
  1. """
  2. Use a Timer to react to events in another thread
  3. ------------------------------------------------
  4. You should never modify Blender data at arbitrary points in time in separate threads.
  5. However you can use a queue to collect all the actions that should be executed when Blender is in the right state again.
  6. Pythons `queue.Queue` can be used here, because it implements the required locking semantics.
  7. """
  8. import bpy
  9. import queue
  10. execution_queue = queue.Queue()
  11. # This function can savely be called in another thread.
  12. # The function will be executed when the timer runs the next time.
  13. def run_in_main_thread(function):
  14. execution_queue.put(function)
  15. def execute_queued_functions():
  16. while not execution_queue.empty():
  17. function = execution_queue.get()
  18. function()
  19. return 1.0
  20. bpy.app.timers.register(execute_queued_functions)