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

synchronize_titles.py « operators « power_sequencer - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3df342f44c6a498042ce93eedcd580d5675b17f3 (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
#
# 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/>.
#
import bpy

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


# TODO: rewrite to sync strips to corresponding identifiers instead
# See https://github.com/GDquest/Blender-power-sequencer/issues/55
class POWER_SEQUENCER_OT_synchronize_titles(bpy.types.Operator):
    """
    *brief* Snap the selected image or text strips to the corresponding title marker


    The marker and strip names have to start with TITLE-001
    """

    doc = {
        "name": doc_name(__qualname__),
        "demo": "",
        "description": doc_description(__doc__),
        "shortcuts": [],
        "keymap": "Sequencer",
    }
    bl_idname = doc_idname(__qualname__)
    bl_label = doc["name"]
    bl_description = doc_brief(doc["description"])
    bl_options = {"REGISTER", "UNDO"}

    TITLE_REGEX = r"^TITLE-?([0-9]+)-?"

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

    def execute(self, context):
        markers = context.scene.timeline_markers
        selection = context.selected_sequences

        if not markers and selection:
            if not markers:
                self.report({"INFO"}, "No markers, operation cancelled.")
            else:
                self.report({"INFO"}, "No sequences selected, operation cancelled.")
            return {"CANCELLED"}

        title_markers = self.find_markers(context, self.TITLE_REGEX)
        if not title_markers:
            self.report({"INFO"}, "No title markers found, operation cancelled.")

        matched = self.match_sequences_and_markers(selection, title_markers, self.TITLE_REGEX)
        for s, m in matched:
            s.frame_start = m.frame
        return {"FINISHED"}

    def match_sequences_and_markers(self, sequences, markers, regex):
        """Takes a list of sequences, of markers, and checks if they both
            match a regular expression.
            Returns a list of pairs of sequence and marker as tuples

            Args:
            - sequences, the list of sequences
            - markers, a list of markers
            - regex, the regular expression to match"""
        if not sequences and markers and regex:
            raise AttributeError("missing attributes")

        import re
        from .utils.global_settings import SequenceTypes

        sequences = (s for s in sequences if s.type not in SequenceTypes.EFFECT)

        return_list = []
        re_title = re.compile(regex)
        for s in sequences:
            found = re_title.match(s.name)
            title_id = int(found.group(1)) if found else None
            for m in markers:
                found = re_title.match(m.name)
                marker_id = int(found.group(1)) if found else None
                if marker_id == title_id:
                    return_list.append((s, m))
                    break
        return return_list

    def find_markers(self, context, regex):
        """Finds and returns all markers using REGEX
        Args:
            - regex, the re match pattern to use"""
        if not regex:
            raise AttributeError("regex parameter missing")

        import re

        regex = re.compile(regex)
        markers = context.scene.timeline_markers
        markers = (m for m in markers if regex.match(m.name))
        return markers