Trigger Python Script on OBS Scene Transition

Automating actions based on your OBS Studio broadcast flow can significantly elevate your production value. This guide outlines how to configure OBS Studio to execute a specific Python script immediately after a scene transition completes. By leveraging OBS Studio’s built-in Python scripting engine and frontend event callbacks, you can seamlessly trigger external APIs, smart lighting, or custom local applications whenever you switch scenes.

OBS Studio requires a local installation of Python to execute scripts. Ensure you install a compatible version (usually Python 3.10 or 3.11, depending on your OBS version).

  1. Download and install Python from the official Python website. Ensure you check the box to “Add Python to PATH” during installation.
  2. Open OBS Studio.
  3. Navigate to Tools in the top menu and select Scripts.
  4. Click on the Python Settings tab.
  5. Click Browse and select the folder where Python is installed on your system (e.g., C:/Users/YourUsername/AppData/Local/Programs/Python/Python311 on Windows).

Step 2: Create the Python Script

To detect when a transition completes, your script needs to listen to OBS frontend events. Create a new text file, name it transition_trigger.py, and paste the following code into it:

import obspython as obs
import subprocess

# Define the specific scene you want to trigger the script for
# Leave empty "" to trigger on any scene transition
TARGET_SCENE = "Your Scene Name"

def execute_action(scene_name):
    """
    Your custom Python code goes here. 
    Modify this function to run your desired commands.
    """
    print(f"Transition completed. Active scene: {scene_name}")
    
    # Example: Run an external system script or application
    # subprocess.Popen(["python", "path/to/your/external_script.py"])

def on_event(event):
    if event == obs.OBS_FRONTEND_EVENT_SCENE_CHANGED:
        # Fetch the active scene
        current_scene = obs.obs_frontend_get_current_scene()
        if current_scene:
            scene_name = obs.obs_source_get_name(current_scene)
            obs.obs_source_release(current_scene)
            
            # Check if the transition matches our target scene (or trigger for all)
            if TARGET_SCENE == "" or scene_name == TARGET_SCENE:
                execute_action(scene_name)

def script_load(settings):
    # Register the event callback when OBS loads the script
    obs.obs_frontend_add_event_callback(on_event)

def script_description():
    return "Triggers a custom Python function when a scene transition completes."

Save the file and customize the execute_action function with your own code or the path to an external script you want to run.

Step 3: Load the Script into OBS Studio

Once your script file is ready, you must load it into OBS:

  1. Open OBS Studio and go to Tools > Scripts.
  2. Stay on the Scripts tab and click the + (plus) icon in the bottom-left corner.
  3. Locate and select your transition_trigger.py file.
  4. The script description will appear, confirming that the script has loaded successfully.

Step 4: Test the Transition

To verify the setup is working correctly:

  1. Open the script log window by clicking Show Log in the Scripts window.
  2. Switch scenes in OBS Studio using your transitions.
  3. Watch the log window. You should see the print statements and your defined actions execute the moment the transition finishes and the new scene becomes fully active.