|
| 1 | +# Custom JavaScript events for OBS Studio browser sources |
| 2 | +# Copyright (C) 2023 DGCK81LNN |
| 3 | +# Documentation can be found at https://github.com/DGCK81LNN/obs_custom-browser-events.py |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute and/or modify it |
| 6 | +# under the terms of the GNU General Public License version 3. |
| 7 | +# |
| 8 | +# This program is distributed WITHOUT ANY WARRANTY; |
| 9 | +# See the GNU General Public License for more details. |
| 10 | +# |
| 11 | +# You should have received a copy of the GNU General Public License |
| 12 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 13 | + |
| 14 | +import obspython as obs |
| 15 | +from datetime import datetime |
| 16 | + |
| 17 | +def obs_data_get_string_array(data, name): |
| 18 | + items = [] |
| 19 | + array = obs.obs_data_get_array(data, name) |
| 20 | + for i in range(obs.obs_data_array_count(array)): |
| 21 | + item = obs.obs_data_array_item(array, i) |
| 22 | + items.append(obs.obs_data_get_string(item, "value")) |
| 23 | + obs.obs_data_release(item) |
| 24 | + obs.obs_data_array_release(array) |
| 25 | + return items |
| 26 | + |
| 27 | +events = [] |
| 28 | +hotkey_ids = {} |
| 29 | +hotkey_callbacks = [] |
| 30 | + |
| 31 | +def script_description(): |
| 32 | + return "Assign hotkeys to custom browser events. Set hotkeys in OBS settings.\n\nby DGCK81LNN <https://github.com/DGCK81LNN>" |
| 33 | + |
| 34 | +def script_defaults(settings): |
| 35 | + array = obs.obs_data_array_create() |
| 36 | + for i in range(3): |
| 37 | + item = obs.obs_data_create() |
| 38 | + obs.obs_data_set_string(item, "value", "obsCustomEvent%d" % (i + 1)) |
| 39 | + obs.obs_data_array_insert(array, obs.obs_data_array_count(array), item) |
| 40 | + obs.obs_data_release(item) |
| 41 | + obs.obs_data_set_default_array(settings, "events", array) |
| 42 | + obs.obs_data_array_release(array) |
| 43 | + |
| 44 | +def script_properties(): |
| 45 | + props = obs.obs_properties_create() |
| 46 | + obs.obs_properties_add_editable_list(props, "events", "Events", obs.OBS_EDITABLE_LIST_TYPE_STRINGS, None, None) |
| 47 | + return props |
| 48 | + |
| 49 | +def script_load(settings): |
| 50 | + global events, hotkey_ids |
| 51 | + events = obs_data_get_string_array(settings, "events") |
| 52 | + hotkey_ids = {} |
| 53 | + |
| 54 | + for event_name in events: |
| 55 | + hotkey_callback = callback_for(event_name) |
| 56 | + hotkey_id = obs.obs_hotkey_register_frontend( |
| 57 | + script_path() + "//" + event_name, |
| 58 | + "Custom browser event %r" % event_name, |
| 59 | + hotkey_callback |
| 60 | + ) |
| 61 | + hotkey_callbacks.append(hotkey_callback) |
| 62 | + hotkey_save_array = obs.obs_data_get_array(settings, event_name) |
| 63 | + obs.obs_hotkey_load(hotkey_id, hotkey_save_array) |
| 64 | + obs.obs_data_array_release(hotkey_save_array) |
| 65 | + hotkey_ids[event_name] = hotkey_id |
| 66 | + |
| 67 | +def script_unload(): |
| 68 | + for callback in hotkey_callbacks: |
| 69 | + obs.obs_hotkey_unregister(callback) |
| 70 | + hotkey_callbacks.clear() |
| 71 | + |
| 72 | +def script_update(settings): |
| 73 | + if obs_data_get_string_array(settings, "events") != events: |
| 74 | + script_unload() |
| 75 | + script_load(settings) |
| 76 | + |
| 77 | +def script_save(settings): |
| 78 | + for event_name in events: |
| 79 | + hotkey_save_array = obs.obs_hotkey_save(hotkey_ids[event_name]) |
| 80 | + obs.obs_data_set_array(settings, event_name, hotkey_save_array) |
| 81 | + obs.obs_data_array_release(hotkey_save_array) |
| 82 | + |
| 83 | +def callback_for(event_name): |
| 84 | + return lambda pressed: trigger(event_name) if pressed else None |
| 85 | + |
| 86 | +def trigger(event_name): |
| 87 | + print( |
| 88 | + "[%s] Triggered event %r" % ( |
| 89 | + datetime.now().astimezone().isoformat(timespec="seconds"), |
| 90 | + event_name |
| 91 | + ) |
| 92 | + ) |
| 93 | + for src in obs.obs_enum_sources(): |
| 94 | + if obs.obs_source_get_id(src) == "browser_source": |
| 95 | + cd = obs.calldata_create() |
| 96 | + obs.calldata_set_string(cd, "eventName", event_name) |
| 97 | + obs.calldata_set_string(cd, "jsonString", "{}") |
| 98 | + obs.proc_handler_call(obs.obs_source_get_proc_handler(src), "javascript_event", cd) |
| 99 | + obs.calldata_destroy(cd) |
0 commit comments