URLhttps://learnscript.net/zh/obs-python-scripting/scenes/scene-items-and-groups/
    复制链接转到说明  示例

    如何操作 OBS 场景项,分组

    我被代码海扁署名-非商业-禁演绎
    阅读 24:10·字数 7254·更新 

    前提

    阅读本节的前提是对 OBS 场景有所掌握,你可以查看如何操作 OBS 场景?场景对象介绍一节来了解相关信息。

    OBS 场景项,分组

    OBS 场景包含了一组场景项,每一个场景项都引用了一个 OBS 来源,当多个场景项引用同一个来源时,他们将在某些行为上表现一致,比如显示的文字。OBS 分组与场景类似,也可以包含场景项,但分组中的场景项可被用户编辑。

    查找和获取场景中的 OBS 场景项

    obspython模块的obs_scene_find_sourceobs_scene_find_source_recursive函数,均可用于查找场景中指定名称的场景项,两者的区别在于,obs_scene_find_source_recursive将查找分组中的项。如果指定名称的场景项不存在,那么函数会返回空值None

    obs_scene_find_source(scene, name)
    obs_scene_find_source_recursive(scene, name)

    scene 参数

    scene参数为需要查找场景项的场景对象。

    name 参数

    name参数为被查找场景项的名称,也是该场景项对应 OBS 来源的名称。

    obspython模块的obs_scene_find_sceneitem_by_id函数,可用于查找场景中指定 ID 的场景项,场景项的 ID 是一个整数,不同场景项的 ID 不相同。如果指定 ID 的场景项不存在,那么函数会返回空值None

    obs_scene_find_sceneitem_by_id(scene, id)

    scene 参数

    scene参数为需要查找场景项的场景对象。

    id 参数

    id参数为被查找场景项的 ID。

    obspython模块的obs_scene_sceneitem_from_source函数,可用于获取场景中指定来源对应的场景项。

    obs_scene_sceneitem_from_source(scene, source)

    scene 参数

    scene参数为需要获取场景项的场景对象。

    source 参数

    source参数为场景项所引用的来源。

    使用 obs_sceneitem_release 函数释放获取的场景项对象

    对于使用obs_scene_sceneitem_from_source函数获取的场景项对象,需要通过obs_sceneitem_release释放引用,否则 OBS 可能会出现错误。

    在下面的代码中,我们对场景项Welcome进行了两次查询,一次包括分组,一次不包括,假设Welcome是分组Group中的场景项。

    items_and_groups.py
    # 导入模块 obspython 和 vec2
    import obspython as obs
    from obspython import vec2
    
    def test(props, prop): # 获取场景 Scene 对应的场景对象 source_scene = obs.obs_get_source_by_name('Scene') scene = obs.obs_scene_from_source(source_scene)
    # 两次查找场景项 Welcome,第二次包括分组 welcome = obs.obs_scene_find_source(scene, 'Welcome') obs.script_log(obs.LOG_INFO, f'找到了 Welcome?{welcome != None}') welcome = obs.obs_scene_find_source_recursive(scene, 'Welcome') obs.script_log(obs.LOG_INFO, f'找到了 Welcome(包括分组中的)?{welcome != None}')
    # 释放来源对象 obs.obs_source_release(source_scene)
    # 为脚本添加一个用于测试的按钮,回调函数为 test def script_properties(): props = obs.obs_properties_create() obs.obs_properties_add_button(props, 'test', '测试', test) return props
    [items_and_groups.py] 找到了 Welcome?False
    [items_and_groups.py] 找到了 Welcome(包括分组中的)?True

    获取 OBS 场景项所在的场景

    obspython模块的obs_sceneitem_get_scene函数,可用于获取场景项所在的场景。

    obs_sceneitem_get_scene(item)

    item 参数

    item参数为需要获取所在场景的场景项对象。

    获取 OBS 场景项引用的来源

    obspython模块的obs_sceneitem_get_source函数,可用于获取场景项引用的来源对象。

    obs_sceneitem_get_source(item)

    item 参数

    item参数为需要获取来源的场景项对象。

    下面的代码,将显示场景项Hi的来源类型标识符。

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 显示场景项 Welcome 的来源类型标识符
    	source_welcome = obs.obs_sceneitem_get_source(welcome)
    	obs.script_log(obs.LOG_INFO, f'{obs.obs_source_get_unversioned_id(source_welcome)}')
    	# …
    [items_and_groups.py] ffmpeg_source

    移除场景中的 OBS 场景项

    obspython模块的obs_sceneitem_remove函数,可用于移除场景中指定的场景项,无论该场景项是否处于分组内。

    obs_sceneitem_remove(item)

    item 参数

    item参数为需要移除的场景项对象。

    在下面的代码中,我们移除了Group分组的Welcome场景项。

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 将场景项 Welcome 移除
    	obs.obs_sceneitem_remove(welcome)
    	# …

    排序场景中的 OBS 场景项

    obspython模块的obs_sceneitem_set_order函数,可将场景项的位置向上,向下移动,以及将场景项移动至开头或末尾。需要注意,该函数对分组中的场景项无效。

    obspython模块的obs_sceneitem_get_order_positionobs_sceneitem_set_order_position函数,可将场景项移动到指定索引位置。

    obs_sceneitem_set_order(item, movement)
    obs_sceneitem_get_order_position(item)
    obs_sceneitem_set_order_position(item, position)

    item 参数

    item参数为需要移动位置的场景项对象。

    movement 参数

    movement参数为移动方式,可使用如下obspython模块变量进行设置,OBS_ORDER_MOVE_UP表示向上移动,OBS_ORDER_MOVE_DOWN表示向下移动,OBS_ORDER_MOVE_TOP表示移动至开头,OBS_ORDER_MOVE_BOTTOM表示移动至末尾。

    position 参数

    position参数为目标位置的索引,0表示末尾(倒数第一),倒数第二为1

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 将场景项 Hi 移动至末尾
    	hi = obs.obs_scene_find_source(scene, 'Hi')
    	obs.obs_sceneitem_set_order_position(hi, 0)
    	# …

    获取 OBS 场景项的私有数据设置对象

    obspython模块的obs_sceneitem_get_private_settings函数,可用于获取场景项对应的私有数据设置对象,这可以让开发人员针对场景项添加自定义的设置项。

    obs_sceneitem_get_private_settings(item)

    item 参数

    item参数为需要获取私有数据设置对象的场景项对象。

    使用 obs_data_release 函数释放场景项的私有数据设置对象

    对于使用obs_sceneitem_get_private_settings函数获取的场景项对应的私有数据设置对象,需要通过obs_data_release释放引用,否则 OBS 可能会出现错误。

    获取和设置 OBS 场景项的变换

    obspython模块的obs_sceneitem_get_posobs_sceneitem_set_pos函数,可用于获取和设置场景项在场景或分组中的位置。

    obs_sceneitem_get_pos(item, pos)
    obs_sceneitem_set_pos(item, pos)

    item 参数

    item参数为需要获取或设置位置的场景项对象。

    pos 参数

    pos参数为包含了位置信息的 OBSvec2对象。

    obspython模块的obs_sceneitem_get_rotobs_sceneitem_set_rot函数,可用于获取和设置场景项在场景或分组中的旋转角度。

    obs_sceneitem_get_rot(item)
    obs_sceneitem_set_rot(item, rot_deg)

    item 参数

    item参数为需要获取或设置旋转角度的场景项对象。

    rot_deg 参数

    rot_deg参数为需要设置的旋转角度。

    obspython模块的obs_sceneitem_get_scaleobs_sceneitem_set_scale函数,可用于获取和设置场景项在场景或分组中的缩放。

    obs_sceneitem_get_scale(item, scale)
    obs_sceneitem_set_scale(item, scale)

    item 参数

    item参数为需要获取或设置缩放的场景项对象。

    scale 参数

    scale参数为包含了缩放信息的 OBSvec2对象。

    obspython模块的obs_sceneitem_get_bounds_typeobs_sceneitem_set_bounds_type函数,可用于获取和设置场景项的边框类型。一旦设置了有效的边框,场景项中的内容可能会被缩放。

    obs_sceneitem_get_bounds_type(item)
    obs_sceneitem_set_bounds_type(item, type)

    item 参数

    item参数为需要获取或设置边框类型的场景项对象。

    type 参数

    type参数为边框类型,可使用如下obspython模块变量进行设置,OBS_BOUNDS_NONE表示没有边框,OBS_BOUNDS_STRETCH表示拉伸,内容将填满并全部显示在边框中,OBS_BOUNDS_SCALE_INNER表示等比缩放到框内,内容将全部显示在边框中,并以保持原比例的方式尽量填充整个边框,OBS_BOUNDS_SCALE_OUTER表示等比缩放到框外,内容将以保持原比例的方式填充整个边框,并尽量确保内容全部显示在边框中,OBS_BOUNDS_SCALE_TO_WIDTH表示等比缩放到边框宽度,内容将以保持原比例的方式缩放,使其宽度与边框宽度一致,OBS_BOUNDS_SCALE_TO_HEIGHT表示等比缩放到边框高度,内容将以保持原比例的方式缩放,使其高度与边框高度一致,OBS_BOUNDS_MAX_ONLYOBS_BOUNDS_SCALE_INNER类似,表示等比缩放到框内,但内容不会超过其原始大小。

    在下面的代码中,我们设置了场景项Hi的位置,旋转角度和缩放。

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 设置场景项 Hi 的位置
    	pos = vec2()
    	pos.x = 100
    	pos.y = 100
    	obs.obs_sceneitem_set_pos(hi, pos)
    	# 设置场景项 Hi 的旋转角度
    	obs.obs_sceneitem_set_rot(hi, 30)
    	# 设置场景项 Hi 的缩放
    	scale = vec2()
    	scale.x = 1.5
    	scale.y = 1.5
    	obs.obs_sceneitem_set_scale(hi, scale)
    	# …

    保存和加载 OBS 场景项的变换

    obspython模块的obs_scene_save_transform_states函数,可用于将场景中所有场景项的变换,保存至一个数据设置对象。

    obs_scene_save_transform_states(scene, all_items)

    scene 参数

    scene参数为需要保存变换的场景项所在的场景对象。

    all_items 参数

    all_items参数为True时,保存场景中所有场景项的变换,为False时,仅保存选中场景项的变换。

    obspython模块的obs_scene_load_transform_states函数,可用于从一个数据设置对象加载场景项变换。

    obs_scene_load_transform_states(states)

    states 参数

    states参数为包含了场景项变换信息的数据设置对象。

    使用 obs_data_release 函数释放包含场景项变换信息的数据设置对象

    对于使用obs_scene_save_transform_states函数创建的包含场景项变换信息的数据设置对象,需要通过obs_data_release释放引用,否则 OBS 可能会出现错误。

    在下面的代码中,我们通过保存和加载所有场景项的变换信息,使得对场景项Hi角度的修改无效。

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 保存并加载场景项的变换信息,使中途的修改无效
    	states = obs.obs_scene_save_transform_states(scene, True)
    	# 设置场景项 Hi 的旋转角度
    	obs.obs_sceneitem_set_rot(hi, 90)
    	obs.obs_scene_load_transform_states(obs.obs_data_get_json(states))
    	# 释放数据设置对象
    	obs.obs_data_release(states)
    	# …

    获取和设置 OBS 场景项的选中状态

    obspython模块的obs_sceneitem_selectedobs_sceneitem_select函数,可用于获取和设置场景项的选中状态。需要说明的是,设置某个场景项的选中状态,并不会取消其他场景项的选中。

    obs_sceneitem_selected(item)
    obs_sceneitem_select(item, select)

    item 参数

    item参数为需要获取或设置选中状态的场景项对象。

    select 参数

    select参数为True时,表示选中场景项。

    获取和设置 OBS 场景项是否可见

    obspython模块的obs_sceneitem_visibleobs_sceneitem_set_visible函数,可用于获取和设置场景项是否可见。

    obs_sceneitem_visible(item)
    obs_sceneitem_set_visible(item, visible)

    item 参数

    item参数为需要获取或设置是否可见的场景项对象。

    visible 参数

    visible参数为True时,表示场景项可见。

    获取和设置 OBS 场景项的锁定状态

    obspython模块的obs_sceneitem_lockedobs_sceneitem_set_locked函数,可用于获取和设置场景项的锁定状态。

    obs_sceneitem_locked(item)
    obs_sceneitem_set_locked(item, locked)

    item 参数

    item参数为需要获取或设置锁定状态的场景项对象。

    locked 参数

    locked参数为True时,表示锁定场景项。

    获取和设置 OBS 场景项的转场特效

    obspython模块的obs_sceneitem_get_transitionobs_sceneitem_set_transition函数,可用于获取和设置场景项的转场特效。

    obs_sceneitem_get_transition(item, show)
    obs_sceneitem_set_transition(item, show, transition)

    item 参数

    item参数为需要获取或设置转场特效的场景项对象。

    show 参数

    show参数为True时,将获取或设置显示转场特效,为False时,将获取或设置隐藏转场特效。

    transition 参数

    transition参数为 OBS 转场特效对象。

    obspython模块的obs_sceneitem_get_transition_durationobs_sceneitem_set_transition_duration函数,可用于获取和设置场景项的转场特效持续时间。

    obs_sceneitem_get_transition_duration(item, show)
    obs_sceneitem_set_transition_duration(item, show, duration_ms)

    item 参数

    item参数为需要获取或设置转场特效持续时间的场景项对象。

    show 参数

    show参数为True时,将获取或设置显示转场特效的持续时间,为False时,将获取或设置隐藏转场特效的持续时间。

    duration_ms 参数

    duration_ms参数为转场特效的持续时间,以毫秒为单位。

    触发 OBS 场景项转场特效

    obspython模块的obs_sceneitem_do_transition函数,可用于触发场景项的转场特效。

    obs_sceneitem_do_transition(item, visible)

    item 参数

    item参数为需要触发转场特效的场景项对象。

    visible 参数

    visible参数为True时,触发显示转场特效,为False时,触发隐藏转场特效。

    在为场景项Hi设置隐藏转场特效之后,我们触发了该特效,这需要首先让Hi处于不可见(隐藏)状态。

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 为场景项 Hi 增加滑出的隐藏转场效果
    	transition = obs.obs_source_create('swipe_transition', 'hi_hide_transition', None, None)
    	obs.obs_sceneitem_set_transition(hi, False, transition)
    	obs.obs_sceneitem_do_transition(hi, False)
    	# 释放来源对象
    	obs.obs_source_release(transition)
    	# …

    获取和设置 OBS 场景项的缩放算法

    obspython模块的obs_sceneitem_get_scale_filterobs_sceneitem_set_scale_filter函数,可用于获取和设置场景项的缩放算法。

    obs_sceneitem_get_scale_filter(item)
    obs_sceneitem_set_scale_filter(item, filter)

    item 参数

    item参数为需要获取或设置缩放算法的场景项对象。

    filter 参数

    filter参数为缩放算法,可使用如下obspython模块变量来进行设置,OBS_SCALE_DISABLEOBS_SCALE_POINTOBS_SCALE_BICUBICOBS_SCALE_BILINEAROBS_SCALE_LANCZOS

    获取和设置 OBS 场景项的混合模式

    obspython模块的obs_sceneitem_get_blending_modeobs_sceneitem_set_blending_mode函数,可用于获取和设置场景项的混合模式。

    obs_sceneitem_get_blending_mode(item)
    obs_sceneitem_set_blending_mode(item, type)

    item 参数

    item参数为需要获取或设置混合模式的场景项对象。

    type 参数

    type参数为混合模式,可使用如下obspython模块变量来进行设置,OBS_BLEND_NORMALOBS_BLEND_ADDITIVEOBS_BLEND_SUBTRACTOBS_BLEND_SCREENOBS_BLEND_MULTIPLYOBS_BLEND_LIGHTENOBS_BLEND_DARKEN

    获取和设置 OBS 场景项的混合方式

    obspython模块的obs_sceneitem_get_blending_methodobs_sceneitem_set_blending_method函数,可用于获取和设置场景项的混合方式。

    obs_sceneitem_get_blending_method(item)
    obs_sceneitem_set_blending_method(item, method)

    item 参数

    item参数为需要获取或设置混合方式的场景项对象。

    method 参数

    method参数为混合方式,可使用如下obspython模块变量来进行设置,OBS_BLEND_METHOD_DEFAULTOBS_BLEND_METHOD_SRGB_OFF

    获取和设置 OBS 场景项的 ID

    场景项的 ID 是一个整数,不同场景项的 ID 不应相同,obspython模块提供了以下与场景项 ID 有关的函数。

    obs_sceneitem_get_id函数,可用于获取场景项的 ID。

    obs_sceneitem_set_id函数,可用于为场景项指定一个唯一的 ID,该函数不应被经常使用,他可能会产生一些错误。

    obs_sceneitem_get_id(item)
    obs_sceneitem_set_id(item)

    item 参数

    item参数为需要获取或设置 ID 的场景项对象。

    添加和释放对 OBS 场景项的引用

    obspython模块的obs_sceneitem_addref函数,可用于为场景项对象添加引用。每当你额外调用一次obs_sceneitem_addref函数,就需要对等增加一次obs_sceneitem_release函数的调用。

    obspython模块的obs_sceneitem_release函数,可用于为场景项对象释放引用,他适用于obs_scene_sceneitem_from_source函数所返回的场景项对象。

    obs_sceneitem_addref(item)
    obs_sceneitem_release(item)

    item 参数

    item参数为需要添加或释放引用的场景项对象。

    为场景创建并添加 OBS 分组

    obspython模块的obs_scene_add_groupobs_scene_add_group2函数,可用于为场景创建并添加分组,分组对象会作为函数的返回值。

    obs_scene_add_group(scene, name)
    obs_scene_add_group2(scene, name, signal)

    scene 参数

    scene参数为需要添加分组的场景对象。

    name 参数

    name参数为分组名称。

    signal 参数

    signal参数为True时,信号将刷新。

    获取 OBS 分组

    obspython模块的obs_scene_get_group函数,可用于获取场景中指定名称的分组。

    obs_scene_get_group(scene, name)

    scene 参数

    scene参数为需要获取的分组所在的场景对象。

    name 参数

    name参数为分组名称。

    取消 OBS 分组

    obspython模块的obs_sceneitem_group_ungroupobs_sceneitem_group_ungroup2函数,可用于取消一个分组。

    obs_sceneitem_group_ungroup(group)
    obs_sceneitem_group_ungroup2(group, signal)

    group 参数

    group参数为需要取消的分组对象。

    signal 参数

    signal参数为True时,信号将刷新。

    为 OBS 分组添加场景项

    obspython模块的obs_sceneitem_group_add_item函数,可用于为分组添加场景项。

    obs_sceneitem_group_add_item(group, item)

    group 参数

    group参数为需要添加场景项的分组对象。

    item 参数

    item参数为需要添加的场景项对象。

    下面的代码,将创建新的分组Message,并添加场景项Hi

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 创建一个新的分组,并将 Hi 加入其中
    	message = obs.obs_scene_add_group(scene, 'Message')
    	obs.obs_sceneitem_group_add_item(message, hi)
    	# …

    为 OBS 分组移除场景项

    obspython模块的obs_sceneitem_group_remove_item函数,可用于移除分组中的场景项。

    obs_sceneitem_group_remove_item(group, item)

    group 参数

    group参数为需要移除场景项的分组对象。

    item 参数

    item参数为需要从分组中移除的场景项对象。

    获取场景项所在的 OBS 分组

    obspython模块的obs_sceneitem_get_group函数,可用于获取场景项所在的分组。

    obs_sceneitem_get_group(scene, item)

    scene 参数

    scene参数为需要获取的分组所在的场景对象。

    item 参数

    item参数为分组中包含的场景项对象。

    获取 OBS 分组所在的场景

    obspython模块的obs_sceneitem_group_get_scene函数,可用于获取分组所在的场景。

    obs_sceneitem_group_get_scene(group)

    group 参数

    group参数为需要获取场景的分组对象。

    判断场景项是否为 OBS 分组

    obspython模块的obs_sceneitem_is_group函数,可用于判断一个场景项是否为分组。

    obs_sceneitem_is_group(item)

    item 参数

    item参数为需要判断是否为分组的场景项对象。

    在下面的代码中,我们首先获取了场景项Bye所在的分组,然后判断其是否为分组,理所当然,结果显示为True

    items_and_groups.py
    def test(props, prop):
    	# …
    	# 获取场景项 Bye 所在的分组
    	bye = obs.obs_scene_find_source_recursive(scene, 'Bye')
    	group = obs.obs_sceneitem_get_group(scene, bye)
    	obs.script_log(obs.LOG_INFO, f'是分组?{obs.obs_sceneitem_is_group(group)}')
    	# …
    [items_and_groups.py] 是分组?True

    源码

    items_and_groups.py·codebeatme/obs-python-scripting·GitHub