Skip to content

Gaze Based Interactions

There is an end condition in the GUI for gaze based events that allow you to trigger ending the current trial or progressing to the next one using an onGazeTime event. For additional gaze based triggers see below. 

To add  your own, custom events when an object is in view, (such as gaze based interactions, etc.) you can place code inside the following functions. 

onGazeBegin for your event to be triggered as soon as a gazeObject is seen

onGazeEnd to trigger something to happen when a user’s gaze point is no longer intersecting with that object

onGazeTime to trigger something that happens only when an object is being focused on over the set threshold

Note: If you plan on reviewing with the Session Replay you will need both an onGazeBegin and onGazeEnd function for the replay data to be saved properly

This example code is also in the "ExampleScripts" folder 

import sightlab_utils.sightlab as sl
from sightlab_utils.settings import *
sightlab = sl.SightLab()
sightlab.setEnvironmentDirectory('....ProjectsSampleProjectResourcesenvironments')
#Gaze interaction objects
chick1 = vizfx.addAvatar('sightlab_resources/objects/chick.osgb')
#basketball = vizfx.addChild('basketball.osgb')
chick1.setPosition([0, 0, 2.5])
creature1 = vizfx.addAvatar('sightlab_resources/objects/Creature.osgb')
creature1.setPosition([1.67, 0.5, 2])
creature1.setEuler(22,0,0); 
creature1.state(5)
sightlab.addSceneObject("chick1", chick1, gaze = True, avatar = True)
sightlab.addSceneObject("creature1", creature1, gaze = True,avatar = True)

def gazeActionEnd(e):

    if e.object == sightlab.sceneObjects[GAZE_OBJECTS]['creature1']:
        creature1.state(4)
        print('stopped looking')

    if e.object == sightlab.sceneObjects[GAZE_OBJECTS]['chick1']:
        chick1.state(0)
        print('stopped looking')

def gazeActionStart(e):

    if e.object == sightlab.sceneObjects[GAZE_OBJECTS]['creature1']:
        creature1.state(6)
        print('saw creature')

    if e.object == sightlab.sceneObjects[GAZE_OBJECTS]['chick1']:
        chick1.state(10)
        print('saw chicken')
vizact.addCallback(sightlab.GAZE_TIME_EVENT, gazeActionStart)    
vizact.addCallback(sightlab.GAZE_END_EVENT, gazeActionEnd)
import viztask

def sightLabExperiment():
 while True:
  yield viztask.waitKeyDown(' ')
  yield sightlab.startTrial()
  yield viztask.waitKeyDown(' ')
  yield sightlab.endTrial()
viztask.schedule(sightlab.runExperiment)
viztask.schedule(sightLabExperiment)

Multi-User

On SightLabVR_Server.py script: 

Add Network events then create a function for gazeActionstart and for gazeActionEnd, add the code for whatever you want to have happen (i.e. a print statement) inside the function. Callback the functions with the network event.

#Generate IDs for events
def id(name):
    """Generate unique ID for use over viznet"""
    return f'viznet:{name}'

FIXATION_NETWORK_KEY_EVENT = id('FIXATION_NETWORK_KEY_EVENT')
FIXATION_STOPPED_NETWORK_KEY_EVENT= id('FIXATION_STOPPED_NETWORK_KEY_EVENT')

def gazeActionEnd(e):
    if e.name == "creature1":
        creature1.setScale(1,1,1)
        print('stopped looking')

    if e.name == "chick1":
        chick1.setScale(1,1,1)
        print('stopped looking')

def gazeActionStart(e):
    if e.name == "creature1":
        creature1.setScale(1.5,1.5,1.5)
        print('saw creature')

    if e.name == "chick1":
        chick1.setScale(1.5,1.5,1.5)
        print('stopped looking')

vizact.addCallback(NETWORK_GAZE_TIME_EVENT, gazeActionStart)    
vizact.addCallback(NETWORK_GAZE_END_EVENT, gazeActionEnd)

On SightLabVR_Client.py script: 

#Generate IDs for events
def id(name):
    """Generate unique ID for use over viznet"""
    return f'viznet:{name}'

FIXATION_NETWORK_KEY_EVENT = id('FIXATION_NETWORK_KEY_EVENT')
FIXATION_STOPPED_NETWORK_KEY_EVENT= id('FIXATION_STOPPED_NETWORK_KEY_EVENT')

def gazeActionEnd(e):
    if e.gazeData.name == "creature1":
        creature1.setScale(1,1,1)
        print('stopped looking')

    if e.gazeData.name == "chick1":
        chick1.setScale(1,1,1)
        print('stopped looking')


def gazeActionStart(e):
    if e.gazeData.name == "creature1":
        creature1.setScale(1.5,1.5,1.5)
        print('saw creature')

    if e.gazeData.name == "chick1":
        chick1.setScale(1.5,1.5,1.5)
        print('stopped looking')

vizact.addCallback(NETWORK_GAZE_TIME_EVENT, gazeActionStart)    
vizact.addCallback(NETWORK_GAZE_END_EVENT, gazeActionEnd)