Skip to content

Commit d9aeee7

Browse files
committed
added typewriter,scrmbl,fixed memory leak
1 parent 41bc8aa commit d9aeee7

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

scripted_text.py

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import string
12
import obspython as obs
23
from itertools import cycle
34
from functools import partial
4-
from random import randint, sample, choice
5+
from random import choice
56
from contextlib import contextmanager
6-
import string
7+
from random import seed
78

89
__author__ = "upgradeQ"
9-
__version__ = "0.3.1"
10+
__version__ = "0.4.0"
1011
HOTKEY_ID = obs.OBS_INVALID_HOTKEY_ID
1112

1213
# auto release context managers
@@ -78,6 +79,10 @@ def __init__(self, text_string, source_name):
7879
self.duration = 5 * 1000
7980
self.effect_duration = 3 * 1000
8081

82+
self._text_chars = []
83+
self.rand_index = 0
84+
self.lst_index = 0
85+
self.blinking_bar = cycle([True, False])
8186
self.position_swap = cycle([True, False])
8287
self.last_jump_x = self.last_jump_y = 0
8388
self.blinking = cycle([True, False])
@@ -86,7 +91,16 @@ def __init__(self, text_string, source_name):
8691

8792
def load(self):
8893
mapping = dict()
89-
for i in ["rainbow", "static", "blink", "loading", "tremor", "sanic"]:
94+
for i in [
95+
"rainbow",
96+
"static",
97+
"blink",
98+
"loading",
99+
"tremor",
100+
"sanic",
101+
"typewriter",
102+
"scrmbl",
103+
]:
90104
mapping[i] = getattr(self, i + "_" + "effect")
91105
return mapping
92106

@@ -108,6 +122,8 @@ def check_duration():
108122
self.duration = 5 * 1000
109123
self.lock = True
110124
self.last_jump_y, self.last_jump_x = 0, 0
125+
self.lst_index = 0
126+
self.rand_index = 0
111127

112128
try:
113129
self.txt_efcts[text_effect]()
@@ -219,9 +235,50 @@ def sanic_effect(self):
219235
obs.obs_source_update(scroll, filter_settings)
220236
# set to zero scrolling speed and it will not interfere with others effects
221237
if self.duration // self.refresh_rate <= 3:
238+
obs.obs_source_filter_remove(source, scroll)
222239
self.duration = 0
223-
obs.obs_data_set_int(filter_settings, "speed_x", 0)
224-
obs.obs_source_update(scroll, filter_settings)
240+
241+
def typewriter_effect(self):
242+
"""adjust refresh rate in settings"""
243+
l = len(self.scripted_text)
244+
result = self.scripted_text[: self.lst_index]
245+
space_count = l - len(result)
246+
bar = "_" if next(self.blinking_bar) else ""
247+
result += bar
248+
result += space_count * " "
249+
self.lst_index += 1
250+
self.update_text(result)
251+
252+
def scrmbl_effect(self):
253+
"""tweak refresh rate & duration """
254+
try:
255+
self.update_text(self._text_chars[self.rand_index])
256+
self.rand_index += 1
257+
except IndexError:
258+
self.update_text(self.scripted_text)
259+
# self.rand_index = 0
260+
261+
def text_chars(self, scripted_str):
262+
"""inspired by https://github.com/etienne-napoleone/scrmbl """
263+
264+
ALL_CHARS = string.digits + string.ascii_letters + string.punctuation
265+
266+
def gen(scripted_str, iterations):
267+
seed() # set new seed
268+
echoed = ""
269+
fill = len(scripted_str)
270+
for char in scripted_str:
271+
for _ in range(iterations):
272+
if char != " ":
273+
ran_char = choice(ALL_CHARS)
274+
yield f"{echoed}{ran_char}{' '*fill}"
275+
else:
276+
yield f"{echoed}{' '*fill}"
277+
echoed += char
278+
if echoed: # last char
279+
yield f"{echoed}"
280+
281+
return gen(scripted_str, 3)
225282

226283
def hotkey_hook(self):
227284
""" trigger hotkey event"""
@@ -259,13 +316,14 @@ def script_update(settings):
259316
)
260317
scripted_text_driver.refresh_rate = obs.obs_data_get_int(settings, "refresh_rate")
261318

262-
print("setting duration")
263319
scripted_text_driver.effect_duration = obs.obs_data_get_int(settings, "duration")
264320
scripted_text_driver.sound_source_name = obs.obs_data_get_string(
265321
settings, "playsound"
266322
)
267323
scripted_text_driver.effect = obs.obs_data_get_string(settings, "text_effect")
268-
print("stopping sound")
324+
scripted_text_driver._text_chars = list(
325+
scripted_text_driver.text_chars(scripted_text_driver.scripted_text)
326+
)
269327
scripted_text_driver.stop_sound()
270328

271329

@@ -318,7 +376,6 @@ def script_properties():
318376
if source_id == "ffmpeg_source":
319377
name = obs.obs_source_get_name(source)
320378
obs.obs_property_list_add_string(sp, name, name)
321-
print(name)
322379

323380
obs.source_list_release(sources)
324381

@@ -332,13 +389,11 @@ def script_properties():
332389
def script_save(settings):
333390
global HOTKEY_ID
334391
hotkey_save_array = obs.obs_hotkey_save(HOTKEY_ID)
335-
print("htksave", hotkey_save_array)
336392
obs.obs_data_set_array(settings, "scripted_text_hotkey", hotkey_save_array)
337393
obs.obs_data_array_release(hotkey_save_array)
338394

339395

340396
def script_load(settings):
341-
# BUG, something here causing single memory leak
342397
global HOTKEY_ID
343398

344399
def callback_up(pressed):
@@ -348,7 +403,6 @@ def callback_up(pressed):
348403
HOTKEY_ID = obs.obs_hotkey_register_frontend(
349404
"scripted_text_hotkey", "Trigger sripted text", callback_up
350405
)
351-
obs.obs_data_get_array(settings, "scripted_text_hotkey")
352406
hotkey_save_array = obs.obs_data_get_array(settings, "scripted_text_hotkey")
353407
obs.obs_hotkey_load(HOTKEY_ID, hotkey_save_array)
354408
obs.obs_data_array_release(hotkey_save_array)

0 commit comments

Comments
 (0)