-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path__init__.py
343 lines (277 loc) · 11.7 KB
/
__init__.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# ##### 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 #####
# <pep8 compliant>
bl_info = {
"name": "Import EDL",
"author": "Campbell Barton, tintwotin",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "Sequencer -> Track View Properties",
"description": "Load a CMX formatted EDL into the sequencer",
"warning": "",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"Scripts/Import-Export/EDL_Import",
"category": "Import-Export",
}
import bpy
from bpy.props import (
StringProperty,
IntProperty,
PointerProperty,
)
from bpy.types import Operator
# ----------------------------------------------------------------------------
# Panel to show EDL Import UI
class SEQUENCER_PT_import_edl(bpy.types.Panel):
"""Path to an CMX 3600 EDL(.edl) file"""
bl_label = "EDL Import"
bl_space_type = 'SEQUENCE_EDITOR'
bl_region_type = 'UI'
def draw(self, context):
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False
scene = context.scene
edl_import_info = scene.edl_import_info
col = layout.column()
#col.prop(edl_import_info, "frame_offset")
col.prop(edl_import_info, "filepath", text="EDL File")
subcol = layout.column()
subcol.operator(ReloadEDL.bl_idname, text="Check For Missing Files")
if not edl_import_info.filepath == "":
subcol.enabled = True
else:
subcol.enabled = False
box = layout.box()
reel = None
for reel in scene.edl_import_info.reels:
col = box.column(align=True)
if reel.filepath == "":
col.prop(reel, "filepath", text="Missing: "+reel.name)
else:
col.prop(reel, "filepath", text="Found: "+reel.name)
if reel is None:
col = layout.column(align=True)
col.operator(ImportEDL.bl_idname)
col.enabled = False
else:
box.enabled = True
box.operator(FindReelsEDL.bl_idname)
layout.operator(ImportEDL.bl_idname)
# ----------------------------------------------------------------------------
# Main Operators
class ReloadEDL(Operator):
"""Reloads the EDL file and refreshes all reels"""
bl_idname = "sequencer.import_edl_refresh"
bl_label = "Refresh Reels"
def execute(self, context):
import os
from . import parse_edl
scene = context.scene
edl_import_info = scene.edl_import_info
filepath = edl_import_info.filepath
dummy_fps = 25
if not os.path.exists(filepath):
self.report({'ERROR'}, "File Not Found %r" % filepath)
return {'CANCELLED'}
elist = parse_edl.EditList()
if not elist.parse(filepath, dummy_fps):
self.report({'ERROR'}, "Failed to parse EDL %r" % filepath)
return {'CANCELLED'}
scene = context.scene
edl_import_info = scene.edl_import_info
bl_reels = edl_import_info.reels
data_prev = {reel.name: (reel.filepath, reel.frame_offset)
for reel in edl_import_info.reels}
reels = elist.reels_as_dict()
reels = [k for k in reels.keys() if k not in parse_edl.BLACK_ID]
# re-create reels collection, keeping old values
bl_reels.clear()
for k in sorted(reels):
reel = bl_reels.add()
reel.name = k
filepath, frame_offset = data_prev.get(k, (None, None))
if filepath is not None:
reel.filepath = filepath
reel.frame_offset = frame_offset
return {'FINISHED'}
class FindReelsEDL(Operator):
"""Scan a path for missing reel files, """ \
""" Matching by reel name and existing filename when set"""
bl_idname = "sequencer.import_edl_findreel"
bl_label = "Scan For Missing Files"
directory: StringProperty(
subtype='DIR_PATH',
)
@staticmethod
def missing_reels(context):
import os
scene = context.scene
edl_import_info = scene.edl_import_info
return [reel for reel in edl_import_info.reels
if not os.path.exists(reel.filepath)]
def execute(self, context):
import os
# walk over .avi, .mov, .wav etc.
def media_file_walker(path):
ext_check = bpy.path.extensions_movie | bpy.path.extensions_audio
for dirpath, dirnames, filenames in os.walk(path):
# skip '.git'
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
for filename in filenames:
fileonly, ext = os.path.splitext(filename)
ext_lower = ext.lower()
if ext_lower in ext_check:
yield os.path.join(dirpath, filename), fileonly
# scene = context.scene
# edl_import_info = scene.edl_import_info
bl_reels = FindReelsEDL.missing_reels(context)
assert(len(bl_reels))
# Search is as follows
# Each reel has a triple:
# ([search_names, ...], [(priority, found_file), ...], bl_reel)
bl_reels_search = [(set(), [], reel) for reel in bl_reels]
# first get the search names...
for reel_names, reel_files_found, reel in bl_reels_search:
reel_names_list = []
reel_names_list.append(reel.name.lower())
# add non-extension version of the reel name
if "." in reel_names_list[-1]:
reel_names_list.append(os.path.splitext(reel_names_list[-1])[0])
# use the filepath if set
reel_filepath = reel.filepath
if reel_filepath:
reel_filepath = os.path.basename(reel_filepath)
reel_filepath = os.path.splitext(reel_filepath)[0]
reel_names_list.append(reel_filepath.lower())
# when '_' are found, replace with space
reel_names_list += [reel_filepath.replace("_", " ")
for reel_filepath in reel_names_list
if "_" in reel_filepath]
#reel_names.update(reel_names_list)
# debug info
print("Searching or %d reels" % len(bl_reels_search))
for reel_names, reel_files_found, reel in bl_reels_search:
print("Reel: %r --> (%s)" % (reel.name, " ".join(sorted(reel_names))))
print()
for filename, fileonly in media_file_walker(self.directory):
for reel_names, reel_files_found, reel in bl_reels_search:
if fileonly.lower() in reel_names:
reel_files_found.append((0, filename))
else:
# check on partial match
for r in reel_names:
if fileonly.startswith(r):
reel_files_found.append((1, filename))
if fileonly.endswith(r):
reel_files_found.append((2, filename))
# apply back and report
tot_done = 0
tot_fail = 0
for reel_names, reel_files_found, reel in bl_reels_search:
if reel_files_found:
# make sure partial matches end last
reel_files_found.sort()
reel.filepath = reel_files_found[0][1]
tot_done += 1
else:
tot_fail += 1
self.report({'INFO'} if tot_fail == 0 else {'WARNING'},
"Found %d clips, missing %d" % (tot_done, tot_fail))
return {'FINISHED'}
def invoke(self, context, event):
import os
scene = context.scene
edl_import_info = scene.edl_import_info
if not FindReelsEDL.missing_reels(context):
self.report({'INFO'},
"Nothing to do, all reels point to valid files")
return {'CANCELLED'}
# default to the EDL path
if not self.directory and edl_import_info.filepath:
self.directory = os.path.dirname(edl_import_info.filepath)
wm = context.window_manager
wm.fileselect_add(self)
return {"RUNNING_MODAL"}
class ImportEDL(Operator):
"""Import an EDL file into the sequencer"""
bl_idname = "sequencer.import_edl"
bl_label = "Import Video Sequence"
def execute(self, context):
import os
from . import import_edl
scene = context.scene
edl_import_info = scene.edl_import_info
filepath = edl_import_info.filepath
reel_filepaths = {reel.name: reel.filepath
for reel in edl_import_info.reels}
reel_offsets = {reel.name: reel.frame_offset
for reel in edl_import_info.reels}
if not os.path.exists(filepath):
self.report({'ERROR'}, "File Not Found %r" % filepath)
return {'CANCELLED'}
msg = import_edl.load_edl(
scene, filepath,
reel_filepaths, reel_offsets,
edl_import_info.frame_offset)
if msg:
self.report({'WARNING'}, msg)
return {'FINISHED'}
# ----------------------------------------------------------------------------
# Persistent Scene Data Types (store EDL import info)
class EDLReelInfo(bpy.types.PropertyGroup):
name: StringProperty(
name="Name",
)
filepath: StringProperty(
name="Video File",
subtype='FILE_PATH',
)
frame_offset: IntProperty(
name="Frame Offset",
)
class EDLImportInfo(bpy.types.PropertyGroup):
filepath: StringProperty(
subtype='FILE_PATH',
)
reels: bpy.props.CollectionProperty(
type=EDLReelInfo,
)
frame_offset: IntProperty(
name="Global Frame Offset",
)
def register():
bpy.utils.register_class(ReloadEDL)
bpy.utils.register_class(FindReelsEDL)
bpy.utils.register_class(ImportEDL)
bpy.utils.register_class(SEQUENCER_PT_import_edl)
# edl_import_info
bpy.utils.register_class(EDLReelInfo)
bpy.utils.register_class(EDLImportInfo)
bpy.types.Scene.edl_import_info = PointerProperty(type=EDLImportInfo)
bpy.types.Scene.edl_reel_info = PointerProperty(type=EDLReelInfo)
def unregister():
bpy.utils.unregister_class(ReloadEDL)
bpy.utils.unregister_class(FindReelsEDL)
bpy.utils.unregister_class(ImportEDL)
bpy.utils.unregister_class(SEQUENCER_PT_import_edl)
# edl_import_info
bpy.utils.unregister_class(EDLImportInfo)
bpy.utils.unregister_class(EDLReelInfo)
del bpy.types.Scene.edl_import_info
del bpy.types.Scene.edl_reel_info