Skip to content

Instantly share code, notes, and snippets.

@PiMaker
Last active March 21, 2024 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PiMaker/73c3b27f35c8293716fd50fa2749a135 to your computer and use it in GitHub Desktop.
Save PiMaker/73c3b27f35c8293716fd50fa2749a135 to your computer and use it in GitHub Desktop.
# made by pi (and copilot), available as CC0, no attribution required, use at your own risk, yadda yadda
# requires websocket-client from pip
import websocket
import json
import time
import threading
import re
import sys
# Tweak this value to your liking!
# 8-10 seems to work well for my Vive Pro 1 on 7900XTX
desiredOffset = 8
if len(sys.argv) > 1:
desiredOffset = int(sys.argv[1])
msgInc = "mailbox_send vrcompositor_mailbox {\"type\": \"vsync_to_photons_increment\"}"
msgDec = "mailbox_send vrcompositor_mailbox {\"type\": \"vsync_to_photons_decrement\"}"
currentOffset = 99999
def on_message(ws, message):
global currentOffset
j = json.loads(message)
msg = j["sMessage"]
m = re.search('VSyncToPhotonsOffsetMs=([-\d]+)', msg)
if m:
currentOffset = int(m.group(1))
def on_open(ws):
ws.send("console_open")
# trigger once so we have it in console for sure
ws.send(msgInc)
ws.send(msgDec)
threading.Thread(target=wait_for_result).start()
def wait_for_result():
global ws
global currentOffset
global desiredOffset
print("Waiting for current offset...")
time.sleep(1)
print("Current offset: " + str(currentOffset))
if currentOffset == 99999:
print("Error: offset not found")
else:
cur = currentOffset
print("Offset found: " + str(cur))
print("Adjusting offset by " + str(desiredOffset - cur) + "...")
while cur > desiredOffset:
ws.send(msgDec)
cur -= 1
while cur < desiredOffset:
ws.send(msgInc)
cur += 1
print("Waiting for confirmation...")
time.sleep(1)
print("New offset: " + str(currentOffset) + " (target: " + str(desiredOffset) + ")")
ws.close()
print("Done!")
print("Setting vsync_to_photons to " + str(desiredOffset) + "ms")
print("Connecting to SteamVR...")
ws = websocket.WebSocketApp("ws://localhost:27062", on_open=on_open, on_message=on_message)
ws.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment