Vizard 7 » Reference » Flow control » Asynchronous Loading
7.7

Asynchronous Loading

Models, avatars, and textures can be loaded asynchronously using the viz.LOAD_ASYNC flag. This helps to reduce frame rate drops and stutter in the rendered scene when a model is loaded at runtime. The file is first opened and parsed in a background thread then file data (textures, geometry, shaders) are uploaded to the graphics card in the main Vizard loop. Although data is uploaded incrementally to maintain smoothness, the frame rate can take a hit from the upload of large textures and geometry.

Note: Asynchronous loading will typically increase the load time. It should only be used if you need to load resources in the background while the script is rendering.

Async Flag

Use the same flag to load models, avatars, and textures asynchronously:

model = viz.addChild('piazza.osgb',flags=viz.LOAD_ASYNC)
avatar = viz.addAvatar('vcc_male2.cfg',flags=viz.LOAD_ASYNC)
texture = viz.addTexture('images/tile_slate.jpg',flags=viz.LOAD_ASYNC)

All the commands above immediately return a valid node which can be used to query the status of the async operation. Most node commands will not work properly until the asynchronous operation has completed successfully.

if avatar.getAsyncStatus() == viz.ASYNC_SUCCESS:
    avatar.state(1)

Async Load Event

When the async operation completes, due to either success or failure, a viz.ASYNC_EVENT will be triggered. Use the <vizact>.onAsyncLoad command to register a callback function for this event:

def onModelLoad(e):
    if e.status == viz.ASYNC_SUCCESS:
        print('Model loaded')
vizact.onAsyncLoad(model, onModelLoad)

To handle the async event within a task function use the <viztask>.waitAsyncLoad command:

import  viztask

def LoadTask():
    
    yield viztask.waitKeyDown(' ')
    
    model = viz.addChild('piazza.osgb', flags=viz.LOAD_ASYNC)
    yield viztask.waitAsyncLoad(model)
    
    print('Finished loading')
    
viztask.schedule( LoadTask() )