forked from upgradeQ/Streaming-Software-Scripting-Reference
-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_nested.py
89 lines (70 loc) · 2.54 KB
/
add_nested.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
82
83
84
85
86
87
88
89
import obspython as S
from contextlib import contextmanager
from random import randint
# auto release context managers
@contextmanager
def source_ar(source_name):
source = S.obs_get_source_by_name(source_name)
try:
yield source
finally:
S.obs_source_release(source)
@contextmanager
def source_create_ar(id, source_name, settings):
try:
_source = S.obs_source_create(id, source_name, settings, None)
yield _source
finally:
S.obs_source_release(_source)
@contextmanager
def scene_create_ar(name):
try:
_scene = S.obs_scene_create(name)
yield _scene
finally:
S.obs_scene_release(_scene)
@contextmanager
def data_ar(source_settings=None):
if not source_settings:
settings = S.obs_data_create()
if source_settings:
settings = S.obs_source_get_settings(source_settings)
try:
yield settings
finally:
S.obs_data_release(settings)
@contextmanager
def scene_from_source_ar(source):
source = S.obs_scene_from_source(source)
try:
yield source
finally:
S.obs_scene_release(source)
def add_random_text_source(scene):
r = " random text # " + str(randint(0, 10))
with data_ar() as settings:
S.obs_data_set_string(settings, "text", f"random text value {r}")
with source_create_ar("text_ft2_source", f"random text{r}", settings) as source:
pos = S.vec2()
pos.x = randint(0, 1920)
pos.y = randint(0, 1080)
scene_item = S.obs_scene_add(scene, source)
S.obs_sceneitem_set_pos(scene_item, pos)
def add_scene_with_sources():
current_scene_source = S.obs_frontend_get_current_scene()
with scene_from_source_ar(current_scene_source) as scene_source:
with scene_create_ar("_nested_scene") as _scene:
py_scene_source = S.obs_scene_get_source(_scene)
with scene_from_source_ar(py_scene_source) as scene:
add_random_text_source(scene)
add_random_text_source(scene)
add_random_text_source(scene)
# add created scene to current scene ( nested scene)
_scene_source = S.obs_scene_get_source(scene)
S.obs_scene_add(scene_source, _scene_source)
def callback(*p):
add_scene_with_sources()
def script_properties():
props = S.obs_properties_create()
S.obs_properties_add_button(props, "button1", "add to current scene", callback)
return props