Skip to content

Setting Flags

See ExampleSripts Setting Flags

import sightlab_utils.sightlab as sl
from sightlab_utils.settings import *

sightlab = sl.SightLab()
# run sightlab experiment
import viztask



def sightLabExperiment():
    yield viztask.waitKeyDown(" ")
    yield sightlab.startTrial()
    print("experiment start")

    # Example that drops a flag when you press the ‘t’ key:

    def buttonTrigger():
        sightlab.setCustomTrialData("T key pressed")
        print("T key pressed")

    vizact.onkeydown("t", buttonTrigger)

    # Example of a flag that drops when you click the trigger on your controller.
    # This one gets the callback from an event you can create in your vizconnect file.
    # Default is either trigger with controllor or 'v' key

    def rightHandTrigger(e):
        sightlab.setCustomTrialData("sensor pressed")
        print("sensor pressed")

    viz.callback(viz.getEventID("triggerPress"), rightHandTrigger)
    yield viztask.waitKeyDown(" ")
    yield sightlab.endTrial()
    print("experiment end")


# For manually using a button, such as a SteamVR controller trigger:

if not sightlab.getConfig() in ["Desktop"]:
    import steamvr
    import vizact

    controller = steamvr.getControllerList()[1]


    def controllerTest():
        print("test")

    vizact.onsensordown(controller, steamvr.BUTTON_TRIGGER, controllerTest)
viztask.schedule(sightlab.runExperiment)
viztask.schedule(sightLabExperiment)
vizact.onkeydown(" ", sightlab.startTrial)

Setting Flags Using a Network Event

You can also drop a flag from another Vizard script (or an external application) by sending a network event to the SightLab experiment. This is useful for triggering flags from a second machine, a control station, or any external process.

Receiver (SightLab experiment)

Register a viz.NETWORK_EVENT callback inside the trial. Positional arguments sent over the network arrive in e.data, and keyword arguments arrive as attributes on the event (e.g. e.flag):

import sightlab_utils.sightlab as sl
from sightlab_utils.settings import *

sightlab = sl.SightLab()

# run sightlab experiment
import viztask


def sightLabExperiment():
    yield viztask.waitEvent(EXPERIMENT_START)
    viz.callback(viz.getEventID("ResetPosition"), sightlab.resetViewPoint)

    for trial in range(sightlab.getTrialCount()):
        yield viztask.waitEvent(TRIAL_START)

        # Example that drops a flag when a network event is received.
        # Any other Vizard script (or external app) can trigger this by sending
        # a network message to this machine (see the sender script below).
        def onNetwork(e):
            # e.data holds the positional arguments, keyword arguments are
            # available as attributes (e.g. e.flag)
            flagName = getattr(e, "flag", "network event")
            sightlab.setCustomTrialData("Network flag: " + str(flagName))
            print("Network event received. Positional data:", e.data)
            print("Flag set:", flagName)

        viz.callback(viz.NETWORK_EVENT, onNetwork)

        yield viztask.waitEvent(TRIAL_END)


viztask.schedule(sightlab.runExperiment)
viztask.schedule(sightLabExperiment)
vizact.onkeydown(" ", sightlab.startTrial)

Sender (second Vizard instance)

Run this in a second Vizard instance. Every spacebar press sends a network event that drops a flag in the receiving experiment. Use 'localhost' when both scripts run on the same machine, otherwise replace it with the computer name or IP address of the receiving machine:

import viz
import vizact

viz.go()

network = viz.addNetwork("localhost")


def sendFlag():
    # Positional args end up in e.data, keyword args become attributes on
    # the event (e.g. e.flag) in the receiver's NETWORK_EVENT callback
    network.send(1, 2, 3, flag="spacebar pressed on sender")
    print("Network flag sent")


# Sends an event every time the spacebar is pressed
vizact.onkeydown(" ", sendFlag)

Note: in the receiver, spacebar is bound to sightlab.startTrial, so make sure the sender window has focus when pressing spacebar to send flags.