Search code examples
pythonpython-3.xwebsocketobs

Python OBS-WebSocket-Py Set Source Visibility


I have been trying to work with the obs-websocket-py (https://github.com/Elektordi/obs-websocket-py) Python library to control OBS Studio (version 29.0.2) via a remote script. I have done this before with C# libraries but having some trouble with the Python (version 3.11.8) versions.

I can connect to the websocket without issue and can change scene easily enough. However, I can't seem to set a sources visibility. I have gone through both the current version and older versions of the implementation and had no success, nor am I getting an error to point me to where I'm going wrong.

from obswebsocket import obsws, requests

SOCK = {}
INITIALISED = False

# Create socket and connect to OBS websocket before returning socket object
def create_connection():
    global INITIALISED
    sock = obsws(IP, PORT, PASSWORD)
    sock.connect()
    INITIALISED = True
    return sock

# Call OBS to set visibility of specified source
def activate_source(source, visibility, reset = -1):
    global SOCK
    if INITIALISED == False:
        SOCK = create_connection()
    input_settings = {
        "visibility": True,
        "visibile": 'true'
    }
   

    response = SOCK.call(requests.SetInputSettings(inputName="Art 8 - Image", inputSettings=input_settings, overlay=True))

If anyone can provide working code samples or point to working code examples then that would also be appreciated. I have been trying to work with the documentation (https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md) for it but struggling to fully understand what its looking for in terms of code implementation


Solution

  • Turns out that I was calling the wrong function in requests, I actually wanted SetSceneItemEnabled instead of SetInputSettings. To enable a source in a scene, the code is as shown below

    from obswebsocket import obsws, requests
    
    SOCK = {}
    INITIALISED = False
    
    def create_connection():
        global INITIALISED
        sock = obsws(IP, PORT, PASSWORD)
        sock.connect()
        INITIALISED = True
        return sock
    
    def activate_source(source, visibility, reset = -1):
        global SOCK
        if INITIALISED == False:
            SOCK = create_connection()
    
        # Using hard coded values
        SOCK.call(requests.SetSceneItemEnabled(sceneName="Random Art", sceneItemId=8, sceneItemEnabled=True))