-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathobscontroller.py
81 lines (65 loc) · 2.22 KB
/
obscontroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from obswebsocket import obsws, requests
import time
import logger
class ObsController:
def __init__(self, IP, PORT, PASS=None, cooldown=3, forbidden=[]):
"""
:param IP: OBSWS Host (should be localhost)
:param PORT: OBSWS Port (default = 4444)
:param PASS: Password. Strongly recommanded
:param cooldown: mimimum time beetween two scene change
:param forbidden: list of the scenes that doesn't allows for switching
"""
self.socket = None
self.IP = IP
self.PORT = PORT
self.PASS = PASS
# Behaviour related variables
self.last_scene = None
self.last_switch = 0
self.cooldown = cooldown
self.forbidden = forbidden
def socket_init(self):
"""
Init the socket. Raise ConnectionError if failed.
:return:
"""
try:
self.socket = obsws(self.IP, self.PORT, None if self.PASS == "" else self.PASS)
self.socket.connect()
except:
self.socket = None
raise ConnectionError
def keep_alive(self):
"""
Init the socket if dead, otherwise do nothing
"""
if self.socket is None:
self.socket_init()
@property
def current_scene(self):
"""
Get the current Scene.
:return: Str. Scene name
"""
return self.socket.call(requests.GetCurrentScene()).datain['name']
def go_to_scene(self, scene):
"""
Switch to a scene. Allows for non-existing scenes
:param scene: Scene name
"""
self.socket.call(requests.SetCurrentScene(scene))
self.last_scene = scene
self.last_switch = time.time()
def smart_switch(self, scene):
# Skipping if cooldown not reached
if self.last_switch + self.cooldown > time.time():
return
# Skipping if current scene forbids auto change
current_scene = self.current_scene
if current_scene in self.forbidden:
return
# Only switching if the last targeted scene is different
if scene != self.last_scene:
logger.log.log("Switching to [{}]".format(scene))
self.go_to_scene(scene)