# Python scripting missing bindings to frontend events, making it hard to trigger on stream start/stop



## TheAstropath (Mar 24, 2018)

Hey all,

I'm working on what I had thought would be a simple script in Python for OBS Studio, and am now repeatedly hitting dead ends.  Here's what I'm trying to accomplish:

When you start streaming or recording, make a call to a URL to turn on a physical light switch
When you stop streaming or recording, make another call to a different URL to turn off that light switch.

I got all the configuration options set up so the URLs can be defined in the Scripts menu, with buttons to test the URLs (And it does, in fact, turn the light on and off).  So far so good.

The first step to proceeding is obviously to get some sort of callback setup to execute my code when the stream (or recording) starts or stops.  In the API, there are "obs_frontend_event"s defined that looked great:
-OBS_FRONTEND_EVENT_STREAMING_STARTED
-OBS_FRONTEND_EVENT_STREAMING_STOPPED
-OBS_FRONTEND_EVENT_RECORDING_STARTED
-OBS_FRONTEND_EVENT_RECORDING_STOPPED
I would just need to register a callback using obs_frontend_add_event_callback...  Except there isn't a binding for that API call in obspython.  OK, next option then.

I saw there was an actual way to get to the signal handler for an output and receive start/stop events for individual outputs (obs_output_get_signal_handler and signal_handler_connect).  I got that all rigged up, and everything seemed to be working great - lights went on when I started streaming/recording, lights went off when I stopped.  That is, until I switched to a different profile in OBS.  At that point, my callbacks weren't getting called.  I assume when you switch profiles, the outputs get destroyed and new ones created with the new profile settings.

OK then, I just need to know when you switch profiles so that I can re-register my callbacks, that shouldn't be too bad.  Except it looks like the only way to get a notification of a profile change is through the frontend events, which I already discovered couldn't be done in Python.

I even went so far as to try just removing and re-adding the callbacks every 5 seconds using the timer callbacks.  For some reason though, despite using signal_handler_disconnect before doing another signal_handler_connect, I was still getting more and more callbacks per start/stop the longer I waited (as though the signal_handler_disconnect wasn't actually removing the callback?).

Is my only option here really to just periodically poll obs_frontend_streaming_active and obs_frontend_recording_active to see if OBS is alive?  Is there any particular reason I can't register for frontend events?


----------



## Marcedo (Mar 24, 2018)

From what i read lua Frontend callbacks should work. If you already through the point of tryin to poll for stuff, then a lua wrapper just calling your python logic might be argueable - besides doi that completely in lua, but that language just sucks if you ask me.


----------



## TheAstropath (Mar 25, 2018)

Ugh that's annoying.  I'm not really a big fan of LUA - I'd love to see the OBS team expand the capabilities of the Python scripting.  In the meantime though, I've just gone to a pure polling method with Python.  I can't see there being that much load or other issues being generated by polling once a second (or less frequently) to see if you are streaming or recording.


----------



## Marcedo (Mar 25, 2018)

Hm. as long its non-blocking, there shouldnt be much of a problem.  .. i think :)'


----------



## OZtwo (Mar 25, 2018)

From what I understand you could use Script_save which is called every a button is pressed which then in that function check the status of the stream?


----------



## TheAstropath (Mar 25, 2018)

If script_save gets called on every button press, that could be a good way to check if the profile was changed and if the callbacks need to be readded on the outputs.  Thanks for the idea, I'll have to experiment with that.


----------



## OZtwo (Mar 25, 2018)

NP. found it on error the other night when trying to figure out scripting. Still stuck on how to switch senses. :)


----------



## fenghuyu (Jul 5, 2018)

I also encountered this problem in development, and ended up failing several times. Because there is no obs_frontend_add_event_callback method in the current _obspython.pyd file.In addition to the methods you mentioned, there is another solution, which is to use a third-party library[lupa], so that lua can run in python, so that we can get the data we want to monitor through LUA time. However, because it is more troublesome to configure in windows. so I didn't try it. I hope obspython can add these functions in the future.


----------



## Scratch (Mar 4, 2020)

This is half a necrobump, but another half an update. So if anyone's actually still subscribed to this thread they should be notified.

The obs_frontend_add_event_callback  method has been added into OBS for release 25, with it hitting release candidate 1 yesterday








						Release OBS Studio 25.0 Release Candidate 1 · obsproject/obs-studio
					

A newer version than this one is out!  Check https://github.com/obsproject/obs-studio/releases to get the latest. New Features   Added the ability to capture Vulkan-based games with game capture.  ...




					github.com
				




it's not in the patchnotes, but it's there ;)
This should make it a lot easier to develop scripts that require it, sidestepping the LUA only methods

*It only took two years*


----------



## fenghuyu (Mar 4, 2020)

Scratch said:


> This is half a necrobump, but another half an update. So if anyone's actually still subscribed to this thread they should be notified.
> 
> The obs_frontend_add_event_callback  method has been added into OBS for release 25, with it hitting release candidate 1 yesterday
> 
> ...


Oh my god, finally this feature is available, thank you for your information。


----------

