Define and use virtual buttons on the touchpad of a VR controller

This shows how to add five virtual buttons to a touchpad of a VR controller. One button will be in the center and four buttons of same size will be placed around the center button. This example implements a device interaction that contains five device actions for the press of each button. The connected methods will print out which button has been pressed. Here, only the left controller is used. Note that the interaction is part of an interaction group that only contains this interaction, which means that other interactions are not available, when this interaction group is active.

vr/virtualControllerButtons.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# Define actions as python functions
 4class VirtualPad:
 5
 6    def __init__(self):
 7        # Get the left controller
 8        self.leftController = vrDeviceService.getVRDevice("left-controller")
 9
10        # Define several buttons on the touchpad
11        self.padCenter = vrdVirtualTouchpadButton("padcenter", 0.0, 0.5, 0.0, 360.0)
12        self.padLeft = vrdVirtualTouchpadButton("padleft", 0.5, 1.0, 225.0, 315.0)
13        self.padUp = vrdVirtualTouchpadButton("padup", 0.5, 1.0, 315.0, 45.0)
14        self.padRight = vrdVirtualTouchpadButton("padright", 0.5, 1.0, 45.0, 135.0)
15        self.padDown = vrdVirtualTouchpadButton("paddown", 0.5, 1.0, 135.0, 225.0)
16
17        # Add the virtual buttons to the controller
18        self.leftController.addVirtualButton(self.padCenter, "touchpad")
19        self.leftController.addVirtualButton(self.padLeft, "touchpad")
20        self.leftController.addVirtualButton(self.padUp, "touchpad")
21        self.leftController.addVirtualButton(self.padRight, "touchpad")
22        self.leftController.addVirtualButton(self.padDown, "touchpad")
23
24        # Create new interaction
25        self.multiButtonPad = vrDeviceService.createInteraction("MultiButtonPad")
26        # Limit the interaction to a new mode to not interfere with other interactions
27        self.multiButtonPad.setSupportedInteractionGroups(["VirtualButtons"])
28
29        # Create action objects that a triggered by some input
30        self.leftAction = self.multiButtonPad.createControllerAction("left-padleft-pressed")
31        self.upAction = self.multiButtonPad.createControllerAction("left-padup-pressed")
32        self.rightAction = self.multiButtonPad.createControllerAction("left-padright-pressed")
33        self.downAction = self.multiButtonPad.createControllerAction("left-paddown-pressed")
34        self.centerAction = self.multiButtonPad.createControllerAction("left-padcenter-pressed")
35
36        # Connect these actions to the actual python functions
37        self.leftAction.signal().triggered.connect(self.left)
38        self.upAction.signal().triggered.connect(self.up)
39        self.rightAction.signal().triggered.connect(self.right)
40        self.downAction.signal().triggered.connect(self.down)
41        self.centerAction.signal().triggered.connect(self.center)
42
43        # Activate the mode that supports the new interaction
44        vrDeviceService.setActiveInteractionGroup("VirtualButtons")
45
46    def left(self, action, device):
47        print("left")
48
49    def up(self, action, device):
50        print("up")
51
52    def right(self, action, device):
53        print("right")
54
55    def down(self, action, device):
56        print("down")
57
58    def center(self, action, device):
59        print("center")
60
61pad = VirtualPad()