예제 #1
0
def startCountClick(props, prop):
    global itemSource
    global xCoord
    global yCoord
    global Debug_Mode
    global jsonItemData
    global imagePath
    global imageName
    global widthCount
    global heightCount

    if (obs.obs_frontend_preview_program_mode_active()):
        sceneSource = obs.obs_frontend_get_current_preview_scene()
    else:
        sceneSource = obs.obs_frontend_get_current_scene()
    sceneWidth = obs.obs_source_get_width(sceneSource)
    sceneHeight = obs.obs_source_get_height(sceneSource)

    xWidth = math.ceil(sceneWidth / widthCount)
    yHeight = math.ceil(sceneHeight / heightCount)
    blankImage = np.zeros(shape=[yHeight, xWidth, 4], dtype=np.uint8)
    cv2.rectangle(blankImage, (0, 0), (xWidth, yHeight), (169, 169, 169, 255),
                  2)

    if (os.path.isfile(imagePath + imageName)):
        os.remove(imagePath + imageName)

    cv2.imwrite(imagePath + imageName, blankImage)
    if Debug_Mode:
        print("startCount")
        print(imagePath + imageName)

    gridWidth = 0
    gridHeight = 0
    scene = obs.obs_scene_from_source(sceneSource)
    itemData = obs.obs_data_create_from_json(jsonItemData)
    itemSource = obs.obs_load_source(itemData)

    vec2 = obs.vec2()
    position = 0
    if itemSource != None:
        item = obs.obs_scene_find_source(scene, "OBSWindowGridItem")
        while item != None:
            obs.obs_sceneitem_remove(item)
            item = obs.obs_scene_find_source(scene, "OBSWindowGridItem")

        vec2.y = 0
        while gridHeight < sceneHeight:
            gridWidth = 0
            vec2.x = 0
            while gridWidth < sceneWidth:
                newItem = obs.obs_scene_add(scene, itemSource)
                obs.obs_sceneitem_set_pos(newItem, vec2)
                obs.obs_sceneitem_set_locked(newItem, True)
                obs.obs_sceneitem_set_order_position(newItem, position)
                position += 1
                vec2.x += xWidth
                gridWidth += xWidth
            vec2.y += yHeight
            gridHeight += yHeight
    def update_cursor(self):
        source = obs.obs_get_source_by_name(self.source_name)
        settings = obs.obs_data_create()
        if source is not None:
            scene_source = obs.obs_frontend_get_current_scene()
            scene_width = obs.obs_source_get_width(source)
            scene_height = obs.obs_source_get_height(source)
            scene = obs.obs_scene_from_source(scene_source)
            scene_item = obs.obs_scene_find_source(scene, self.source_name)
            if scene_item:
                scale = obs.vec2()
                obs.obs_sceneitem_get_scale(scene_item, scale)
                scene_width, scene_height = apply_scale(
                    scale.x, scale.y, scene_width, scene_height
                )
                next_pos = obs.vec2()
                next_pos.x, next_pos.y = get_position()
                next_pos.x -= scene_width / 2
                next_pos.y -= scene_height / 2
                # set position to center of source where cursor is
                obs.obs_sceneitem_set_pos(scene_item, next_pos)

            obs.obs_data_release(settings)
            obs.obs_scene_release(scene)
            obs.obs_source_release(source)
예제 #3
0
def add_text_to_scene(scene, text_element):
    """Places text source into some location within scene
    returns width and height to align next scene items later """

    with data_ar() as settings:
        set_text_source_settings(settings, text_element.text)
        with source_create_ar("text_ft2_source", text_element.name,
                              settings) as source:
            pos = obs.vec2()
            pos.x = text_element.x
            pos.y = text_element.y
            scene_item = obs.obs_scene_add(scene, source)
            obs.obs_sceneitem_set_pos(scene_item, pos)
            height = obs.obs_source_get_height(source)
            width = obs.obs_source_get_width(source)
            return width, height
예제 #4
0
 def source_size(self):
     source = obs.obs_get_source_by_name(self.source_name)
     w = obs.obs_source_get_width(source)
     h = obs.obs_source_get_height(source)
     obs.obs_source_release(source)
     return w, h
