Last active
February 14, 2025 02:40
-
-
Save tpruvot/78e352ce15e3ba78d8b7daa249be9f70 to your computer and use it in GitHub Desktop.
OBS Python Script to toggle a source or a source filter with Hotkeys
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
# OBS Python Script to toggle a source or a source filter with Hotkeys | |
# Edit the bottom JSON defines to change (or add) the default F8 F9 keys | |
# tpruvot@github - February 2025 | |
# See https://github.com/upgradeQ/Streaming-Software-Scripting-Reference | |
# and https://docs.obsproject.com/reference-scenes | |
import obspython as S | |
def script_description(): | |
return "Script to toggle a (source) filter with Hotkeys." | |
# Show/hide a source | |
def toggle_source(sourceName="Webcam"): | |
current_scene = S.obs_scene_from_source(S.obs_frontend_get_current_scene()) | |
scene_item = S.obs_scene_find_source(current_scene, sourceName) | |
visible = S.obs_sceneitem_visible(scene_item) | |
S.obs_sceneitem_set_visible(scene_item, not visible) | |
# Enable/disable a source filter | |
def toggle_source_filter(sourceName="Webcam", filterName=""): | |
source = S.obs_get_source_by_name(sourceName) | |
filters = S.obs_source_backup_filters(source) | |
filter_count = S.obs_source_filter_count(source) | |
enabled = False | |
for i in range(filter_count): | |
settings = S.obs_data_array_item(filters, i) | |
filter_name = S.obs_data_get_string(settings, "name") | |
if filter_name == filterName: | |
enabled = S.obs_data_get_bool(settings, "enabled") | |
S.obs_data_release(settings) | |
S.obs_data_array_release(filters) | |
srcFilter = S.obs_source_get_filter_by_name(source, filterName) | |
if srcFilter: | |
S.obs_source_set_enabled(srcFilter, not enabled) | |
S.obs_source_release(srcFilter) | |
S.obs_source_release(source) | |
# Change current Scene | |
def set_current_scene(sceneName): | |
scenes = S.obs_frontend_get_scenes() | |
for scene in scenes: | |
name = S.obs_source_get_name(scene) | |
if name == sceneName: | |
S.obs_frontend_set_current_scene(scene) | |
S.source_list_release(scenes) | |
# My hotkeys | |
def on_obs_key_f8(pressed): | |
if pressed: | |
toggle_source("Giphy") | |
#raise Exception("F8 pressed") | |
def on_obs_key_f9(pressed): | |
if pressed: | |
toggle_source_filter("Capture d'écran Centre", "Flou") | |
#raise Exception("F9 pressed") | |
def script_load(settings): | |
ID = "htk_F8" | |
s = S.obs_data_create_from_json('{"%s":[{"key":"OBS_KEY_F8"}]}' % ID) | |
a = S.obs_data_get_array(s, ID) | |
h = S.obs_hotkey_register_frontend(ID, ID, on_obs_key_f8) | |
S.obs_hotkey_load(h, a) | |
S.obs_data_release(s) | |
S.obs_data_array_release(a) | |
ID = "htk_F9" | |
s = S.obs_data_create_from_json('{"%s":[{"key":"OBS_KEY_F9"}]}' % ID) | |
a = S.obs_data_get_array(s, ID) | |
h = S.obs_hotkey_register_frontend(ID, ID, on_obs_key_f9) | |
S.obs_hotkey_load(h, a) | |
S.obs_data_release(s) | |
S.obs_data_array_release(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment