WorldViz User Forum

WorldViz User Forum (
-   Vizard (
-   -   Vizard Tip of the Month: Use Tasks to Simplify your Code (

Gladsomebeast 04-28-2008 12:28 PM

Vizard Tip of the Month: Use Tasks to Simplify your Code
Vizard's Task feature makes writing code that you want to happen over time easier. Tasks are functions that pause as they wait for some condition (like 2 seconds to pass or spacebar pressed) before continuing with their local variables preserved.

Say you want a traffic light that changes color every second. Using timers, you could create a Vizard timer event that sets the next color based on a global variable that remembers the previous color. Or you could create a self-contained task function.


import viztask
ball = viz.add('white_ball.wrl', pos=[0, 1.7, 1])
def doStoplight(theBall):
        while True:
                yield viztask.waitTime(1)
                yield viztask.waitTime(.5)
                yield viztask.waitTime(1)

First, the code imports the viztask module of Vizard that provides the Task functionality. After adding our stoplight ball we define our task function just like a normal function. The function becomes a task function because of the "yield" statement. The yield statement indicates when the task pauses and waits for some seconds. When the wait time is up, the next lines of code are run until we hit another yield statement. The last line passes an instance of our "doStoplightTask" to the scheduler. The scheduler triggers the execution of the task. Tasks must be scheduled to run, they can't be called like normal functions.

Creating tasks that wait for other tasks can simplify programs that progress though a series of states. Research experiments often progress though these states: get parameters; present a number of trials; finalize data recording. Here is a experiment framework using tasks:


import viztask
def doExperimentTask():
        NUMBER_OF_TRIALS = 3
        for i in range(NUMBER_OF_TRIALS):
                yield doTrialTask(i)
        print 'experiment done'

def doTrialTask(trialNumber):
        #present stimulus
        yield viztask.waitKeyDown(' ') #gather response
        #record data
        print 'trial number', trialNumber

More information regarding Vizard task is in your Vizard help file and at this address:

Andrey 04-29-2008 11:28 AM

Is it possible to use viztask in order to delay onCollide events? - i.e. to create the time interval after the collision when objects do not produce collision event?

theuberk 04-29-2008 11:39 AM

A tip of the month is an excellent idea. Thanks!

Gladsomebeast 04-29-2008 12:27 PM


Tasks can wait for a number of conditions, including events like COLLIDE_BEGIN_EVENT. Perhaps you want to do something like this?


import viztask
def doSomethingWhenCollidingTask():
        while True:
                yield viztask.waitEvent(viz.COLLIDE_BEGIN_EVENT)
                print 'collidy !!!'
                #do something
                yield viztask.waitTime(2) #ignore events for 2 seconds

k_iwan 05-02-2008 04:22 PM

Thank you for sharing. I'm using director for task scheduling... Is there any different?

farshizzo 05-02-2008 04:30 PM

Unless you are performing blocking operations (i.e. reading from file or socket) I strongly recommend against using director functions. Director functions use real operating system threads, therefore they are not deterministic and you could experience synchronization problems.

All times are GMT -7. The time now is 09:28 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC