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

jump_to_cut.py « operators « power_sequencer - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02a0b9f99954be770087e069e3536620e95ab0be (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
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2016-2020 by Nathan Lovato, Daniel Oakey, Razvan Radulescu, and contributors
import bpy
from operator import attrgetter

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


class POWER_SEQUENCER_OT_jump_to_cut(bpy.types.Operator):
    """
    *brief* Jump to next/previous cut


    Jump to the next or the previous cut in the edit.  Unlike Blender's default tool, also
    works during playback.
    """

    doc = {
        "name": doc_name(__qualname__),
        "demo": "",
        "description": doc_description(__doc__),
        "shortcuts": [
            (
                {"type": "UP_ARROW", "value": "PRESS"},
                {"direction": "RIGHT"},
                "Jump to next cut or keyframe",
            ),
            (
                {"type": "DOWN_ARROW", "value": "PRESS"},
                {"direction": "LEFT"},
                "Jump to previous cut or keyframe",
            ),
        ],
        "keymap": "Frames",
    }
    bl_idname = doc_idname(__qualname__)
    bl_label = doc["name"]
    bl_description = doc_brief(doc["description"])
    bl_options = {"REGISTER", "UNDO"}

    direction: bpy.props.EnumProperty(
        name="Direction",
        description="Jump direction, either forward or backward",
        items=[
            ("RIGHT", "Right", "Jump forward in time"),
            ("LEFT", "Left", "Jump backward in time"),
        ],
    )

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

    def execute(self, context):
        frame_current = context.scene.frame_current

        fcurves = []
        animation_data = context.scene.animation_data
        if animation_data and animation_data.action:
            fcurves = animation_data.action.fcurves

        frame_target = -1

        # First find the closest cut, then if that sequence has an associated
        # fcurve, loop through the curve's keyframes.
        if self.direction == "RIGHT":
            frame_target = 100_000_000
            for s in context.sequences:
                if s.frame_final_end <= frame_current:
                    continue

                candidates = [frame_target, s.frame_final_end]
                if s.frame_final_start > frame_current:
                    candidates.append(s.frame_final_start)

                frame_target = min(candidates)

                for f in fcurves:
                    if s.name not in f.data_path:
                        continue

                    for k in f.keyframe_points:
                        frame = k.co[0]
                        if frame <= frame_current:
                            continue
                        frame_target = min(frame_target, frame)

        elif self.direction == "LEFT":
            for s in context.sequences:
                if s.frame_final_start >= frame_current:
                    continue

                candidates = [frame_target, s.frame_final_start]
                if s.frame_final_end < frame_current:
                    candidates.append(s.frame_final_end)

                frame_target = max(candidates)

                for f in fcurves:
                    if s.name not in f.data_path:
                        continue

                    for k in f.keyframe_points:
                        frame = k.co[0]
                        if frame >= frame_current:
                            continue
                        frame_target = max(frame_target, frame)

        if frame_target > 0 and frame_target != 100_000_000:
            context.scene.frame_current = int(frame_target)

        return {"FINISHED"}