Cant connect to OBS

Fairly new to python.
trying to setup swapping to a random scene in OBS through Python and really struggling.
im sure im missing something easy but im going off pretty old tutorials
This is my script in Python
Ive also installed pip install obs-websocket-py in the terminal

edit: i get this error
line 15, in
ws.call(requests.SetCurrentScene({“Normal”: random_scene}))
TypeError: init() takes 1 positional argument but 2 were given

import time
import random
from obswebsocket import obsws, requests

# Connect to OBS WebSocket server
ws = obsws("xxxxxx", 4455, "xxxxxxx") 
ws.connect()

scenes = ['Scroll', 'Normal', 'clouds']

try:
    while True:
        random_scene = random.choice(scenes)
        ws.call(requests.SetCurrentScene({"scene-name": random_scene}))

        print(f"Switched to '{random_scene}' scene.")

        time.sleep(30)

except KeyboardInterrupt:
    ws.disconnect()
    print('Disconnected from OBS')

I’m not familiar with the obswebsocket package, but I’m assuming you mean this one?

Wha happens when you try this? Do you get any sort of exceptions?

Have you set up OBS to have the necessary password, and is it listening on the IP you expect it to be on? (Probably easiest to run everything on the same computer and use localhost, at least for initial testing.)

sorry my bad, forgot to post the error in the post, its this

line 15, in
ws.call(requests.SetCurrentScene({“Normal”: random_scene}))
TypeError: init() takes 1 positional argument but 2 were given

everything password and port wised is the same and its all on the same computer

Looking at their examples, it seems they use keyword arguments, not a dictionary:

That would explain why it’s not expecting any positional arguments.

Ahhhhh i see where im going wrong
Just got it working now so that it will change to a random scene for before swapping back to my default
Not sure how pretty the coding is but as long as it works im happy
Thanks for the help really appreciate it

import sys
import time
import random

import logging
logging.basicConfig(level=logging.DEBUG)

sys.path.append('../')
from obswebsocket import obsws, requests  # noqa: E402

host = "localhost"
port = 4455
password = "xxxxxxx"

ws = obsws(host, port, password)
ws.connect()

try:
    while True:
        scenes = ws.call(requests.GetSceneList())
        random_scene = random.choice(scenes.getScenes())['sceneName']

        print("Switching to {}".format(random_scene))
        ws.call(requests.SetCurrentProgramScene(sceneName=random_scene))
        time.sleep(30)

        print("Switching to 'Normal'")
        ws.call(requests.SetCurrentProgramScene(sceneName='Normal'))
        time.sleep(120)

        print(f"Switched to {random_scene}")

except KeyboardInterrupt:
    pass
1 Like