Elektordi / obs-websocket-py

Python library to communicate with an obs-websocket server (for OBS Studio)
MIT License
235 stars 59 forks source link

How I can get the value of an call request? #89

Closed marcus-universe closed 10 months ago

marcus-universe commented 10 months ago

I read out my Drohne Source in OBS if its active or not active.

from obswebsocket import obsws, events, requests as obs_requests

host = "localhost"
port = 4455
password = "****"

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

prevDroneState = ws.call(obs_requests.GetSourceActive(sourceName='Drohne'))
print("Drone is {}".format(prevDroneState))

I looked into https://github.com/obsproject/obs-websocket/blob/4.x-compat/docs/generated/protocol.md#getsourceactive and seen that I need to get the sourceActive response/object.

I tried to use dot notations like:


droneVisible = prevDroneState.getSourceActive()

droneVisible = prevDroneState.getsourceActive()

droneVisible = prevDroneState.sourceActive

But it dosent work and just result in errors like:

  droneVisible = prevDroneState.getSourceActive()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\RP Streaming\AppData\Roaming\Python\Python311\site-packages\obswebsocket\base_classes.py", line 65, in getter
    raise KeyError(key1)
KeyError: 'sourceActive'

How I can get only the videoActive or videoShowing result?

This is my Print result: Drone is <GetSourceActive request ({'sourceName': 'Drohne'}) called: success ({'videoActive': False, 'videoShowing': False})>

iarspider commented 10 months ago

Are you using v4 (legacy) or v5 sockets? If you are using v4, you need to pass legacy=True to a call to obsws(...). For v5, you need to use different documentation (https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests) and use something like this:

tmp = ws.call(obsws_requests.GetSceneItemId(sceneName="Scene", sourceName="Game Capture"))
id = tmp.getSceneItemId()
res = ws.call(obsws_requests.GetSceneItemEnabled(sceneName="Scene", sceneItemId=id))
res.getSceneItemEnabled()
marcus-universe commented 10 months ago

Are you using v4 (legacy) or v5 sockets? If you are using v4, you need to pass legacy=True to a call to obsws(...). For v5, you need to use different documentation (https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests) and use something like this:

tmp = ws.call(obsws_requests.GetSceneItemId(sceneName="Scene", sourceName="Game Capture"))
id = tmp.getSceneItemId()
res = ws.call(obsws_requests.GetSceneItemEnabled(sceneName="Scene", sceneItemId=id))
res.getSceneItemEnabled()

I found a solution that worked for me but its not so elegant 😅 I using v5 😎 But thanks for the tip with the v5 docs. I was wondering why I getting different responses then in the docs 😄

import re

prevDroneState = ws.call(obs_requests.GetSourceActive(sourceName='Drohne'))
state_str = str(prevDroneState)

videoActive = re.search("'videoActive': (\w+)", state_str)
videoShowing = re.search("'videoShowing': (\w+)", state_str)

if videoActive:
    print("Video Active: {}".format(videoActive.group(1)))

if videoShowing:
    print("Video Showing: {}".format(videoShowing.group(1)))
marcus-universe commented 10 months ago

Are you using v4 (legacy) or v5 sockets? If you are using v4, you need to pass legacy=True to a call to obsws(...). For v5, you need to use different documentation (https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests) and use something like this:

tmp = ws.call(obsws_requests.GetSceneItemId(sceneName="Scene", sourceName="Game Capture"))
id = tmp.getSceneItemId()
res = ws.call(obsws_requests.GetSceneItemEnabled(sceneName="Scene", sceneItemId=id))
res.getSceneItemEnabled()

You solution worked. Thanks alot again 👍