Add VRC OSC tooling

This commit is contained in:
messypy
2026-06-03 21:39:55 +09:00
committed by every_holiday
parent 072e819b0b
commit 54854969b9
24 changed files with 2982 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
import pyautogui
import pygetwindow as gw
from common.project_paths import RUNTIME_DIR
SCREENSHOT_PATH = RUNTIME_DIR / "latest_screenshot.png"
OCR_SCREENSHOT_PATH = RUNTIME_DIR / "latest_ocr.png"
EXCLUDED_WINDOW_KEYWORDS = [
"Google Chrome",
"Microsoft Edge",
"Mozilla Firefox",
"Visual Studio Code",
"PowerShell",
"Windows Terminal",
"ChatGPT",
]
def log(level, message):
from datetime import datetime
now = datetime.now().strftime("%H:%M:%S")
print(f"[{level}] {now} {message}", flush=True)
def findVrchatWindow():
windows = gw.getAllWindows()
candidates = []
for window in windows:
title = window.title or ""
normalized = title.strip().lower()
if not normalized:
continue
if any(keyword.lower() in normalized for keyword in EXCLUDED_WINDOW_KEYWORDS):
continue
if normalized == "vrchat":
candidates.append((0, window))
continue
if normalized.startswith("vrchat "):
candidates.append((1, window))
continue
if normalized.startswith("vrchat"):
candidates.append((2, window))
if not candidates:
visible_titles = []
for window in windows:
title = (window.title or "").strip()
if title:
visible_titles.append(title)
log("ERROR", "VRChatウィンドウが見つかりません")
log("CHECK", "現在検出できるウィンドウ")
for title in visible_titles[:30]:
print(title, flush=True)
raise RuntimeError("VRChatウィンドウが見つかりません")
candidates = sorted(
candidates,
key=lambda item: (item[0], -(item[1].width * item[1].height)),
)
window = candidates[0][1]
if window.width <= 0 or window.height <= 0:
raise RuntimeError("VRChatウィンドウサイズが不正です")
return window
def getVrchatRegion():
window = findVrchatWindow()
try:
if window.isMinimized:
window.restore()
except Exception:
pass
left = int(window.left)
top = int(window.top)
width = int(window.width)
height = int(window.height)
if width <= 0 or height <= 0:
raise RuntimeError("VRChatウィンドウ範囲が不正です")
log("INFO", f"VRChat window title={window.title}")
log("INFO", f"VRChat region left={left} top={top} width={width} height={height}")
return left, top, width, height
def captureVrchatWindowTo(image_path):
image_path.parent.mkdir(parents=True, exist_ok=True)
left, top, width, height = getVrchatRegion()
image = pyautogui.screenshot(region=(left, top, width, height))
image.save(image_path)
log("ACTION", f"captureVrchatWindow saved={image_path}")
return image_path
def captureVrchatWindow():
return captureVrchatWindowTo(SCREENSHOT_PATH)
def captureOcrRegion():
return captureVrchatWindowTo(OCR_SCREENSHOT_PATH)
def captureScreen():
return captureVrchatWindow()
def takeScreenshot():
return captureVrchatWindow()