@@ -64,6 +64,8 @@ local monitor_override_sy = 0
64
64
local monitor_override_dw = 0
65
65
local monitor_override_dh = 0
66
66
local debug_logs = false
67
+ local is_obs_loaded = false
68
+ local is_script_loaded = false
67
69
68
70
local ZoomState = {
69
71
None = 0 ,
@@ -74,7 +76,9 @@ local ZoomState = {
74
76
local zoom_state = ZoomState .None
75
77
76
78
local version = obs .obs_get_version_string ()
77
- local major = tonumber (version :match (" (%d+%.%d+)" )) or 0
79
+ local m1 , m2 = version :match (" (%d+%.%d+)%.(%d+)" )
80
+ local major = tonumber (m1 ) or 0
81
+ local minor = tonumber (m2 ) or 0
78
82
79
83
-- Define the mouse cursor functions for each platform
80
84
if ffi .os == " Windows" then
@@ -589,12 +593,14 @@ function refresh_sceneitem(find_newest)
589
593
end
590
594
591
595
if source_width == 0 or source_height == 0 then
592
- log (" ERROR: Something went wrong determining source size." ..
593
- " Try using the 'Set manual source position' option and adding override values" )
594
-
595
- if monitor_info ~= nil then
596
+ if monitor_info ~= nil and monitor_info .width > 0 and monitor_info .height > 0 then
597
+ log (" WARNING: Something went wrong determining source size.\n " ..
598
+ " Using source size from info: " .. monitor_info .width .. " , " .. monitor_info .height )
596
599
source_width = monitor_info .width
597
600
source_height = monitor_info .height
601
+ else
602
+ log (" ERROR: Something went wrong determining source size.\n " ..
603
+ " Try using the 'Set manual source position' option and adding override values" )
598
604
end
599
605
else
600
606
log (" Using source size: " .. source_width .. " , " .. source_height )
@@ -999,16 +1005,34 @@ end
999
1005
1000
1006
function on_frontend_event (event )
1001
1007
if event == obs .OBS_FRONTEND_EVENT_SCENE_CHANGED then
1002
- log (" Scene changed" )
1008
+ log (" OBS Scene changed" )
1003
1009
-- If the scene changes we attempt to find a new source with the same name in this new scene
1004
1010
-- TODO: There probably needs to be a way for users to specify what source they want to use in each scene
1011
+ -- Scene change can happen before OBS has completely loaded, so we check for that here
1012
+ if is_obs_loaded then
1013
+ refresh_sceneitem (true )
1014
+ end
1015
+ elseif event == obs .OBS_FRONTEND_EVENT_FINISHED_LOADING then
1016
+ log (" OBS Loaded" )
1017
+ -- Once loaded we perform our initial lookup
1018
+ is_obs_loaded = true
1019
+ monitor_info = get_monitor_info (source )
1005
1020
refresh_sceneitem (true )
1021
+ elseif event == obs .OBS_FRONTEND_EVENT_SCRIPTING_SHUTDOWN then
1022
+ log (" OBS Shutting down" )
1023
+ -- Add a fail-safe for unloading the script during shutdown
1024
+ if is_script_loaded then
1025
+ script_unload ()
1026
+ end
1006
1027
end
1007
1028
end
1008
1029
1009
1030
function on_update_transform ()
1010
1031
-- Update the crop/size settings based on whatever the source in the current scene looks like
1011
- refresh_sceneitem (true )
1032
+ if is_obs_loaded then
1033
+ refresh_sceneitem (true )
1034
+ end
1035
+
1012
1036
return true
1013
1037
end
1014
1038
@@ -1064,7 +1088,8 @@ function log_current_settings()
1064
1088
debug_logs = debug_logs
1065
1089
}
1066
1090
1067
- log (" OBS Version: " .. string.format (" %.1f" , major ))
1091
+ log (" OBS Version: " .. string.format (" %.1f" , major ) .. " ." .. minor )
1092
+ log (" Platform: " .. ffi .os )
1068
1093
log (" Current settings:" )
1069
1094
log (format_table (settings ))
1070
1095
end
@@ -1250,11 +1275,16 @@ function script_load(settings)
1250
1275
log (" ERROR: Could not get X11 Display for Linux\n " ..
1251
1276
" Mouse position will be incorrect." )
1252
1277
end
1278
+
1279
+ source_name = " "
1280
+ is_script_loaded = true
1253
1281
end
1254
1282
1255
1283
function script_unload ()
1284
+ is_script_loaded = false
1285
+
1256
1286
-- Clean up the memory usage
1257
- if major > 29.0 then -- 29.0 seems to crash if you do this, so we ignore it as the script is closing anyway
1287
+ if major > 29.1 or ( major == 29.1 and minor > 2 ) then -- 29.1.2 and below seems to crash if you do this, so we ignore it as the script is closing anyway
1258
1288
local transitions = obs .obs_frontend_get_transitions ()
1259
1289
if transitions ~= nil then
1260
1290
for i , s in pairs (transitions ) do
@@ -1272,6 +1302,8 @@ function script_unload()
1272
1302
1273
1303
if x11_lib ~= nil and x11_display ~= nil then
1274
1304
x11_lib .XCloseDisplay (x11_display )
1305
+ x11_display = nil
1306
+ x11_lib = nil
1275
1307
end
1276
1308
end
1277
1309
@@ -1348,7 +1380,7 @@ function script_update(settings)
1348
1380
debug_logs = obs .obs_data_get_bool (settings , " debug_logs" )
1349
1381
1350
1382
-- Only do the expensive refresh if the user selected a new source
1351
- if source_name ~= old_source_name then
1383
+ if source_name ~= old_source_name and is_obs_loaded then
1352
1384
refresh_sceneitem (true )
1353
1385
end
1354
1386
@@ -1363,7 +1395,9 @@ function script_update(settings)
1363
1395
monitor_override_sy ~= old_sy or
1364
1396
monitor_override_w ~= old_dw or
1365
1397
monitor_override_h ~= old_dh then
1366
- monitor_info = get_monitor_info (source )
1398
+ if is_obs_loaded then
1399
+ monitor_info = get_monitor_info (source )
1400
+ end
1367
1401
end
1368
1402
end
1369
1403
@@ -1384,4 +1418,4 @@ function populate_zoom_sources(list)
1384
1418
1385
1419
obs .source_list_release (sources )
1386
1420
end
1387
- end
1421
+ end
0 commit comments