-- Scene CountUp Timer 0.1 -- -- Whenever a scene is changed, the script starts a count up timer and identifies the scene's name obs = obslua gtime_text_source = "" gscene_text_source = "" ghave_timer = false gcur_seconds = 0 gdebug = 0 gversion = 0.1 ---------------------------------------------------------- local function logIt(name, msg) if msg ~= nil then msg = " > " .. tostring(msg) else msg = "" end if gdebug == 1 then obs.script_log(obs.LOG_gdebug, name .. msg) end end ---------------------------------------------------------- function updateTextSource(whichSource, msg_text) logIt('in updateTextSource', msg_text) local source = obs.obs_get_source_by_name(whichSource) logIt(' whichSource', whichSource) logIt(' source', source) if source ~= nil then local settings = obs.obs_data_create() obs.obs_data_set_string(settings, "text", msg_text) obs.obs_source_update(source, settings) obs.obs_data_release(settings) obs.obs_source_release(source) end end ---------------------------------------------------------- function getTimeText() --logIt('in setTimeText') local seconds = math.floor(gcur_seconds % 60) local total_minutes = math.floor(gcur_seconds / 60) local minutes = math.floor(total_minutes % 60) local hours = math.floor(total_minutes / 60) local text = string.format("%02d:%02d", minutes, seconds) return text end ---------------------------------------------------------- function timer_callback() --log_it('in timer_callback') gcur_seconds = gcur_seconds + 1 local msg_text = getTimeText() updateTextSource(gtime_text_source, msg_text) end ---------------------------------------------------------- function onFrontendEvent(event) logIt('in onFrontendEvent', event) if event == obs.OBS_FRONTEND_EVENT_SCENE_CHANGED then local currentSceneSrc = obslua.obs_frontend_get_current_scene() current_scene = obslua.obs_source_get_name(currentSceneSrc) logIt(' current_scene', current_scene) updateTextSource(gscene_text_source, current_scene) gcur_seconds = 0 local msg_text = getTimeText() updateTextSource(gtime_text_source, msg_text) obs.obs_source_release(currentSceneSrc) end end ---------------------------------------------------------- -- A function named script_properties defines the properties that the user -- can change for the entire script module itself function script_properties() logIt('in script_properties') local props = obs.obs_properties_create() local ts = obs.obs_properties_add_list(props, "gtime_text_source", "Time text source ", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING) local ss = obs.obs_properties_add_list(props, "gscene_text_source", "Scene name text source ", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING) local sources = obs.obs_enum_sources() if sources ~= nil then for _, s in ipairs(sources) do source_id = obs.obs_source_get_unversioned_id(s) if source_id == "text_gdiplus" or source_id == "text_ft2_source" then local name = obs.obs_source_get_name(s) obs.obs_property_list_add_string(ts, name, name) obs.obs_property_list_add_string(ss, name, name) end end end obs.source_list_release(sources) return props end ---------------------------------------------------------- function script_description() return "Sets a text source to act as a countup timer when a scene is changed." end ---------------------------------------------------------- function script_update(settings) logIt('in script_update') gtime_text_source = obs.obs_data_get_string(settings, "gtime_text_source") gscene_text_source = obs.obs_data_get_string(settings, "gscene_text_source") end ---------------------------------------------------------- function script_load(settings) logIt('--------\nin script_load') obs.timer_add(timer_callback, 1000) obs.obs_frontend_add_event_callback(onFrontendEvent) onFrontendEvent(obslua.OBS_FRONTEND_EVENT_SCENE_CHANGED) end