obs = obslua --[[ Source Visibility Toggle Script for OBS on Record Start/Stop event Version: v1.1 Release Date: 8th December 2023 Description: This script for OBS Studio enables users to automatically toggle the visibility of a selected source when starting or stopping a recording. The script provides a dropdown list containing all sources available in OBS, each prefixed with its respective scene name for easy identification. The user can select a source from this list, and the script will handle the visibility of this source during the recording sessions. When recording starts, the chosen source becomes visible, and when recording stops, the source is hidden. This functionality is particularly useful for showing or hiding elements like logos, notifications, or specific messages only during recordings. It's primary purpose as intended is to toggle off/on a scene boarder on monitor window to indicate recording is active. How to Use: 1. Install and load the script in OBS. 2. Select the desired source from the dropdown list in the script's properties. 3. Start recording in OBS; the selected source will automatically become visible. 4. Stop recording; the selected source will automatically become hidden. Please note that the source list contains all sources from all scenes, prefixed with the scene name for clarity. Tested on OBS 30.0.0 History: 08-DEC-2023 - v1.0 Initial version 08-DEC-2023 - v1.1 Code Clean up and Comments added Credits: With thanks to Nutty for the inspiration from his YouTube video tutorial series for the idea to create this. Additional credit to Chat-GPT4 for the code generation. And myself, Joanne Lockwood, for having the patience to prompt, implement and debug the AI generated code in order to come up with something that actually works, doesn't hang OBS or crash it. :-) License: This script is released as open source under the MIT License. You are free to use, modify, and distribute this script 'as-is', but not for resale. Copyright for this script is retained by Joanne Lockwood. The MIT License (MIT) Copyright (c) 2023 Joanne Lockwood (jo.lockwood1965@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] selected_source_name = "" -- Function to return the description of the script function script_description() return [[
This script allows you to automatically toggle the visibility of a selected source when starting or stopping a recording in OBS. Select a source from the dropdown list, and the script will handle its visibility during recording sessions.
Usage:
This is useful for displaying specific items, such as logos, notifications, or messages, only when recording.
]] end -- Function to return the version of the script function script_version() return "v1.1" end -- Start of Main Code obs = obslua selected_source_combo_name = "" -- This will store the combined scene-source name selected_source_name = "" -- This will store just the source name for toggling -- Function to print debug information to the OBS log function script_log(message) obs.script_log(obs.LOG_INFO, message) end -- Function to populate the source dropdown with sources prefixed by their scene names function populate_source_dropdown(props) script_log("populate_source_dropdown: Populating source dropdown") local sources_property = obs.obs_properties_get(props, "source_list") obs.obs_property_list_clear(sources_property) local scenes = obs.obs_frontend_get_scenes() if scenes then local scene_source_pairs = {} for _, scene in ipairs(scenes) do local scene_name = obs.obs_source_get_name(scene) local scene_source = obs.obs_scene_from_source(scene) local scene_items = obs.obs_scene_enum_items(scene_source) for _, item in ipairs(scene_items) do local source = obs.obs_sceneitem_get_source(item) local source_name = obs.obs_source_get_name(source) table.insert(scene_source_pairs, scene_name .. " - " .. source_name) end obs.sceneitem_list_release(scene_items) end obs.source_list_release(scenes) -- Sort the list alphabetically by scene name table.sort(scene_source_pairs) -- Add sorted pairs to the dropdown for _, pair in ipairs(scene_source_pairs) do obs.obs_property_list_add_string(sources_property, pair, pair) end end end -- Function to extract just the source name from the combined scene-source name function extract_source_name(combo_name) local split_name = {} for name in string.gmatch(combo_name, "[^%-]+") do table.insert(split_name, name:match("^%s*(.-)%s*$")) -- Trim whitespace end return split_name[#split_name] -- Return the last element, which should be the source name end -- Function to toggle the visibility of the selected source function set_source_visibility(visible) local found = false local scenes = obs.obs_frontend_get_scenes() if scenes then for _, scene in ipairs(scenes) do local scene_source = obs.obs_scene_get_source(obs.obs_scene_from_source(scene)) local scene_items = obs.obs_scene_enum_items(obs.obs_scene_from_source(scene)) for _, item in ipairs(scene_items) do local source = obs.obs_sceneitem_get_source(item) local source_name = obs.obs_source_get_name(source) if source_name == selected_source_name then obs.obs_sceneitem_set_visible(item, visible) script_log("set_source_visibility: " .. selected_source_name .. " visibility set to " .. tostring(visible)) found = true break end end obs.sceneitem_list_release(scene_items) if found then break end end obs.source_list_release(scenes) end if not found then script_log("set_source_visibility: Source not found - " .. selected_source_name) end end -- Function called when recording starts or stops function on_event(event) if event == obs.OBS_FRONTEND_EVENT_RECORDING_STARTED then script_log("on_event: Recording started") selected_source_name = extract_source_name(selected_source_combo_name) set_source_visibility(true) elseif event == obs.OBS_FRONTEND_EVENT_RECORDING_STOPPED then script_log("on_event: Recording stopped") selected_source_name = extract_source_name(selected_source_combo_name) set_source_visibility(false) end end -- UI setup function script_properties() script_log("script_properties: Setting up UI") local props = obs.obs_properties_create() local source_list_property = obs.obs_properties_add_list(props, "source_list", "Source", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING) populate_source_dropdown(props) return props end -- Registering the event and updating settings with debug function script_load(settings) script_log("Script loaded") obs.obs_frontend_add_event_callback(on_event) end function script_update(settings) selected_source_combo_name = obs.obs_data_get_string(settings, "source_list") script_log("Script update: Selected source - " .. selected_source_combo_name) end