OBS crashes when switching scene during a timer_add (Python scripting)

redynotredy

New Member
Hi, I'm still quite new to scripting for OBS and I'm trying to make a timer for switching scenes for my own purposes in python, since it's the language I'm the most used to.
Inside my callback function for the timer, I added the obs.obs_frontend_set_current_scene(scene) which is causing OBS to crash for some odd reason.
20:01:21.358: ==== Virtual Camera Start ==========================================
20:01:22.358: [Python: Stream timer.py] 1
20:01:23.358: [Python: Stream timer.py] 2
20:01:24.358: [Python: Stream timer.py] 3
20:01:25.358: [Python: Stream timer.py] 4
20:01:26.358: [Python: Stream timer.py] 5
20:01:26.358: [Python: Stream timer.py] 1
20:01:26.358: [Python: Stream timer.py] <Swig Object of type 'obs_source_t *' at 0x00000287A426E2D0>
20:01:28.188: Virtual output stopping
20:01:28.191: Output 'virtualcam_output': stopping
20:01:28.191: Output 'virtualcam_output': Total frames output: 409
20:01:28.192: Output 'virtualcam_output': Total drawn frames: 410
20:01:28.193: Virtual output stopped
20:01:28.194: ==== Virtual Camera Stop ===========================================
20:02:47.809: Virtual output started
20:02:47.813: ==== Virtual Camera Start ==========================================
20:02:48.824: [Python: Stream timer.py] 1
20:02:49.824: [Python: Stream timer.py] 2
20:02:50.824: [Python: Stream timer.py] 3
20:02:51.824: [Python: Stream timer.py] 4
20:02:52.824: [Python: Stream timer.py] 5
A snippet of my code for reference:
global scenes
scenes = obs.obs_frontend_get_scenes()

def script_load(settings):
obs.obs_frontend_add_event_callback(onevent)

def script_update(settings):
global trigger, s_minutes, s_seconds, ending
trigger = obs.obs_data_get_string(settings, "e_trigger scene")
s_minutes = obs.obs_data_get_int(settings, "s_minutes")
s_seconds = obs.obs_data_get_int(settings, "s_seconds")
ending = obs.obs_data_get_string(settings, "s_ending scene")
def timer_callback():
if state == 1:
global tElapsed
tElapsed += 1
print(tElapsed)
if tElapsed == timer:
set_scene()
obs.remove_current_callback()
if state == 2:
print("End timer")
def set_scene():
index = (obs.obs_frontend_get_scene_names()).index(ending)
scene = scenes[index]
obs.obs_frontend_set_current_scene(scene)
def onevent(event):
global state
if event==obs.OBS_FRONTEND_EVENT_VIRTUALCAM_STOPPED:
state = 0
if event==obs.OBS_FRONTEND_EVENT_VIRTUALCAM_STARTED:
state = 1
global timer
timer = s_minutes * 60 + s_seconds
obs.timer_add(timer_callback,1000)
In essence, I added a callback for when the virtual camera is turned on, after which it will compute the input time from script properties.
For testing purposes, I set the timer to 5 seconds, which is why the OBS log shows only up to 5, but nothing after.
I've attempted putting the set scene function within the callback function and putting it in a helper function and for both cases it ends up crashing.
However, when I separate the set scene function from the timer, it works perfectly.

Does anyone have any workarounds? And any explanation on why I'm experiencing this?
 

Suslik V

Active Member
Look at the script "url-text.py" - it shipped with the OBS. There you will find working example how to use the timer_remove()/timer_add().

I think, your script crashes because you constantly (in timed intervals) calling the remove_current_callback(). Maybe you simply need to reset the timer value before the timeout? It is hard to tell what your intention was.
 

redynotredy

New Member
My bad, I didn't realise the code would be displayed like that
1658682039875.png

Maybe this image would make things clearer
 

Suslik V

Active Member
Code:
...
<Swig Object of type 'obs_source_t *' at 0x00000287A426E2D0>
...
The provided log says that you are printing the object, it is no longer a number. If this gives you any clues of course.
 
Top