在obs中通过运行python脚本控制obs

258 阅读1分钟

改变脚本右侧ui

image.png

以下代码可以添加ui

image.png 这里docs.obsproject.com/reference-p… 记录了所有可使用的控件和其枚举,比如控制int控件的最大最小值...

# 文本描述,这里可以返回html的子集,展示更多文字样式
def script_description():
    return "<h1>每过一段时间从url更新文本源</h1><h3>by kevin</h3>"

def script_update(settings):

    global url
    global interval
    global source_name
    
    url         = obs.obs_data_get_string(settings, "url")
    interval    = obs.obs_data_get_int(settings, "interval")
    source_name = obs.obs_data_get_string(settings, "source")
    print(f"script_update{url,interval,source_name}")
    obs.timer_remove(update_text)

    if url != "" and source_name != "":
        obs.timer_add(update_text, interval * 1000)

  
def script_defaults(settings):
    print(f"script_defaults")
    obs.obs_data_set_default_int(settings, "interval", 30)

# obs的周期函数,需要返回一个properties对象
def script_properties():
    print(f"script_properties")
    # 创建一个props对象,这种写法是c里面的
    props = obs.obs_properties_create()
    
    # 往props里面添加prop对象,函数返回值就是prop对象
    # 添加文本控件
    obs.obs_properties_add_text(props, "url", "URL", obs.OBS_TEXT_DEFAULT)
    # 添加整数控件
    obs.obs_properties_add_int(props, "interval", "Update Interval (seconds)", 5, 3600, 1)
    p = obs.obs_properties_add_list(props, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)

    # 获取所有的源,如果是文字源,就源名称放到列表里
    sources = obs.obs_enum_sources()
    if sources is not None:
        for source in sources:
            source_id = obs.obs_source_get_unversioned_id(source)
            if source_id == "text_gdiplus" or source_id == "text_ft2_source":
                name = obs.obs_source_get_name(source)
                obs.obs_property_list_add_string(p, name, name)
        # 释放源对象
        obs.source_list_release(sources)
    # 添加按钮控件
    obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
    return props

获取所有的源

 # 获取所有的源
    sources = obs.obs_enum_sources()

obs回调函数的调用顺序

添加脚本时

image.png

重载脚本时

image.png

修改文本源的内容

source_name = ""

def update_text():
    global source_name
    source = obs.obs_get_source_by_name(source_name)
    settings = obs.obs_data_create()
    text = "changed text"
    obs.obs_data_set_string(settings, "text", text)
    obs.obs_source_update(source, settings)
    obs.obs_data_release(settings)
    obs.obs_source_release(source)