Selector Tool
Overview
The Selector Tool is designed for interacting with objects in a VR scene using a highlighter mechanism. This document outlines its functionality, implementation, and usage.
Default Controls
- VR Mode:
- Controller grip button (right or left): Turn on the selector (for Vive controllers, these are the Left and Right system buttons)
- Trigger: Use the selector
- Desktop Mode:
- Right mouse button: Toggle the selector on/off
- Left mouse button: Use the selector
Examples
Locate the following files in the ExampleScripts\Selector Tool
directory (also in Multi User ExamplesScripts\Selector Tool
):
Selector_Tool_GUI
- Automatically targets objects tagged for interactions
- Displays a window with the name of the highlighted object
Selector_Tool_GUI_Grab
- Allows grabbing objects from a distance
- Left click to select and move objects, left click again to place
Selector_Tool
- A simple non-GUI example
Multi User
- Example for Multiple Users
Implementation Guide
1. Add From Main Class
sightlab = sl.SightLab(highlighttool = True)
2. Next Steps
After (sightlab.startTrial() or yield viztask.waitEvent(TRIAL_START)
Set up global variables:
isConfirmingTarget = False
currentHighlightedObject = None
3. Define Target Objects
Manually:
soccerball = env.getChild('soccerball')
basketball = env.getChild('basketball')
target_objects = [basketball, soccerball]
for obj in target_objects:
sightlab.setHighlighter(mode = sl.highlighter.MODE_BOX, setItems = target_objects)
targetObject = basketball
Automatically from GUI:
target_names = list(sightlab.sceneObjects[GAZE_OBJECTS].keys())
target_objects = {}
object_name_map = {}
for name in target_names:
obj = env.getChild(name)
if obj:
target_objects[name] = obj
object_name_map[obj] = name
sightlab.setHighlighter(mode = sl.highlighter.MODE_BOX, setItems = (list(target_objects.values())))
targetObject = list(target_objects.values())
4. Define Callback Functions
(After "Action to Perform," place the action you want to happen when the target is clicked on.)
def confirmTarget(e):
global isConfirmingTarget, currentHighlightedObject
isConfirmingTarget = True
if currentHighlightedObject == targetObject:
# Action to perform
print('Object is Targeted')
isConfirmingTarget = False
def onHighlight(e):
global currentHighlightedObject
currentHighlightedObject = e.new
if isConfirmingTarget and currentHighlightedObject == targetObject:
print(f'{currentHighlightedObject} is highlighted')
5. Set Up Event Callbacks
viz.callback(viz.getEventID("triggerPress"), confirmTarget)
viz.callback(sl.highlighter.HIGHLIGHT_EVENT, onHighlight)
Multi User
For multi-user just add this code for the clients (not needed on the server)
sightlab = sl.SightLabClient(highlighttool = True)
Highlight and confirm events are then sent as network events and callbacks
def id(name):
"""Generate unique ID for use over viznet"""
return f'viznet:{name}'
ON_HIGHLIGHT_EVENT = id('ON_HIGHLIGHT_EVENT')
ON_HIGHLIGHT_EVENT2 = id('ON_HIGHLIGHT_EVENT2')
CONFIRM_EVENT = id('CONFIRM_EVENT')
CONFIRM_EVENT2 = id('CONFIRM_EVENT2')
STOP_HIGHLIGHT_EVENT = id('STOP_HIGHLIGHT_EVENT')
target_objects = [targetObject]
for obj in target_objects:
sightlab.setHighlighter(mode = sl.highlighter.MODE_BOX, setItems = target_objects)
#Global variables and key event handlers
global isConfirmingTarget, currentHighlightedObject
isConfirmingTarget = False
currentHighlightedObject = None
def confirmTarget(e):
global isConfirmingTarget
isConfirmingTarget = True
if currentHighlightedObject == targetObject:
print('targetObject is confirmed')
viznet.client.sendAll(CONFIRM_EVENT, client=viz.net.getName())
elif currentHighlightedObject == targetObject2:
print('targetObject2 is confirmed')
viznet.client.sendAll(CONFIRM_EVENT2, client=viz.net.getName())
isConfirmingTarget = False
viz.callback(viz.getEventID('triggerPress'), confirmTarget)
viz.callback(viz.getEventID('triggerPressLeft'), confirmTarget)
def onHighlight(e):
global currentHighlightedObject
if e.new: # Check if there's a new object being highlighted
currentHighlightedObject = e.new
if currentHighlightedObject == targetObject:
print('This happens on highlight for target 1')
viznet.client.sendAll(ON_HIGHLIGHT_EVENT, client=viz.net.getName())
if isConfirmingTarget:
print(f'{targetObject} is highlighted')
elif currentHighlightedObject == targetObject2:
print('This happens on highlight for target 2')
viznet.client.sendAll(ON_HIGHLIGHT_EVENT2, client=viz.net.getName())
if isConfirmingTarget:
print(f'{targetObject2} is highlighted')
else: # No object is highlighted, reset the alpha
if currentHighlightedObject == targetObject:
viznet.client.sendAll(STOP_HIGHLIGHT_EVENT, client=viz.net.getName())
elif currentHighlightedObject == targetObject2:
viznet.client.sendAll(STOP_HIGHLIGHT_EVENT, client=viz.net.getName())
currentHighlightedObject = None
viz.callback(sl.highlighter.HIGHLIGHT_EVENT, onHighlight)
#Receive highlight or confirm from other clients and place on server script
def highlightAction(e):
print('receive highlight action from other client for target 1')
viz.callback(ON_HIGHLIGHT_EVENT, highlightAction)
def highlightAction2(e):
print('receive highlight action from other client for target 2')
viz.callback(ON_HIGHLIGHT_EVENT2, highlightAction2)
def noHighlightAction(e):
print('stop highlight actions')
viz.callback(STOP_HIGHLIGHT_EVENT, noHighlightAction)
def receiveConfirm1(e):
print('received confirm from other client for target 1')
viz.callback(CONFIRM_EVENT, showInfo)
def receiveConfirm2(e):
print('received confirm from other client for target 2')
viz.callback(CONFIRM_EVENT2, showInfo2)
For more information, see this page in the Vizard Documentation.