Created
May 5, 2020 04:16
-
-
Save cowboy/856c03f21cc58a9abd164768d2c92d25 to your computer and use it in GitHub Desktop.
OBS Studio script to turn child items of a scene on/off over time
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
-- https://obsproject.com/docs/scripting.html | |
local obs = obslua | |
-- Globals | |
local function log(name, msg) | |
if msg ~= nil then | |
msg = " > " .. tostring(msg) | |
else | |
msg = "" | |
end | |
obs.script_log(obs.LOG_DEBUG, name .. msg) | |
end | |
-- source_handlers | |
local source_handlers = {} | |
local function activate(s, source, source_name, activated) | |
s.source = source | |
if s.activated == activated then | |
return | |
end | |
s.activated = activated | |
if activated then | |
log("start", source_name) | |
s.start() | |
s.update() | |
obs.timer_add(s.update, s.delay) | |
else | |
log("stop", source_name) | |
obs.timer_remove(s.update) | |
end | |
end | |
local function each_child(s, cb) | |
local scene = obs.obs_scene_from_source(s.source) | |
local items = obs.obs_scene_enum_items(scene) | |
for i, item in pairs(items) do | |
local item_source = obs.obs_sceneitem_get_source(item) | |
local item_name = obs.obs_source_get_name(item_source) | |
cb(item, item_name) | |
end | |
end | |
local function add(name, cb) | |
local s = {} | |
s.source = nil | |
s.activated = false | |
s.activate = activate | |
s.each_child = each_child | |
source_handlers[name] = s | |
cb(s) | |
end | |
-- Called when scenes become activated or deactivated | |
-- https://dev.to/talk2megooseman/zeal-stream-11-8-2019-learning-obs-lua-plugin-development-2nh8 | |
local function activate_signal(calldata, activated) | |
local source = obs.calldata_source(calldata, "source") | |
if source ~= nil then | |
local name = obs.obs_source_get_name(source) | |
if (source_handlers[name]) then | |
source_handlers[name]:activate(source, name, activated) | |
end | |
end | |
end | |
local function source_activate(calldata) activate_signal(calldata, true) end | |
local function source_deactivate(calldata) activate_signal(calldata, false) end | |
function script_load(settings) | |
log("load") | |
local sh = obs.obs_get_signal_handler() | |
obs.signal_handler_connect(sh, "source_activate", source_activate) | |
obs.signal_handler_connect(sh, "source_deactivate", source_deactivate) | |
end | |
-- Source handlers | |
function script_description() | |
return "Animate robots" | |
end | |
add("Scene: Hypno Robots Wrapper", function(s) | |
local step_index | |
local count = 0 | |
local rl = "Robot Overlay Left" | |
local rc = "Robot Overlay Center" | |
local rr = "Robot Overlay Right" | |
local ml = "Robot Overlay Left Dynamic Mask" | |
local mc = "Robot Overlay Center Dynamic Mask" | |
local mr = "Robot Overlay Right Dynamic Mask" | |
local steps = { | |
{count=100, rc}, | |
{count=100, mc}, | |
{count=100, ml, mc, mr}, | |
{count=100, ml, rc, mr}, | |
{count=100, ml, mc, mr}, | |
{count=100, mc}, | |
} | |
s.delay = 200 | |
s.start = function() | |
count = 0 | |
step_index = 0 | |
end | |
s.update = function() | |
count = count - 1 | |
if count > 0 then | |
return | |
end | |
step_index = step_index + 1 | |
if step_index > table.getn(steps) then | |
step_index = 1 | |
end | |
local step = steps[step_index] | |
count = step.count | |
local stepMap = {} | |
for i, name in pairs(step) do | |
stepMap[name] = true | |
end | |
s:each_child(function(item, item_name) | |
obs.obs_sceneitem_set_visible(item, stepMap[item_name] or false) | |
end) | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment