Skip to content

Commit d04566d

Browse files
committed
Add many hotkeys example,stream key, closes #29
1 parent 7562382 commit d04566d

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Also check out [issues](https://github.com/upgradeQ/OBS-Studio-Python-Scripting-
3737
- [Access source dB volume level](#access-source-db-volume-level)
3838
- [Get current profile settings via ffi](#get-current-profile-settings-via-ffi)
3939
- [Convert from SWIG type to ctype](#convert-from-swig-type-to-ctype)
40+
- [Set current stream key](#set-current-stream-key)
4041
- [Debug](#debug)
4142
- [Security](#security)
4243
- [Docs and code examples](#docs-and-code-examples)
@@ -579,6 +580,7 @@ def send_hotkey(obs_htk_id, key_modifiers=None):
579580
- [Example with global ](src/hotkey_exmpl.py)
580581
- [Full example with json](src/hotkey_json.py)
581582
- [Full example with send hotkey](src/send_hotkey.py)
583+
- [Example with many hotkeys](src/hotkey_many.py)
582584

583585
See also:
584586
https://github.com/obsproject/obs-studio/blob/master/libobs/obs-hotkeys.h
@@ -756,6 +758,13 @@ cfg = cast(
756758
c_void_p(int(S.obs_frontend_get_profile_config())), POINTER(Config)
757759
)
758760
```
761+
# Set current stream key
762+
```python
763+
service = S.obs_frontend_get_streaming_service()
764+
settings = S.obs_service_get_settings(service)
765+
S.obs_data_set_string(settings, "key", _G._my_key)
766+
```
767+
- [Full source](src/stream_key.py)
759768

760769
Note,that this uses `obspython.obs_frontend_get_profile_config` so there is no need to load additional libraries.
761770

src/change_order.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def scene_ar(scene):
1313

1414

1515
@contextmanager
16-
def scene_enum(items):
17-
items = S.obs_scene_enum_items(items)
16+
def scene_enum(_scene):
17+
items = S.obs_scene_enum_items(_scene)
1818
try:
1919
yield items
2020
finally:

src/hotkey_many.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import obspython as S
2+
3+
4+
class Hotkey:
5+
def __init__(self, callback, obs_settings, _id):
6+
self.obs_data = obs_settings
7+
self.hotkey_id = S.OBS_INVALID_HOTKEY_ID
8+
self.hotkey_saved_key = None
9+
self.callback = callback
10+
self._id = _id
11+
12+
self.load_hotkey()
13+
self.register_hotkey()
14+
self.save_hotkey()
15+
16+
def register_hotkey(self):
17+
description = "Htk " + str(self._id)
18+
self.hotkey_id = S.obs_hotkey_register_frontend(
19+
"htk_id" + str(self._id), description, self.callback
20+
)
21+
S.obs_hotkey_load(self.hotkey_id, self.hotkey_saved_key)
22+
23+
def load_hotkey(self):
24+
self.hotkey_saved_key = S.obs_data_get_array(
25+
self.obs_data, "htk_id" + str(self._id)
26+
)
27+
S.obs_data_array_release(self.hotkey_saved_key)
28+
29+
def save_hotkey(self):
30+
self.hotkey_saved_key = S.obs_hotkey_save(self.hotkey_id)
31+
S.obs_data_set_array(
32+
self.obs_data, "htk_id" + str(self._id), self.hotkey_saved_key
33+
)
34+
S.obs_data_array_release(self.hotkey_saved_key)
35+
36+
37+
class Keyboard:
38+
def __init__(self):
39+
self.hotkeys_map = {}
40+
self.hotkeys_obj = []
41+
self.inspect_self()
42+
43+
def desc(description):
44+
def actual_decorator(func):
45+
func.desc = description
46+
return func
47+
48+
return actual_decorator
49+
50+
def do_load(self, settings):
51+
for h_id, cb in self.hotkeys_map.items():
52+
self.hotkeys_obj.append(Hotkey(cb, settings, h_id))
53+
54+
def do_save(self, settings):
55+
for i in self.hotkeys_obj:
56+
if i is not None:
57+
i.save_hotkey()
58+
59+
def inspect_self(self):
60+
for attr, value in vars(self.__class__).items():
61+
if callable(value) and hasattr(value, "desc"):
62+
self.hotkeys_map[value.desc] = value
63+
64+
@desc("dh1test")
65+
def cb1(pressed):
66+
if pressed:
67+
print("cb1" + e1.txt)
68+
else:
69+
print("???")
70+
71+
@desc("dh2test")
72+
def cb2(pressed):
73+
if pressed:
74+
print("cb2" + e2.txt)
75+
76+
@desc("dh3test")
77+
def cb3(pressed):
78+
if not pressed:
79+
print("cb3" + e1.txt)
80+
81+
@desc("dh4test")
82+
def cb4(pressed):
83+
if pressed:
84+
print("cb4" + e2.txt)
85+
86+
87+
kbd = Keyboard()
88+
89+
90+
class e:
91+
txt = "default txt"
92+
93+
94+
e1 = e()
95+
e2 = e()
96+
97+
98+
def script_properties():
99+
props = S.obs_properties_create()
100+
S.obs_properties_add_text(props, "_text1", "_text1:", S.OBS_TEXT_DEFAULT)
101+
S.obs_properties_add_text(props, "_text2", "_text2:", S.OBS_TEXT_DEFAULT)
102+
return props
103+
104+
105+
def script_update(settings):
106+
_text1 = S.obs_data_get_string(settings, "_text1")
107+
_text2 = S.obs_data_get_string(settings, "_text2")
108+
e1.txt = _text1
109+
e2.txt = _text2
110+
111+
112+
def script_load(settings):
113+
kbd.do_load(settings)
114+
115+
116+
def script_save(settings):
117+
# it will crash! If you delete script then add it and bind the same hotkeys
118+
kbd.do_save(settings)

src/stream_key.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import obspython as S
2+
from types import SimpleNamespace as G
3+
4+
_G = G()
5+
_G._my_key = ""
6+
7+
8+
def script_properties():
9+
props = S.obs_properties_create()
10+
S.obs_properties_add_text(props, "key", "Stream Key", S.OBS_TEXT_DEFAULT)
11+
S.obs_properties_add_button(props, "button", "Update", callback)
12+
return props
13+
14+
15+
def script_update(settings):
16+
_G._my_key = S.obs_data_get_string(settings, "key")
17+
18+
19+
def callback(props, prop):
20+
service = S.obs_frontend_get_streaming_service()
21+
settings = S.obs_service_get_settings(service)
22+
S.obs_data_set_string(settings, "key", _G._my_key)
23+
24+
S.obs_data_release(settings)
25+
return True

0 commit comments

Comments
 (0)