04 February, 2021

Blender Python Tutorial: Application Timers

 

In this video, we will be taking a quick look at Timers. We will be creating a simple timer that will run a function once after a set amount of time. Then we will create a timer that will run a function once every second for a set amount of times.








This timer is quite simple but effective. If you want to run a Function after (x) amount of seconds this will do the trick.

We first create a Function and call it in_5_seconds. After 5 seconds we want something to happen, such as printing a message to the System Console.
We are not limited to just printing messages, we could add objects, make adjustments or even run other functions.

We can then call the Function as a Timer: bpy.app.timers.register(in_5_seconds, first_interval= 5). We just need to add the Function Name and then set the First Interval.

Since this is a float, we can lower the number to slow it down.












The next Timer we will be taking a look at is one where we can Run a Function once, every (x) amount of seconds for (n) amount of times. With this Timer we can set how many times we want a function to run along with how quick the function is ran between intervals.

We first create a Global Variable called counter. This variable will go up as our Function is ran. Once it hits a defined value it will then stop running the Function.

For this Timer we must make sure we add the If statement. We need to tell the Timer when to stop. By using the Counter we can define the amount of times the Function is ran. When we call this Timer we only need to add the Function Name.

There are other example of Timers and if you want to check out the Official Docs you can click here. If you want to see more Complex Timers be sure to let us know in the comments.

I hope you find this helpful and as always thanks for reading!.

No comments:

Post a Comment