Vizard 7 » Reference » Flow control » Timers » Timer basics

Timer Basics

Timers allow you to trigger events at a given time or repeatedly at a given interval. When you create a timer, you specify the expiration delay (in seconds) and the number of times the timer should repeat. There are several ways to set up timers in Vizard-- you can use a global timer callback, a callback within an event class, or timers using actions (all described below).


Timers, per se, aren't the only way to control timing in Vizard. If you're animating a node or setting up a series of animations, consider using actions available in the vizact library. You might be able to control the timing of your animations using vizact.waittime. For precise timing control within a thread of code, the viztask library is useful.

Registering global timers

To register a global timer event, use viz.callback( viz.TIMER_EVENT, <function>). This callback will call the specified function whenever a timer expires, providing it with the timer id as an argument.  Use viz.starttimer( <timer name> ) within you script whenever you want to start a timer for this callback. To stop a timer from going off, use viz.killtimer( <timer name> ).

#Define the timer ids.
ONCE = 2
#Create a function to handle the timer events.
def onTimer(num):
    #Use the time ids to identify the timer.
    if num == REPEATER:
        print('first timer')
    elif num == ONCE:
        print('second timer')
        #Kill the other timer.
        viz.killtimer( REPEATER )
#Register the timer callback.
#Start both timers.
#The first will be repeated.
viz.starttimer( REPEATER, 1, viz.PERPETUAL )
#The second will go off only once.
viz.starttimer( ONCE, 5, 1 )

Registering timer events within a class

Event classes can have their own timers inside of them. These timers have much of the same code as the global timers. However, you will need to initialize your class as an event class:

#Create an event class.
class modelMaker( viz.EventClass ):
    def __init__(self, modelName):
        #Initialize the base class
        #Create a callback to our own event class function
        #Start a perpetual timer for this event class
        #to go once per second.
        #Add a counter.
        self.counter = 0
        #Store the model name in this class.
        self.modelName = modelName
    def myTimer( self, num ):
        #Add a model by the given name and place it according to the counter.
        viz.add(self.modelName).setPosition( 0,self.counter,0 )
        #Advance the counter.
        self.counter += 1

#Call the class.

Registering timer events with actions

The action library has its own code for timers. The vizact.ontimer command calls a function perpetually at the provided interval. The vizact.ontimer2 command calls a function a specific number of times at the provided interval. To kill either of these timers, use setEnabled( 0 ).

#Import the random module.
import random

#Create a function to call with a timer.
def changeColor( ):
    viz.clearcolor( random.choice( [viz.BLACK, viz.GREEN, viz.ORANGE] ) )
#Create a timer to call the function every half second.
myTimerAction = vizact.ontimer(.5,changeColor)
#Disable the timer with a keystroke.
vizact.onkeydown( ' ', myTimerAction.setEnabled, 0 )

#Create another timer to spin an object.
myObject = viz.add('wheelbarrow.ive')
#This timer has the function embedded within it.
#It will go off 10 times, every 2 seconds.
vizact.ontimer2( 2,10, myObject.addAction, vizact.spin(0,1,0,30,1))

See also

Other sections:

Action basics

Event Basics