Implementation of a custom device interaction

This example shows how a custom device interaction can be implemented. First, it is necessary to create an object for the custom interaction using the device service.

5self.customInteraction = vrDeviceService.createInteraction("CustomInteraction")

As the custom interaction should use device actions that are already in use by default interactions (left-trigger-pressed, left-trigger-released), it needs to be added to another interaction group.

7self.customInteraction.setSupportedInteractionGroups(["CustomGroup"])

Interaction groups combine different interactions for a certain use case. Within one interaction group each action, like left-trigger-pressed, is only allowed to trigger a single function. This is to prevent unwanted side effects. In this case a new interaction group is used that only contains the custom interaction. The device actions that reflect the button presses on a VR controller need to be created for the device interaction.

10self.grabAction = self.customInteraction.createControllerAction("left-trigger-pressed")
11self.releaseAction = self.customInteraction.createControllerAction("left-trigger-released")

After that, the newly created device actions can connect their signals to the corresponding methods that should be executed.

14self.grabAction.signal().triggered.connect(self.press)
15self.releaseAction.signal().triggered.connect(self.release)

It is necessary to activate the interaction group for this interaction.

18vrDeviceService.setActiveInteractionGroup("CustomGroup")

The move of a controller is a special signal that is needed by multiple interactions and for this reason it is not handled as a device action. Here, the moved signal is only connected when needed.

22device.signal().moved.connect(self.move)

When the interaction is used, it will only print out “press”, “release” and “move” as long as the trigger is pressed.

vr/customInteraction.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# Define actions as python functions
 4class MyCustomInteraction:
 5    def __init__(self):
 6        # Create new interaction
 7        self.customInteraction = vrDeviceService.createInteraction("CustomInteraction")
 8        # Limit the interaction to a new mode to not interfere with other interactions
 9        self.customInteraction.setSupportedInteractionGroups(["CustomGroup"])
10
11        # Create action objects that a triggered by some input
12        self.grabAction = self.customInteraction.createControllerAction("left-trigger-pressed")
13        self.releaseAction = self.customInteraction.createControllerAction("left-trigger-released")        
14
15        # Connect these actions to the actual python functions
16        self.grabAction.signal().triggered.connect(self.press)
17        self.releaseAction.signal().triggered.connect(self.release)        
18
19        # Activate the mode that supports the new interaction
20        vrDeviceService.setActiveInteractionGroup("CustomGroup")
21    
22    def press(self, action, device):
23        print("press")
24        device.signal().moved.connect(self.move)
25
26    def release(self, action, device):
27        print("release")
28        device.signal().moved.disconnect(self.move)
29
30    def move(self, device):
31        print("move")
32
33myCustomInteraction = MyCustomInteraction()