カスタム デバイスのインタラクションの実装

このサンプルでは、カスタム デバイスのインタラクションを実装する方法を示します。最初に、デバイス サービスを使用してカスタム インタラクションのためのオブジェクトを作成する必要があります。

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

カスタム インタラクションでは、既定のインタラクション(left-trigger-pressed、left-trigger-released)で既に使用されているデバイス アクションを使用する必要があるため、別のインタラクション グループに追加する必要があります。

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

インタラクション グループは、特定の使用事例のために異なるインタラクションを組み合わせます。1 つのインタラクション グループ内で、left-trigger-pressed などの各アクションがトリガできる機能は 1 つのみです。これは、不要な副作用を防ぐためです。この場合、カスタム インタラクションのみを含む新しいインタラクション グループが使用されます。VR コントローラでボタンを押す動作を反映するデバイス アクションは、デバイスのインタラクションに対して作成する必要があります。

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

その後、新しく作成されたデバイス アクションは、実行する必要のある対応するメソッドにその信号を接続することができます。

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

このインタラクションのインタラクション グループをアクティブにする必要があります。

vrDeviceService.setActiveInteractionGroup("CustomGroup")

コントローラの移動は、複数のインタラクションで必要とされる特殊な信号です。このため、デバイス アクションとしては処理されません。ここでは、移動した信号は必要な場合にのみ接続されます。

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

このインタラクションを使用すると、トリガが押されている限り、「press」、「release」、「move」のみが出力されます。

vr/customInteraction.py

# Define actions as python functions
class MyCustomInteraction:
    def __init__(self):
        # Create new interaction
        self.customInteraction = vrDeviceService.createInteraction("CustomInteraction")
        # Limit the interaction to a new mode to not interfere with other interactions
        self.customInteraction.setSupportedInteractionGroups(["CustomGroup"])

        # Create action objects that a triggered by some input
        self.grabAction = self.customInteraction.createControllerAction("left-trigger-pressed")
        self.releaseAction = self.customInteraction.createControllerAction("left-trigger-released")        

        # Connect these actions to the actual python functions
        self.grabAction.signal().triggered.connect(self.press)
        self.releaseAction.signal().triggered.connect(self.release)        

        # Activate the mode that supports the new interaction
        vrDeviceService.setActiveInteractionGroup("CustomGroup")

    def press(self, action, device):
        print("press")
        device.signal().moved.connect(self.move)

    def release(self, action, device):
        print("release")
        device.signal().moved.disconnect(self.move)

    def move(self, device):
        print("move")

myCustomInteraction = MyCustomInteraction()