Skip to content

Instantly share code, notes, and snippets.

@eric
Created July 24, 2020 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric/0ff2a02a1b6d86112609a46b037f61cd to your computer and use it in GitHub Desktop.
Save eric/0ff2a02a1b6d86112609a46b037f61cd to your computer and use it in GitHub Desktop.
obs = obslua
os = require("os")
io = require("io")
relative_path_from_home = "Dropbox/Twitch Assets"
settings_path = ""
function refresh_button_clicked(props, p)
update()
end
function splitpath(path)
return string.match(path,"^(.-)[\\/]?([^\\/]*)$")
end
function pathjoin(root, path)
if string.sub(root, string.len(root) - 1) ~= "/" and string.sub(path, 0, 0) ~= "/" then
return root .. "/" .. path
else
return root .. path
end
end
function relative_path_iterator(path)
local i = string.len(path)
return function()
if i == 0 then
return nil
end
repeat
i = i - 1
until i <= 0 or string.sub(path, i, i) == "/"
if i == 0 then
return path
else
return string.sub(path, i + 1)
end
end
end
function location_for_file(file)
-- Nothing to do
if obs.os_file_exists(file) then
return true, file
end
for relative_path in relative_path_iterator(file) do
obs.script_log(obs.LOG_INFO, "Trying " .. relative_path)
local proposed_file = pathjoin(settings_path, relative_path)
if obs.os_file_exists(proposed_file) then
return true, proposed_file
end
end
return false, ''
end
function update()
local sources = obs.obs_enum_sources()
if sources ~= nil then
for _, source in ipairs(sources) do
local source_id = obs.obs_source_get_id(source)
local source_name = obs.obs_source_get_name(source)
local settings = obs.obs_source_get_settings(source)
if settings then
local process = false
local settings_key = nil
if source_id == "ffmpeg_source" and obs.obs_data_get_bool(settings, "is_local_file") ~= false then
process = true
settings_key = "local_file"
elseif source_id == "image_source" then
process = true
settings_key = "file"
end
if process then
file_from_settings = obs.obs_data_get_string(settings, settings_key)
local found, local_file = location_for_file(file_from_settings)
if found then
if file_from_settings ~= local_file then
obs.script_log(obs.LOG_INFO, source_id .. ": " .. source_name .. ": Updated file " .. file_from_settings .. " -> " .. local_file)
obs.obs_data_set_string(settings, settings_key, local_file)
obs.obs_source_update(source, settings)
else
obs.script_log(obs.LOG_INFO, source_id .. ": " .. source_name .. ": Already up-to-date " .. file_from_settings)
end
else
obs.script_log(obs.LOG_INFO, source_id .. ": " .. source_name .. ": Not found " .. file_from_settings)
end
end
obs.obs_data_release(settings)
end
end
end
obs.source_list_release(sources)
end
function handle_event(event)
if event == obs.OBS_FRONTEND_EVENT_SCENE_CHANGED then
-- handle_scene_change()
end
if event == obs.OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED then
update()
end
end
function default_path()
local default_path = ""
local home_directory = os.getenv("HOME")
if home_directory then
default_path = home_directory .. "/" .. relative_path_from_home
end
return default_path
end
-- A function named script_properties defines the properties that the user
-- can change for the entire script module itself
function script_properties()
local props = obs.obs_properties_create()
obs.obs_properties_add_path(props, "path", "Path to images", obs.OBS_PATH_DIRECTORY, nil, default_path())
obs.obs_properties_add_button(props, "refresh", "Refresh", refresh_button_clicked)
return props
end
-- A function named script_description returns the description shown to
-- the user
function script_description()
return "Updates source locations for this system"
end
-- A function named script_update will be called when settings are changed
function script_update(settings)
settings_path = obs.obs_data_get_string(settings, "path")
update()
end
-- A function named script_defaults will be called to set the default settings
function script_defaults(settings)
obs.obs_data_set_default_string(settings, "path", default_path())
end
-- a function named script_load will be called on startup
function script_load(settings)
obs.obs_frontend_add_event_callback(handle_event)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment