Welcome to mirror list, hosted at ThFree Co, Russian Federation.

expand_to_surrounding_cuts.py « operators « power_sequencer - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7129e42d981d1d72855a2af2971f7da7d8bfb926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2016-2020 by Nathan Lovato, Daniel Oakey, Razvan Radulescu, and contributors

# This file is part of Power Sequencer.

import bpy

from .utils.functions import slice_selection
from .utils.doc import doc_name, doc_idname, doc_brief, doc_description


class POWER_SEQUENCER_OT_expand_to_surrounding_cuts(bpy.types.Operator):
    """
    *Brief* Expand selected strips to surrounding cuts

    Finds potential gaps surrounding each block of selected sequences and extends the corresponding
    sequence handle to it
    """

    doc = {
        "name": doc_name(__qualname__),
        "demo": "",
        "description": doc_description(__doc__),
        "shortcuts": [
            ({"type": "E", "value": "PRESS", "ctrl": True}, {}, "Expand to Surrounding Cuts",)
        ],
        "keymap": "Sequencer",
    }
    bl_idname = doc_idname(__qualname__)
    bl_label = doc["name"]
    bl_description = doc_brief(doc["description"])
    bl_options = {"REGISTER", "UNDO"}

    margin: bpy.props.FloatProperty(
        name="Trim margin",
        description="Margin to leave on either sides of the trim in seconds",
        default=0.2,
        min=0,
    )
    gap_remove: bpy.props.BoolProperty(
        name="Remove gaps",
        description="When trimming the sequences, remove gaps automatically",
        default=True,
    )

    @classmethod
    def poll(cls, context):
        return context.selected_sequences

    def invoke(self, context, event):
        sequence_blocks = slice_selection(context, context.selected_sequences)
        for sequences in sequence_blocks:
            sequences_frame_start = min(
                sequences, key=lambda s: s.frame_final_start
            ).frame_final_start
            sequences_frame_end = max(sequences, key=lambda s: s.frame_final_end).frame_final_end

            frame_left, frame_right = find_closest_cuts(
                context, sequences_frame_start, sequences_frame_end
            )
            if sequences_frame_start == frame_left and sequences_frame_end == frame_right:
                continue

            to_extend_left = [s for s in sequences if s.frame_final_start == sequences_frame_start]
            to_extend_right = [s for s in sequences if s.frame_final_end == sequences_frame_end]

            for s in to_extend_left:
                s.frame_final_start = (
                    frame_left if frame_left < sequences_frame_start else sequences_frame_start
                )
            for s in to_extend_right:
                s.frame_final_end = (
                    frame_right if frame_right > sequences_frame_end else sequences_frame_end
                )
        return {"FINISHED"}


def find_closest_cuts(context, frame_min, frame_max):
    frame_left = max(
        context.sequences,
        key=lambda s: s.frame_final_end if s.frame_final_end <= frame_min else -1,
    ).frame_final_end
    frame_right = min(
        context.sequences,
        key=lambda s: s.frame_final_start if s.frame_final_start >= frame_max else 1000000,
    ).frame_final_start
    return frame_left, frame_right