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

trim_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: 0d4c012b0299f66295da291ccbfe6811aa6bcf9d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#
# Copyright (C) 2016-2019 by Nathan Lovato, Daniel Oakey, Razvan Radulescu, and contributors
#
# This file is part of Power Sequencer.
#
# Power Sequencer is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Power Sequencer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Power Sequencer. If
# not, see <https://www.gnu.org/licenses/>.
#
"""
Find the two closest cuts, trims and deletes all strips above in the range but leaves some
margin. Removes the newly formed gap.
"""
import bpy

from .utils.functions import convert_duration_to_frames, trim_strips
from .utils.doc import doc_name, doc_idname, doc_brief, doc_description
from .utils.functions import find_closest_surrounding_cuts_frames


class POWER_SEQUENCER_OT_trim_to_surrounding_cuts(bpy.types.Operator):
    """*Brief* Automatically trim to surrounding cuts with some time offset

    Finds the two cuts closest to the mouse cursor and trims the footage in between, leaving a
    little time offset. It's useful after you removed some bad audio but you need to keep some
    video around for a transition.

    By default, the tool leaves a 0.2 seconds margin on either side of the trim.
    """

    doc = {
        "name": doc_name(__qualname__),
        "demo": "",
        "description": doc_description(__doc__),
        "shortcuts": [
            (
                {"type": "LEFTMOUSE", "value": "PRESS", "shift": True, "alt": True},
                {},
                "Trim 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

    def invoke(self, context, event):
        if not context.sequences:
            return {"CANCELLED"}

        frame = context.region.view2d.region_to_view(
            x=event.mouse_region_x, y=event.mouse_region_y
        )[0]
        frame = round(frame)

        margin_frame = convert_duration_to_frames(context, self.margin)
        left_cut_frame, right_cut_frame = find_closest_surrounding_cuts_frames(context, frame)
        if abs(left_cut_frame - right_cut_frame) <= margin_frame * 2:
            self.report(
                {"WARNING"},
                ("The trim margin is larger than the gap\n" "Use snap trim or reduce the margin"),
            )
            return {"CANCELLED"}

        to_delete, to_trim = self.find_strips_in_range(context, left_cut_frame, right_cut_frame)
        trim_start, trim_end = (left_cut_frame + margin_frame, right_cut_frame - margin_frame)

        trim_strips(context, trim_start, trim_end, to_trim, to_delete)

        if self.gap_remove:
            frame_to_remove_gap = right_cut_frame - 1 if frame == right_cut_frame else frame
            # bpy.ops.anim.change_frame(frame_to_remove_gap)
            context.scene.frame_current = frame_to_remove_gap
            bpy.ops.power_sequencer.gap_remove()
            context.scene.frame_current = trim_start

        return {"FINISHED"}

    def find_strips_in_range(
        self, context, start_frame, end_frame, sequences=[], find_overlapping=True
    ):
        """
        Returns strips which start and end within a certain frame range, or that overlap a
        certain frame range
        Args:
        - start_frame, the start of the frame range
        - end_frame, the end of the frame range
        - sequences (optional): only work with these sequences.
        If it doesn't receive any, the function works with all the sequences in the current context
        - find_overlapping (optional): find and return a list of strips that overlap the
          frame range

        Returns a tuple of two lists:
        [0], strips entirely in the frame range
        [1], strips that only overlap the frame range
        """
        strips_in_range = []
        strips_overlapping_range = []
        sequences = sequences if sequences else context.sequences
        for s in sequences:
            if start_frame < s.frame_final_start <= end_frame:
                if start_frame <= s.frame_final_end < end_frame:
                    strips_in_range.append(s)
                elif find_overlapping:
                    strips_overlapping_range.append(s)
            elif find_overlapping and start_frame <= s.frame_final_end <= end_frame:
                strips_overlapping_range.append(s)
            if s.frame_final_start < start_frame and s.frame_final_end > end_frame:
                strips_overlapping_range.append(s)
        return strips_in_range, strips_overlapping_range