use context manager in scene_rotate

other refactors
This commit is contained in:
onyx-and-iris 2022-10-24 22:42:46 +01:00
parent 27fd86efa5
commit b8b446f9bf
4 changed files with 29 additions and 27 deletions

View File

@ -14,3 +14,5 @@ host = "localhost"
port = 4455 port = 4455
password = "mystrongpass" password = "mystrongpass"
``` ```
Closing OBS ends the script.

View File

@ -1,10 +1,12 @@
import time
import obsws_python as obs import obsws_python as obs
class Observer: class Observer:
def __init__(self, cl): def __init__(self):
self._cl = cl self._client = obs.EventClient()
self._cl.callback.register( self._client.callback.register(
[ [
self.on_current_program_scene_changed, self.on_current_program_scene_changed,
self.on_scene_created, self.on_scene_created,
@ -12,7 +14,8 @@ class Observer:
self.on_exit_started, self.on_exit_started,
] ]
) )
print(f"Registered events: {self._cl.callback.get()}") print(f"Registered events: {self._client.callback.get()}")
self.running = True
def on_current_program_scene_changed(self, data): def on_current_program_scene_changed(self, data):
"""The current program scene has changed.""" """The current program scene has changed."""
@ -29,13 +32,12 @@ class Observer:
def on_exit_started(self, _): def on_exit_started(self, _):
"""OBS has begun the shutdown process.""" """OBS has begun the shutdown process."""
print(f"OBS closing!") print(f"OBS closing!")
self._cl.unsubscribe() self._client.unsubscribe()
self.running = False
if __name__ == "__main__": if __name__ == "__main__":
cl = obs.EventClient() observer = Observer()
observer = Observer(cl)
while cmd := input("<Enter> to exit\n"): while observer.running:
if not cmd: time.sleep(0.1)
break

View File

@ -5,10 +5,10 @@ import obsws_python as obs
class Observer: class Observer:
def __init__(self, cl): def __init__(self):
self._cl = cl self._client = obs.EventClient()
self._cl.callback.register(self.on_current_program_scene_changed) self._client.callback.register(self.on_current_program_scene_changed)
print(f"Registered events: {self._cl.callback.get()}") print(f"Registered events: {self._client.callback.get()}")
@property @property
def event_identifier(self): def event_identifier(self):
@ -20,20 +20,19 @@ class Observer:
def version(): def version():
resp = req_cl.get_version() resp = req_client.get_version()
print( print(
f"Running OBS version:{resp.obs_version} with websocket version:{resp.obs_web_socket_version}" f"Running OBS version:{resp.obs_version} with websocket version:{resp.obs_web_socket_version}"
) )
def set_scene(scene, *args): def set_scene(scene, *args):
req_cl.set_current_program_scene(scene) req_client.set_current_program_scene(scene)
if __name__ == "__main__": if __name__ == "__main__":
req_cl = obs.ReqClient() req_client = obs.ReqClient()
ev_cl = obs.EventClient() observer = Observer()
observer = Observer(ev_cl)
keyboard.add_hotkey("0", version) keyboard.add_hotkey("0", version)
keyboard.add_hotkey("1", set_scene, args=("START",)) keyboard.add_hotkey("1", set_scene, args=("START",))

View File

@ -4,16 +4,15 @@ import obsws_python as obs
def main(): def main():
resp = cl.get_scene_list() with obs.ReqClient() as client:
scenes = reversed(tuple(di.get("sceneName") for di in resp.scenes)) resp = client.get_scene_list()
scenes = [di.get("sceneName") for di in reversed(resp.scenes)]
for scene in scenes: for scene in scenes:
print(f"Switching to scene {scene}") print(f"Switching to scene {scene}")
cl.set_current_program_scene(scene) client.set_current_program_scene(scene)
time.sleep(0.5) time.sleep(0.5)
if __name__ == "__main__": if __name__ == "__main__":
cl = obs.ReqClient()
main() main()