Created
August 20, 2021 18:33
-
-
Save MarioMey/37a911a7520df0cd6cfe23c752b25067 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import obspython as obs | |
from contextlib import contextmanager, ExitStack | |
def get_scene_context(scene_name): | |
scenes_list = obs.obs_frontend_get_scenes() | |
return_scene = None | |
for scene in scenes_list: | |
name = obs.obs_source_get_name(scene) | |
if name == scene_name: | |
return_scene = obs.obs_scene_from_source(scene) | |
return return_scene, scenes_list | |
@contextmanager | |
def source_list_auto_release(source_list): | |
try: | |
yield source_list | |
finally: | |
obs.source_list_release(source_list) | |
@contextmanager | |
def new_settings(): | |
newSettings = obs.obs_data_create() | |
try: | |
yield newSettings | |
finally: | |
obs.obs_data_release(newSettings) | |
@contextmanager | |
def source_creation(source_type, source_name, newSettings): | |
source = obs.obs_source_create(source_type, source_name, newSettings, None) | |
try: | |
yield source | |
finally: | |
obs.obs_source_release(source) | |
@contextmanager | |
def private_source_creation(source_type, source_name, newSettings): | |
source = obs.obs_source_create_private(source_type, source_name, newSettings) | |
try: | |
yield source | |
finally: | |
obs.obs_source_release(source) | |
@contextmanager | |
def source_auto_release(source_name): | |
source = obs.obs_get_source_by_name(source_name) | |
try: | |
yield source | |
finally: | |
obs.obs_source_release(source) | |
def item_create_text(scene_name, item_name, text='', private=False): | |
scene, scenes_list = get_scene_context(scene_name) | |
if not scene: | |
obs.source_list_release(scenes_list) | |
print(f'item_create_text() Scene no existe: "{scene_name}"') | |
return | |
with source_list_auto_release(scenes_list): | |
with ExitStack() as stack: | |
newSettings = stack.enter_context(new_settings()) | |
obs.obs_data_set_string(newSettings, 'text', text) | |
if private: | |
source = stack.enter_context(private_source_creation("text_ft2_source", item_name, newSettings)) | |
else: | |
source = stack.enter_context(source_creation("text_ft2_source", item_name, newSettings)) | |
new_item = obs.obs_scene_add(scene, source) | |
item_id = obs.obs_sceneitem_get_id(new_item) | |
return item_id | |
def source_set_setting(source_name, setting, value): | |
with ExitStack() as stack: | |
source = stack.enter_context(source_auto_release(source_name)) | |
if source: | |
newSettings = stack.enter_context(new_settings()) | |
obs.obs_data_set_string(newSettings, setting, value) | |
obs.obs_source_update(source, newSettings) | |
else: | |
print(f'Source no existe: {source_name}') | |
def create_text(props, prop): | |
item_create_text('main', 'text', 'Something', private=False) | |
def change_text(props, prop): | |
source_set_setting('text', 'text', 'Another thing') | |
def create_text_priv(props, prop): | |
item_create_text('main', 'text', 'Something', private=True) | |
def change_text_priv(props, prop): | |
source_set_setting('text', 'text', 'Another thing') | |
def script_properties(): | |
props = obs.obs_properties_create() | |
obs.obs_properties_add_button(props, "CREATE", "Create not private", create_text) | |
obs.obs_properties_add_button(props, "CHANGE", "Change not private", change_text) | |
obs.obs_properties_add_button(props, "CREATE_", "Create private", create_text_priv) | |
obs.obs_properties_add_button(props, "CHANGE_", "Change private", change_text_priv) | |
return props | |
def script_update(settings): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment