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

random_editor.py « kinoraw_tools - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5735bb3ed1b2dd80b4cac1bd91805b2d23a4aa17 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy, os, random
from bpy.props import IntProperty, BoolProperty, StringProperty

from . import functions

# CLASSES

class RandomScratchOperator(bpy.types.Operator):
    """ Random Scratch Operator """
    bl_idname = "sequencer.randomscratchoperator"
    bl_label = "Random Scratch Operator"

    @classmethod
    def poll(self, context):
        strip = functions.act_strip(context)
        scn = context.scene
        if scn and scn.sequence_editor and scn.sequence_editor.active_strip:
            return strip.type in ('META')
        else:
            return False

    def invoke(self, context, event):
        
        preferences = context.user_preferences
        random_frames = preferences.addons[__package__].preferences.random_frames

        sce = context.scene
        seq=sce.sequence_editor
        markers=sce.timeline_markers
        if seq:
            strip= seq.active_strip
            if strip != None:
                if "IN" and "OUT" in markers:
                    sin=markers["IN"].frame
                    sout=markers["OUT"].frame
                    
                    # select active strip 
                    strip = context.scene.sequence_editor.active_strip
                    stripname = strip.name
                    # collect strip names inside the meta
                    stripnames = []
                    stripnames.append(strip.name)
                    for i in seq.active_strip.sequences:
                        stripnames.append(i.name)
                    # get strip channel
                    channel = strip.channel
                    repeat = range(int((sout-sin)/random_frames))
                    print(sin, sout, sout-sin, (sout-sin)/random_frames, repeat)
                    for i in repeat:
                        
                        # select all related strips
                        for j in stripnames:
                            strip = seq.sequences_all[j]
                            strip.select = True
                        strip = seq.sequences_all[stripname]    
                        seq.active_strip = strip
                        # deselect all other strips
                        for j in context.selected_editable_sequences:
                            if j.name not in stripnames:
                                j.select=False
                        a = bpy.ops.sequencer.duplicate_move()
                        # select new strip
                        newstrip = seq.active_strip
                        # deselect all other strips
                        for j in context.selected_editable_sequences:
                            if j.name != newstrip.name:
                                j.select=False
                        # random cut
                        newstrip.frame_start = sin + i * random_frames
                        rand = functions.randomframe(newstrip)
                        functions.triminout(newstrip, rand, rand + random_frames)
                        newstrip.frame_start = i * random_frames + sin - newstrip.frame_offset_start
                        newstrip.channel = channel + 1
                
                
                else:
                    self.report({'WARNING'}, "there is no IN and OUT")
            bpy.ops.sequencer.reload()                   
        return {'FINISHED'} 


class LoadRandomEditOperator(bpy.types.Operator):
    """ Random Editor Operator """
    bl_idname = "sequencer.loadrandomeditoperator"
    bl_label = "Random Editor Operator"

    @classmethod
    def poll(self, context):
        strip = functions.act_strip(context)
        scn = context.scene
        if scn and scn.sequence_editor and scn.sequence_editor.active_strip:
            return strip.type in ('META')
        else:
            return False

    def invoke(self, context, event):
        
        #generate sources_dict
        sources_dict = functions.get_marker_dict(random_selected_scene, random_number_of_subsets)
        print("sources: ",sources_dict)
            
        #generate cut_list
        cut_dict = functions.get_cut_dict(currentscene, random_number_of_subsets)  
        print("cuts: ",cut_dict)
        
        #generate random_edit
        random_edit = functions.random_edit_from_random_subset(cut_dict, sources_dict)    
        print("random edit")
        for i in random_edit: print("fr {}: dur: {} / {} {}".format(i[0],i[1],i[2],i[3]))
        
        sce = bpy.context.scene
        seq=sce.sequence_editor
        markers=sce.timeline_markers
        strip= seq.active_strip
        stripname = strip.name
        if "IN" and "OUT" in markers:
            sin=markers["IN"].frame
            sout=markers["OUT"].frame    
            # collect strip names inside the meta
            stripnames = []
            stripnames.append(strip.name)
            for i in seq.active_strip.sequences:
                stripnames.append(i.name)
            # get strip channel
            channel = strip.channel
            #repeat = range(int((sout-sin)/random_frames))
            #print(sin, sout, sout-sin, (sout-sin)/random_frames, repeat)
            for i in random_edit:    
                # select all related strips
                for j in stripnames:
                    strip = seq.sequences_all[j]
                    strip.select = True
                strip = seq.sequences_all[stripname]    
                seq.active_strip = strip
                # deselect all other strips
                for j in bpy.context.selected_editable_sequences:
                    if j.name not in stripnames:
                        j.select=False
                        
                a = bpy.ops.sequencer.duplicate_move()
                # select new strip
                newstrip = seq.active_strip
                # deselect all other strips
                for j in bpy.context.selected_editable_sequences:
                    if j.name != newstrip.name:
                        j.select=False
                # random cut
                #newstrip.frame_start = sin + i * random_frames
                #rand = functions.randomframe(newstrip)
                functions.triminout(newstrip, MARKER, MARKER+DURATION)
                newstrip.frame_start = i * random_frames + sin - newstrip.frame_offset_start
                newstrip.channel = channel + 1
        else:
            self.report({'WARNING'}, "there is no IN and OUT")
        bpy.ops.sequencer.reload()             
        return {'FINISHED'}      

    
class RandomEditorPanel(bpy.types.Panel):
    """-_-_-"""
    bl_label = "Random Editor"
    bl_idname = "OBJECT_PT_RandomEditor"
    bl_space_type = 'SEQUENCE_EDITOR'
    bl_region_type = 'UI'

    @classmethod
    def poll(self, context):
        if context.space_data.view_type in {'SEQUENCER',
            'SEQUENCER_PREVIEW'}:
            strip = functions.act_strip(context)
            scn = context.scene
            preferences = context.user_preferences
            prefs = preferences.addons[__package__].preferences
            if scn and scn.sequence_editor and scn.sequence_editor.active_strip:
                if prefs.use_random_editor:
                    return strip.type in ('META')
        else:
            return False

    def draw_header(self, context):
        layout = self.layout
        layout.label(text="", icon="MOD_BUILD")

    def draw(self, context):

        preferences = context.user_preferences
        prefs = preferences.addons[__package__].preferences

        layout = self.layout
        layout.label("______ cut duration:)")
        layout = self.layout
        layout.prop(prefs, "random_frames")
        layout = self.layout
        layout.operator("sequencer.randomscratchoperator")
        layout = self.layout
        layout.operator("sequencer.loadrandomeditoperator")