예제 #5
0
파일: discrop.py 프로젝트: UnaiM/discrop
def script_tick(seconds):  # OBS script interface.
    global discord_source

    source_name = obs.obs_data_get_string(settings, 'discord_source')
    if source_name != obs.obs_source_get_name(discord_source):
        obs.obs_source_release(
            discord_source)  # Doesn’t error even if discord_source == None.
        discord_source = obs.obs_get_source_by_name(source_name)

    if not client:
        return

    # NOTE: These are 0 when the source isn’t visible at all in the current scene. Not that it matters, but I was just weirded out by it until I got it.
    source_width = obs.obs_source_get_width(discord_source)
    source_height = obs.obs_source_get_height(discord_source)

    margin_top = MARGIN_TOP
    if not obs.obs_data_get_bool(settings, 'full_screen'):
        margin_top = margin_top + TITLE_BAR

    # Get Discord call layout distribution and caller size.
    people = [x for x in client.video]  # Mutability and shiz.
    nonvideo = obs.obs_data_get_bool(settings, 'show_nonvideo_participants')
    if nonvideo:
        people += client.audio
    count = len(people)
    if count == 1 and (not client.audio or not client.video and nonvideo):
        count = 2  # Discord adds a call to action that occupies the same space as a second caller.
    rows = None
    cols = None
    width = 0
    height = None
    offsetx = 0
    offsety = 0
    offset_last = None
    if source_width and source_height:
        totalw = source_width - MARGIN_SIDES * 2
        totalh = source_height - margin_top - MARGIN_BTM
        if totalw > 0 and totalh > 0:
            wide = None
            # Discord packs the callers in as many columns as possible, unless their videos appear bigger with fewer columns.
            for c in reversed(range(1, count + 1)):
                r = math.ceil(count / c)
                w = (totalw - CALLER_SPACING * (c - 1)) / c
                h = (totalh - CALLER_SPACING * (r - 1)) / r
                wi = w / h > CALLER_ASPECT
                if wi:
                    w = h * CALLER_ASPECT
                if w > width:
                    rows = r
                    cols = c
                    width = w
                    height = h
                    wide = wi
            if rows:
                # If the window is wider or taller than the callers fit in, Discord will center them as a whole.
                inner_width = (width * cols + CALLER_SPACING * (cols - 1))
                if wide:  # Wider than needed, therefore center horizontally.
                    offsetx = (totalw - inner_width) / 2
                else:  # Taller than needed, therefore center vertically.
                    height = width / CALLER_ASPECT  # We compared using widths only before, so height needs to be adjusted.
                    offsety = (totalh - (height * rows + CALLER_SPACING *
                                         (rows - 1))) / 2

                # If last row contains fewer callers than columns, Discord will center it.
                offset_last = count % cols
                if offset_last > 0:
                    offset_last = (inner_width -
                                   (width * offset_last + CALLER_SPACING *
                                    (offset_last - 1))) / 2

    # Apply necessary changes to relevant scene items.
    scene_sources = obs.obs_frontend_get_scenes()
    for scene_src in scene_sources:
        scene = obs.obs_scene_from_source(scene_src)  # Shouldn’t be released.
        items = obs.obs_scene_enum_items(scene)
        i = 0
        next_vis = None
        for item in reversed(items):
            _next_vis = None
            if obs.obs_sceneitem_get_source(
                    item) == discord_source:  # Shouldn’t be released.
                uid = int(
                    obs.obs_data_get_string(settings, f'participant{i}') or -1)
                visible = True
                try:
                    index = people.index(uid)
                except (IndexError, ValueError):
                    visible = False
                i += 1
                obs.obs_sceneitem_set_visible(item, visible)
                if visible and rows:
                    crop = obs.obs_sceneitem_crop()
                    obs.obs_sceneitem_get_crop(item, crop)
                    scale = obs.vec2()
                    obs.obs_sceneitem_get_scale(item, scale)
                    bounds = obs.vec2()
                    obs.obs_sceneitem_get_bounds(item, bounds)

                    # If item was set to not use a bounding box policy, calculate it from its other transform properties.
                    if obs.obs_sceneitem_get_bounds_type(
                            item) == obs.OBS_BOUNDS_NONE:
                        obs.vec2_set(
                            bounds,
                            scale.x * (source_width - crop.right - crop.left),
                            scale.y * (source_height - crop.bottom - crop.top))
                        obs.obs_sceneitem_set_bounds(item, bounds)

                    obs.obs_sceneitem_set_bounds_type(
                        item, obs.OBS_BOUNDS_SCALE_OUTER)
                    obs.obs_sceneitem_set_bounds_alignment(
                        item, 0
                    )  # obs.OBS_ALIGN_CENTER doesn’t seem to be implemented.

                    # Get top left corner of this caller.
                    r = math.ceil((index + 1) / cols)
                    c = index % cols + 1
                    x = MARGIN_SIDES + offsetx + (width +
                                                  CALLER_SPACING) * (c - 1)
                    if r == rows:
                        x = x + offset_last
                    y = margin_top + offsety + (height + CALLER_SPACING) * (r -
                                                                            1)

                    # Make sure the crop doesn’t overflow the item bounds.
                    aspect = bounds.x / bounds.y
                    clipx = 0
                    clipy = 0
                    if aspect > CALLER_ASPECT:
                        clipy = (height - width / aspect) / 2
                    else:
                        clipx = (width - height * aspect) / 2

                    crop.left = math.ceil(x + CALLER_BORDER + clipx)
                    crop.top = math.ceil(y + CALLER_BORDER + clipy)
                    crop.right = source_width - int(x + width - CALLER_BORDER -
                                                    clipx)
                    crop.bottom = source_height - int(y + height -
                                                      CALLER_BORDER - clipy)
                    obs.obs_sceneitem_set_crop(item, crop)

                    sx = abs(scale.x)
                    if uid == int(
                            obs.obs_data_get_string(settings, 'myself')
                            or -1) and uid in client.video:
                        sx = -sx
                    sy = scale.y
                    obs.vec2_set(scale, sx, sy)
                    obs.obs_sceneitem_set_scale(item, scale)
                if not nonvideo and obs.obs_data_get_bool(
                        settings, 'item_right_below'):
                    _next_vis = uid in client.audio
            elif next_vis is not None:
                obs.obs_sceneitem_set_visible(item, next_vis)
            next_vis = _next_vis
        obs.sceneitem_list_release(items)
    obs.source_list_release(scene_sources)