Skip to content
VR Headset Controller Tutorial, get started with virtual reality in Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


OpenVR VR Headset Controller Tutorial



HARFANG® 3D >= 1.2.0

On Windows, retrieve HARFANG® 3D using PIP

pip install -r requirements.txt

On others systems, retrieve the last wheel on HARFANG® 3D website.

pip install harfang.whl


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.


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



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

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


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.

(Tutorial created by Eric Kernin, VR Teleporter by Thomas Simonnet).

You can’t perform that action at this time.