vse_toolbox/sequencer_utils.py

347 lines
10 KiB
Python
Raw Normal View History

2023-03-14 13:38:04 +01:00
# SPDX-License-Identifier: GPL-2.0-or-later
import re
2023-04-20 00:12:39 +02:00
from pathlib import Path
2023-04-21 21:44:05 +02:00
import os
2023-03-14 13:38:04 +01:00
2023-04-20 00:12:39 +02:00
import bpy
2023-03-17 20:03:38 +01:00
from bpy.app.handlers import persistent
2023-04-20 00:12:39 +02:00
2023-03-21 18:33:29 +01:00
from vse_toolbox.bl_utils import get_scene_settings, get_strip_settings
2023-04-04 12:28:31 +02:00
from vse_toolbox.constants import SOUND_SUFFIXES
2023-04-22 15:42:38 +02:00
import multiprocessing
2023-03-14 13:38:04 +01:00
2023-04-19 10:37:38 +02:00
2023-04-14 18:55:00 +02:00
def new_text_strip(name='Text', channel=0, start=0, end=50, text='Text', font_size=48,
x=0.5, y=0.5, align_x='CENTER', align_y='CENTER', select=False,
box_color=None, box_margin=0.005):
sequences = bpy.context.scene.sequence_editor.sequences
strip = sequences.new_effect(name, 'TEXT', channel, frame_start=start, frame_end=end)
strip.select = select
strip.text = text
strip.location.x = x
strip.location.y = y
strip.align_y = align_y
strip.align_x = align_x
strip.channel = channel
strip.font_size = font_size
if box_color:
strip.use_box = True
strip.box_color = box_color
strip.box_margin = box_margin
return strip
def is_strip_at(strip, frame=None):
if frame is None:
frame = bpy.context.scene.frame_current
return (strip.frame_final_start <= frame < strip.frame_final_end)
2023-03-17 20:03:38 +01:00
2023-03-14 13:38:04 +01:00
def get_strips(channel=0, selected_only=False):
scn = bpy.context.scene
if isinstance(channel, str):
2023-04-14 18:55:00 +02:00
channel = get_channel_index(channel)
2023-03-14 13:38:04 +01:00
strips = [s for s in scn.sequence_editor.sequences_all if s.channel==channel]
if selected_only:
strips = [s for s in strips if s.select]
return sorted(strips, key=lambda x : x.frame_final_start)
2023-04-14 18:55:00 +02:00
def get_strip_at(channel=0, frame=None):
strips = get_strips(channel=channel)
return next((s for s in strips if is_strip_at(s, frame)), None)
def get_channel_index(name):
2023-03-16 18:32:17 +01:00
scn = bpy.context.scene
channel_id = 0
channel = scn.sequence_editor.channels.get(name)
if channel:
channel_id = scn.sequence_editor.channels.keys().index(name)
return channel_id
2023-04-14 18:55:00 +02:00
def get_channel_name(strip):
if not strip:
return
scn = bpy.context.scene
return scn.sequence_editor.channels[strip.channel].name
def get_strip_sequence_name(strip):
sequence_strip = get_strip_at(channel='Sequences', frame=strip.frame_final_start)
if sequence_strip:
return sequence_strip.name
else:
return 'NoSequence'
2023-03-14 13:38:04 +01:00
def rename_strips(
strips, template, increment=10, start_number=0, by_sequence=False):
scn = bpy.context.scene
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-14 13:38:04 +01:00
project = settings.active_project
2023-04-14 18:55:00 +02:00
episode_name = ''
if settings.active_episode:
2023-04-19 10:37:38 +02:00
episode_name = settings.active_episode.name
2023-03-14 13:38:04 +01:00
2023-04-14 18:55:00 +02:00
prev_sequence_name = None
2023-03-14 13:38:04 +01:00
strip_number = 0
for strip in strips:
2023-04-14 18:55:00 +02:00
sequence_name = get_strip_sequence_name(strip)
2023-03-14 13:38:04 +01:00
2023-04-14 18:55:00 +02:00
if (by_sequence and prev_sequence_name and
sequence_name and sequence != prev_sequence_name):
2023-03-14 13:38:04 +01:00
strip_number = 0
name = template.format(
2023-04-14 18:55:00 +02:00
sequence=sequence_name,
episode=episode_name,
2023-03-14 13:38:04 +01:00
index=strip_number*increment+start_number
)
existing_strip = scn.sequence_editor.sequences_all.get(name)
if existing_strip:
existing_strip.name = f"{name}_tmp"
print(f'Renaming {strip.name} -> {name}')
strip.name = name
2023-04-14 18:55:00 +02:00
prev_sequence_name = sequence_name
2023-03-14 13:38:04 +01:00
strip_number += 1
def set_channels():
scn = bpy.context.scene
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-16 18:32:17 +01:00
items = settings.rna_type.bl_rna.properties['channel'].enum_items
2023-03-14 13:38:04 +01:00
for i, c in enumerate(items.keys(), start=1):
scn.sequence_editor.channels[i].name = c.title()
2023-04-20 00:12:39 +02:00
def get_strip_render_path(strip, template):
2023-04-20 18:16:34 +02:00
scn = bpy.context.scene
2023-04-21 21:44:05 +02:00
suffix = Path(scn.render.frame_path()).suffix
render_path = template.format(strip_name=strip.name, ext=suffix[1:])
return Path(os.path.abspath(bpy.path.abspath(render_path)))
2023-04-20 00:12:39 +02:00
def render_strips(strips, template):
scn = bpy.context.scene
scene_start = scn.frame_start
scene_end = scn.frame_end
2023-04-21 21:44:05 +02:00
render_path = scn.render.filepath
2023-04-20 00:12:39 +02:00
2023-04-22 15:42:38 +02:00
# pool = multiprocessing.Pool(4)
# p.map(func, range(1, 100))
# def render_strip_background(index):
# cmd = [bpy.app.binary_path, etc]
# process = subprocess.Popen(substr + " --index {}".format(index), shell=True, stdout=subprocess.PIPE)
2023-03-14 13:38:04 +01:00
for strip in strips:
2023-04-20 00:12:39 +02:00
#print(render_template, strip.name, path)
scn.frame_start = strip.frame_final_start
scn.frame_end = strip.frame_final_end - 1
## render animatic
#strip_render_path = render_template.format(strip_name=strip.name, ext=Path(scn.render.frame_path()).suffix)
2023-04-21 21:44:05 +02:00
scn.render.filepath = str(get_strip_render_path(strip, template))
2023-04-20 00:12:39 +02:00
#print(scn.render.filepath)
print(f'Render Strip to {scn.render.filepath}')
#bpy.ops.render.render(animation=True)
bpy.ops.render.opengl(animation=True, sequencer=True)
scn.frame_start = scene_start
scn.frame_end = scene_end
2023-04-21 21:44:05 +02:00
scn.render.filepath = render_path
2023-03-14 13:38:04 +01:00
2023-03-16 18:32:17 +01:00
def import_edit(filepath, adapter="cmx_3600", clean_sequencer=False):
2023-03-14 13:38:04 +01:00
import opentimelineio as otio
2023-03-16 18:32:17 +01:00
from opentimelineio.schema import (
Clip,
ExternalReference,
Gap,
ImageSequenceReference,
Stack,
Timeline,
Track,
)
2023-03-14 13:38:04 +01:00
scn = bpy.context.scene
sequences = scn.sequence_editor.sequences
2023-03-16 18:32:17 +01:00
if clean_sequencer:
movie = get_strips(channel='Movie')
audio = get_strips(channel='Audio')
2023-03-14 13:38:04 +01:00
2023-03-16 18:32:17 +01:00
for strip in sequences:
if strip not in (movie+audio):
sequences.remove(strip)
edl = Path(filepath)
2023-03-14 13:38:04 +01:00
try:
timeline = otio.adapters.read_from_file(
str(edl), adapter, rate=scn.render.fps, ignore_timecode_mismatch=True)
except:
2023-03-16 18:32:17 +01:00
print("[>.] read_from_file Failed. Using read_from_string method.")
2023-03-14 13:38:04 +01:00
data = edl.read_text(encoding='latin-1')
timeline = otio.adapters.read_from_string(
data, adapter, rate=scn.render.fps, ignore_timecode_mismatch=True)
scn.frame_start = (
0 if timeline.global_start_time is None else timeline.global_start_time
)
2023-03-16 18:32:17 +01:00
for track in timeline.tracks:
for child in track.each_child(shallow_search=True):
# FIXME Exclude Gaps for now. Gaps are Transitions, Blank Spaces...
if not isinstance(child, Clip):
continue
# FIXME Exclude Audio for now
2023-04-04 12:28:31 +02:00
if any(child.name.lower().endswith(ext) for ext in SOUND_SUFFIXES):
2023-04-14 18:55:00 +02:00
channel = get_channel_index('Audio')
2023-03-16 18:32:17 +01:00
continue
2023-04-14 18:55:00 +02:00
channel = get_channel_index('Shots')
2023-03-14 13:38:04 +01:00
frame_start = otio.opentime.to_frames(
child.range_in_parent().start_time)
2023-03-16 18:32:17 +01:00
frame_end = frame_start + otio.opentime.to_frames(
child.range_in_parent().duration)
2023-03-14 13:38:04 +01:00
try:
strip = sequences.new_effect(
2023-03-16 18:32:17 +01:00
name=child.name,
2023-03-14 13:38:04 +01:00
type='COLOR',
2023-03-16 18:32:17 +01:00
channel=channel,
2023-03-14 13:38:04 +01:00
frame_start=frame_start,
frame_end=frame_end,
)
2023-03-16 18:32:17 +01:00
strip.blend_alpha = 0.0
2023-03-17 20:03:38 +01:00
strip.select = False
2023-03-21 18:33:29 +01:00
strip.vsetb_strip_settings.source_name = child.name
2023-03-16 18:32:17 +01:00
2023-03-14 13:38:04 +01:00
except Exception as e:
print('e: ', e)
continue
2023-03-16 18:32:17 +01:00
scn.frame_end = frame_end-1
2023-03-14 13:38:04 +01:00
return timeline
2023-03-16 18:32:17 +01:00
def import_movie(filepath):
scn = bpy.context.scene
res_x = scn.render.resolution_x
res_y = scn.render.resolution_y
strip = scn.sequence_editor.sequences.new_movie(
name=filepath.stem,
filepath=str(filepath),
2023-04-14 18:55:00 +02:00
channel=get_channel_index('Movie'),
2023-03-16 18:32:17 +01:00
frame_start=scn.frame_start
)
elem = strip.strip_elem_from_frame(scn.frame_current)
src_width, src_height = elem.orig_width, elem.orig_height
if src_width != res_x:
strip.transform.scale_x = (res_x / src_width)
if src_height != res_y:
strip.transform.scale_y = (res_y / src_height)
if bpy.data.is_saved:
strip.filepath = bpy.path.relpath(str(filepath))
return strip
def import_sound(filepath):
scn = bpy.context.scene
strip = scn.sequence_editor.sequences.new_sound(
name=filepath.stem,
filepath=str(filepath),
2023-04-14 18:55:00 +02:00
channel=get_channel_index('Audio'),
2023-03-16 18:32:17 +01:00
frame_start=scn.frame_start
2023-03-14 13:38:04 +01:00
)
2023-03-16 18:32:17 +01:00
if bpy.data.is_saved:
strip.sound.filepath = bpy.path.relpath(str(filepath))
strip.show_waveform = True
2023-03-14 13:38:04 +01:00
return strip
2023-03-16 18:32:17 +01:00
def clean_sequencer(edit=False, movie=False, sound=False):
scn = bpy.context.scene
sequences = []
if edit:
sequences.extend(get_strips('Shots'))
if movie:
sequences.extend(get_strips('Movie'))
if sound:
sequences.extend(get_strips('Audio'))
for sequence in sequences:
2023-03-17 20:03:38 +01:00
scn.sequence_editor.sequences.remove(sequence)
@persistent
2023-04-14 18:55:00 +02:00
def set_active_strip(scene):
#scn = bpy.context.scene
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-17 20:03:38 +01:00
2023-04-14 18:55:00 +02:00
if not settings.auto_select_strip:
2023-03-17 20:03:38 +01:00
return
bpy.ops.sequencer.select_all(action="DESELECT")
2023-04-14 18:55:00 +02:00
#scene.sequence_editor.active_strip = None
2023-03-17 20:03:38 +01:00
2023-04-14 18:55:00 +02:00
shot_strip = get_strip_at('Shots')
if shot_strip:
shot_strip.select = True
scene.sequence_editor.active_strip = shot_strip
2023-03-17 20:03:38 +01:00
2023-04-14 18:55:00 +02:00
@persistent
def update_text_strips(scene):
#scn = bpy.context.scene
format_data = {
'scene': scene,
'active_shot_name': 'None',
'active_shot_frame': 0,
'active_shot_duration': 0,
'active_shot_start': 0,
'active_shot_end': 0
}
shot_strip = get_strip_at('Shots', frame=scene.frame_current)
if shot_strip:
format_data.update({
'active_shot_name': shot_strip.name,
'active_shot_duration': shot_strip.frame_final_duration,
'active_shot_frame': scene.frame_current - shot_strip.frame_final_start + 1,
'active_shot_start': shot_strip.frame_final_start,
'active_shot_end': shot_strip.frame_final_end,
})
for strip in scene.sequence_editor.sequences_all:
if not strip.type == 'TEXT':
continue
2023-03-17 20:03:38 +01:00
2023-04-14 18:55:00 +02:00
if not is_strip_at(strip):
2023-03-17 20:03:38 +01:00
continue
2023-04-14 18:55:00 +02:00
if '{' in strip.text:
strip['text_pattern'] = strip.text
if 'text_pattern' in strip.keys():
strip.text = strip['text_pattern'].format(**format_data)