[Python] VR headset Controller


#1

openvr VR Headset Controller Tutorial

Setup

To enable the VR in HARFANG we need to switch to a specific FrameRenderer.
First, we ask if the frame renderer plugin called “VR” is available.

openvr_frame_renderer = hg.CreateFrameRenderer("VR")

Then we initialize the renderer to setup OpenVR system. It can return true or false.

if openvr_frame_renderer.Initialize(plus.GetRenderSystem()):

Once initialized, we add the system in the scene which will take care of the update of the head/controllers matrices and the rendering with the correct transformation.

scn.GetRenderableSystem().SetFrameRenderer(openvr_frame_renderer)

Update controllers matrices

HARFANG gives the controllers’ matrices in the real world. The matrices relate to the VR calibration position.
To set the controllers’ matrices in the virtual world, we need to multiply them by the position that represent the calibration center in the scene. From our point of view, this is the camera matrix.

In real world

In virtual world

Example:

To get the controller

  • "VR Controller 0" : Right handheld controller
  • "VR Controller 1" : Left handheld controller
  • "HMD" : headset controller
controller = hg.GetInputSystem().GetDevice("VR Controller 0")

To get the controller’s matrix

controller_mat = controller.GetMatrix(hg.InputDeviceMatrixHead)

To set the controller’s matrix to the geometry in the virtual world

virtual_controller_mat = cam_matrix * controller_mat
controller_node.GetTransform().SetWorld(virtual_controller_mat)

About headset matrix

If you need to get the matrix of the head, multiply the camera matrix by the headset matrix.

controller = hg.GetInputSystem().GetDevice("HMD")
controller_mat = controller.GetMatrix(hg.InputDeviceMatrixHead)
cam_matrix = scene.GetCurrentCamera().GetTransform().GetWorld()
head_mat = cam_matrix * controller_mat

Conclusion

That’s it, you are now VR-enabled right within Python!
This tutorial using the HARFANG engine was tested so far on the HTC Vive, the Oculus Rift and the Lenovo Mixer Reality headsets.
The same should apply to every OpenVR-compatible headset.


#2

Thanks Eric :slight_smile:
To dive into this tutorial quickly, please find below the Resource Files (including the Python source files and the 3D assets) :
https://github.com/harfang3d/vr-python-quickstart