[Bf-blender-cvs] [dcb86689b03] blender2.8: Python API Docs: Examples for new timer api

Jacques Lucke noreply at git.blender.org
Tue Nov 27 11:31:57 CET 2018


Commit: dcb86689b037e545b08ff35c355db08889b73ef1
Author: Jacques Lucke
Date:   Tue Nov 27 11:31:35 2018 +0100
Branches: blender2.8
https://developer.blender.org/rBdcb86689b037e545b08ff35c355db08889b73ef1

Python API Docs: Examples for new timer api

===================================================================

A	doc/python_api/examples/bpy.app.timers.1.py
A	doc/python_api/examples/bpy.app.timers.2.py
A	doc/python_api/examples/bpy.app.timers.3.py
A	doc/python_api/examples/bpy.app.timers.4.py
A	doc/python_api/examples/bpy.app.timers.5.py

===================================================================

diff --git a/doc/python_api/examples/bpy.app.timers.1.py b/doc/python_api/examples/bpy.app.timers.1.py
new file mode 100644
index 00000000000..bae3b94b24a
--- /dev/null
+++ b/doc/python_api/examples/bpy.app.timers.1.py
@@ -0,0 +1,10 @@
+"""
+Run a Function in x Seconds
+---------------------------
+"""
+import bpy
+
+def in_5_seconds():
+    print("Hello World")
+
+bpy.app.timers.register(in_5_seconds, first_interval=5)
diff --git a/doc/python_api/examples/bpy.app.timers.2.py b/doc/python_api/examples/bpy.app.timers.2.py
new file mode 100644
index 00000000000..cdd9bf8e9ff
--- /dev/null
+++ b/doc/python_api/examples/bpy.app.timers.2.py
@@ -0,0 +1,11 @@
+"""
+Run a Function every x Seconds
+------------------------------
+"""
+import bpy
+
+def every_2_seconds():
+    print("Hello World")
+    return 2
+
+bpy.app.timers.register(every_2_seconds)
diff --git a/doc/python_api/examples/bpy.app.timers.3.py b/doc/python_api/examples/bpy.app.timers.3.py
new file mode 100644
index 00000000000..79daf6a7740
--- /dev/null
+++ b/doc/python_api/examples/bpy.app.timers.3.py
@@ -0,0 +1,17 @@
+"""
+Run a Function n times every x seconds
+--------------------------------------
+"""
+import bpy
+
+counter = 0
+
+def run_10_times():
+    global counter
+    counter += 1
+    print(counter)
+    if counter == 10:
+        return None
+    return 0.1
+
+bpy.app.timers.register(run_10_times)
diff --git a/doc/python_api/examples/bpy.app.timers.4.py b/doc/python_api/examples/bpy.app.timers.4.py
new file mode 100644
index 00000000000..a56d0e5f36f
--- /dev/null
+++ b/doc/python_api/examples/bpy.app.timers.4.py
@@ -0,0 +1,12 @@
+"""
+Assign parameters to functions
+------------------------------
+"""
+import bpy
+import functools
+
+def print_message(message):
+    print("Message:", message)
+
+bpy.app.timers.register(functools.partial(print_message, "Hello"), first_interval=2)
+bpy.app.timers.register(functools.partial(print_message, "World"), first_interval=3)
diff --git a/doc/python_api/examples/bpy.app.timers.5.py b/doc/python_api/examples/bpy.app.timers.5.py
new file mode 100644
index 00000000000..b35e307f218
--- /dev/null
+++ b/doc/python_api/examples/bpy.app.timers.5.py
@@ -0,0 +1,25 @@
+"""
+Use a Timer to react to events in another thread
+------------------------------------------------
+
+You should never modify Blender data at arbitrary points in time in separate threads.
+However you can use a queue to collect all the actions that should be executed when Blender is in the right state again.
+Pythons `queue.Queue` can be used here, because it implements the required locking semantics.
+"""
+import bpy
+import queue
+
+execution_queue = queue.Queue()
+
+# This function can savely be called in another thread.
+# The function will be executed when the timer runs the next time.
+def run_in_main_thread(function):
+    execution_queue.put(function)
+
+def execute_queued_functions():
+    while not execution_queue.empty():
+        function = execution_queue.get()
+        function()
+    return 1
+
+bpy.app.timers.register(execute_queued_functions)



More information about the Bf-blender-cvs mailing list