Last active
May 8, 2023 01:27
-
-
Save Specnr/eae7354a07996c440a078e102cae90fe to your computer and use it in GitHub Desktop.
A OBS script which sets and swaps players around for tournaments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import obspython as S | |
import os | |
import csv | |
# CONFIG | |
player_format = "p" | |
focus_scene = "4 Players" | |
main_scene = "6 Players" | |
last_update = 0.0 | |
VERSION = "1.1.0" | |
path = os.path.dirname(os.path.realpath(__file__)) | |
obstxtPath = os.path.abspath(os.path.realpath( | |
os.path.join(path, 'data', 'obs.txt'))) | |
def updatePlayer(number, name): | |
print("Updating browser", number, "to be", name) | |
source = S.obs_get_source_by_name(f"{player_format}{number}") | |
settings = S.obs_source_get_settings(source) | |
url = f"https://player.twitch.tv/?channel={name}&enableExtensions=true&muted=true&parent=twitch.tv&player=popout&quality=chunked&volume=0.01" | |
S.obs_data_set_string(settings, "url", url) | |
S.obs_source_update(source, settings) | |
S.obs_source_release(source) | |
S.obs_data_release(settings) | |
def getUserFromSource(number): | |
source = S.obs_get_source_by_name(f"{player_format}{number}") | |
settings = S.obs_source_get_settings(source) | |
url = S.obs_data_get_string(settings, "url") | |
S.obs_source_release(source) | |
S.obs_data_release(settings) | |
return url.split("channel=")[1].split("&enable")[0] | |
def switchToScene(scene): | |
scene_item = S.obs_get_scene_by_name(scene) | |
scene_source = S.obs_scene_get_source(scene_item) | |
S.obs_scene_release(scene_item) | |
if not scene_source: | |
print(f"Could not find instance scene '{scene}'") | |
S.obs_frontend_set_current_scene(scene_source) | |
def get_cmd(path): | |
cmdFiles = [] | |
cmd = [] | |
for _, _, files in os.walk(path): | |
for filename in files: | |
cmdFiles.append(os.path.abspath(os.path.join(path, filename))) | |
oldest_file = min(cmdFiles, key=os.path.getctime) | |
while (cmd == []): | |
try: | |
with open(oldest_file) as cmd_file: | |
csv_reader = csv.reader(cmd_file, delimiter=",") | |
for row in csv_reader: | |
for value in row: | |
cmd.append(value) | |
except: | |
cmd = [] | |
os.remove(oldest_file) | |
return | |
def execute_cmd(): | |
global last_update | |
global player_format | |
current_time = os.path.getmtime(obstxtPath) | |
if current_time == last_update: | |
return | |
last_update = current_time | |
with open(obstxtPath, "r") as f: | |
user_cmd = f.read().strip() | |
if user_cmd == "": | |
print("Empty command found, will not perform anything.") | |
elif user_cmd == "w": | |
i = 1 | |
while(True): | |
try: | |
print(f"Player {i}: {getUserFromSource(i)}") | |
except: | |
break | |
i += 1 | |
# Switch two | |
elif user_cmd.startswith("sw"): | |
if user_cmd[2].isnumeric() and user_cmd[3].isnumeric(): | |
if user_cmd[2] != user_cmd[3]: | |
a, b = getUserFromSource( | |
user_cmd[2]), getUserFromSource(user_cmd[3]) | |
updatePlayer(int(user_cmd[2]), b) | |
updatePlayer(int(user_cmd[3]), a) | |
# Set | |
elif user_cmd.startswith("s"): | |
if user_cmd[1].isnumeric(): | |
number, name = user_cmd[1].strip(), user_cmd[2:].strip() | |
updatePlayer(int(number), name) | |
# Focus | |
elif user_cmd.startswith("f"): | |
if user_cmd[1].isnumeric(): | |
if user_cmd[1] != "1": | |
current_focus = getUserFromSource(1) | |
updatePlayer(int(user_cmd[1]), current_focus) | |
updatePlayer(1, user_cmd[2:]) | |
switchToScene(focus_scene) | |
# Focus | |
elif user_cmd.startswith("m"): | |
switchToScene(main_scene) | |
else: | |
print("Didn't recognize your command, try again.") | |
def script_description(): | |
return f"Tournanment Player Controller {VERSION}.\n" | |
def script_update(settings): | |
with open(obstxtPath, "w") as f: | |
pass | |
S.timer_remove(execute_cmd) | |
S.timer_add(execute_cmd, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment