How to Dynamically Resize OBS Text with Python

Controlling OBS Studio programmatically allows for dynamic stream overlays, automated animations, and interactive broadcasts. This guide explains how to use a custom Python script to dynamically alter the size of a GDI+ text source in OBS Studio. You will learn how to set up the OBS Python environment, write a script using the native OBS scripting API, and configure it within your OBS scene to change font sizes on the fly.

Step 1: Set Up Python in OBS Studio

Before running scripts, OBS Studio must be linked to a compatible Python installation.

  1. Download and install Python on your system. Note: OBS Studio requires a specific Python major/minor version (usually Python 3.10 or 3.11, depending on your OBS version). Ensure you download the 64-bit version of Python.
  2. Open OBS Studio and navigate to Tools > Scripts.
  3. Go to the Python Settings tab.
  4. Browse and select your Python installation path (e.g., C:/Users/YourUsername/AppData/Local/Programs/Python/Python310 on Windows).

Step 2: Create the Python Script

Create a new file named resize_text.py and paste the following Python code into it. This script uses the OBS Python API (obspython) to target a specific text source and modify its font size.

import obspython as obs

# Global variables for script settings
source_name = ""
target_size = 72

def script_description():
    return "Dynamically changes the font size of a specified text source in OBS."

def script_properties():
    props = obs.obs_properties_create()
    
    # Text input to specify the target OBS text source
    obs.obs_properties_add_text(props, "source_name", "Text Source Name", obs.OBS_TEXT_DEFAULT)
    
    # Integer slider/input to adjust the font size
    obs.obs_properties_add_int(props, "target_size", "Font Size (pt)", 10, 300, 1)
    
    return props

def script_update(settings):
    global source_name, target_size
    source_name = obs.obs_data_get_string(settings, "source_name")
    target_size = obs.obs_data_get_int(settings, "target_size")
    update_text_size()

def update_text_size():
    global source_name, target_size
    
    # Locate the specified text source
    source = obs.obs_get_source_by_name(source_name)
    if source is not None:
        # Retrieve current source settings
        settings = obs.obs_source_get_settings(source)
        font_obj = obs.obs_data_get_obj(settings, "font")
        
        if font_obj is not None:
            # Modify the size property within the font object
            obs.obs_data_set_int(font_obj, "size", target_size)
            
            # Save the modified font object back to settings
            obs.obs_data_set_obj(settings, "font", font_obj)
            obs.obs_source_update(source, settings)
            
            # Clean up font object reference
            obs.obs_data_release(font_obj)
            
        # Clean up settings and source references
        obs.obs_data_release(settings)
        obs.obs_source_release(source)

Step 3: Load and Run the Script in OBS

  1. In OBS Studio, open the Tools > Scripts menu.
  2. In the Scripts tab, click the + (plus) icon at the bottom left.
  3. Locate and select your resize_text.py file.
  4. Under the script settings panel on the right, type the exact name of your Text source (e.g., Text (GDI+)) into the Text Source Name field.
  5. Drag the Font Size slider. You will see the text size in your OBS viewport dynamically scale in real-time without pixelation.