cmbruns / pyopenvr

Unofficial python bindings for Valve's OpenVR virtual reality SDK
BSD 3-Clause "New" or "Revised" License
255 stars 41 forks source link

how do you use mDeviceToAbsoluteTracking to find the tracking data for a controller #77

Closed Simontho001 closed 4 years ago

Simontho001 commented 4 years ago

heres my code

import sys
import time
import openvr
import math
def from_matrix_to_pose_dict(matrix):
    pose = {}
    # From http://steamcommunity.com/app/358720/discussions/0/358417008714224220/#c359543542244499836
    position = {}
    position['x'] = matrix[0][3]
    position['y'] = matrix[1][3]
    position['z'] = matrix[2][3]
    q = {}
    q['w'] = math.sqrt(max(0, 1 + matrix[0][0] + matrix[1][1] + matrix[2][2])) / 2.0
    q['x'] = math.sqrt(max(0, 1 + matrix[0][0] - matrix[1][1] - matrix[2][2])) / 2.0
    q['y'] = math.sqrt(max(0, 1 - matrix[0][0] + matrix[1][1] - matrix[2][2])) / 2.0
    q['z'] = math.sqrt(max(0, 1 - matrix[0][0] - matrix[1][1] + matrix[2][2])) / 2.0
    q['x'] = math.copysign(q['x'], matrix[2][1] - matrix[1][2])
    q['y'] = math.copysign(q['y'], matrix[0][2] - matrix[2][0])
    q['z'] = math.copysign(q['z'], matrix[1][0] - matrix[0][1])
    pose['position'] = position
    pose['orientation'] = q
    return pose
openvr.init(openvr.VRApplication_Scene)
poses = []  # will be populated with proper type after first call
for i in range(100):
    poses, _ = openvr.VRCompositor().waitGetPoses(poses, None)
    hmd_pose = poses[openvr.k_unTrackedDeviceIndex_Hmd]
    matrix = (hmd_pose.mDeviceToAbsoluteTracking)
    d = from_matrix_to_pose_dict(matrix)
    print(d)       
    sys.stdout.flush()
    time.sleep(0.2)
openvr.shutdown()

its doing what i want it to except that its getting data from the head mount display and i want it to get data from a controller instead how would i do so?

risa2000 commented 4 years ago

Please, post the code using the code formatting (markdown).

Simontho001 commented 4 years ago

fixed it sorry not very familiar with this website

risa2000 commented 4 years ago

its doing what i want it to except that its getting data from the head mount display and i want it to get data from a controller instead how would i do so?

This code of yours:

poses, _ = openvr.VRCompositor().waitGetPoses(poses, None)

reads poses for all devices, which are currently tracked. This includes headset, lighthouses, controllers. You are reading the first pose:

hmd_pose = poses[openvr.k_unTrackedDeviceIndex_Hmd]

which indeed is a HMD pose. You need to figure out, which pose corresponds to what (i.e. at which index is your controller) and get it there.

Simontho001 commented 4 years ago

I found a little script online that finds the index but what would I do with that I'm not familiar with this library

risa2000 commented 4 years ago

You will get the pose exactly the same way as you did for the HMD:

controller_pose = poses[controller_index]

and then do whatever you want with it.

There is not much documentation for the Python code, as it basically mimics the native API. So for example, for how to use WaitGetPoses you might want to read the official doc (https://github.com/ValveSoftware/openvr/wiki/IVRCompositor::WaitGetPoses), or even read the source code (https://github.com/ValveSoftware/openvr/blob/master/headers/openvr.h).