--[[ OBS Studio Lua script : rotate an object with hotkeys Author: John Craig Version: 0.2 (added reset) Released: 2018-09-23 --]] local obs = obslua local source, increment, interval, reset, debug local sceneItem local direction = 1 local hk = {} -- if you are extending the script, you can add more hotkeys here -- then add actions in the 'onHotKey' function further below local hotkeys = { ROTATE_cw = "Rotate object clockwise", ROTATE_ccw = "Rotate object counter clockwise", ROTATE_stop = "Stop rotation", ROTATE_reset = "Reset rotation", } local function currentSceneName() local src = obs.obs_frontend_get_current_scene() local name = obs.obs_source_get_name(src) obs.obs_source_release(src) return name end local function findSceneItem(itemName) local src = obs.obs_get_source_by_name(currentSceneName()) if src then local scene = obs.obs_scene_from_source(src) obs.obs_source_release(src) if scene then sceneItem = obs.obs_scene_find_source(scene, source) if sceneItem and debug then obs.script_log(obs.LOG_INFO, string.format("Found : %s", source)) end return true end end sceneItem = nil end local function rotate() if sceneItem then local r = obs.obs_sceneitem_get_rot(sceneItem) + increment * direction obs.obs_sceneitem_set_rot(sceneItem, r) else obs.remove_current_callback() end end -- add any custom actions here local function onHotKey(action) obs.timer_remove(rotate) if debug then obs.script_log(obs.LOG_INFO, string.format("Hotkey : %s", action)) end if action == "ROTATE_cw" then direction = 1 obs.timer_add(rotate, interval) elseif action == "ROTATE_ccw" then direction = -1 obs.timer_add(rotate, interval) elseif action == "ROTATE_reset" and sceneItem then obs.obs_sceneitem_set_rot(sceneItem, reset) end end ---------------------------------------------------------- -- called on startup function script_load(settings) for k, v in pairs(hotkeys) do hk[k] = obs.obs_hotkey_register_frontend(k, v, function(pressed) if pressed then onHotKey(k) end end) local hotkeyArray = obs.obs_data_get_array(settings, k) obs.obs_hotkey_load(hk[k], hotkeyArray) obs.obs_data_array_release(hotkeyArray) end end -- called on unload function script_unload() end -- called when settings changed function script_update(settings) source = obs.obs_data_get_string(settings, "source") increment = obs.obs_data_get_int(settings, "increment") interval = obs.obs_data_get_int(settings, "interval") reset = obs.obs_data_get_int(settings, "reset") debug = obs.obs_data_get_bool(settings, "debug") findSceneItem() end -- return description shown to user function script_description() return "Rotate an object with hotkeys" end -- define properties that user can change function script_properties() local props = obs.obs_properties_create() obs.obs_properties_add_text(props, "source", "Object to rotate", obs.OBS_TEXT_DEFAULT) obs.obs_properties_add_int(props, "increment", "Increment", 1, 90, 1) obs.obs_properties_add_int(props, "interval", "Interval (ms)", 2, 500, 1) obs.obs_properties_add_int(props, "reset", "Reset angle", 0, 359, 1) obs.obs_properties_add_bool(props, "debug", "Debug") return props end -- set default values function script_defaults(settings) obs.obs_data_set_default_string(settings, "source", "") obs.obs_data_set_default_int(settings, "increment", 2) obs.obs_data_set_default_int(settings, "interval", 5) obs.obs_data_set_default_int(settings, "reset", 0) obs.obs_data_set_default_bool(settings, "debug", false) end -- save additional data not set by user function script_save(settings) for k, v in pairs(hotkeys) do local hotkeyArray = obs.obs_hotkey_save(hk[k]) obs.obs_data_set_array(settings, k, hotkeyArray) obs.obs_data_array_release(hotkeyArray) end end