refactor and fixes for episode handling gav

This commit is contained in:
2026-09-01 15:44:11 +02:00
parent 01fccc63d9
commit 4e29c0e699
7 changed files with 448 additions and 164 deletions
+114 -18
View File
@@ -9,7 +9,7 @@ from bpy.props import (BoolProperty, StringProperty, FloatProperty,
from vse_toolbox.sequencer_utils import (get_strips, rename_strips, set_channels,
get_channel_index, new_text_strip, get_strip_at, get_channel_name,
create_shot_strip)
create_shot_strip, update_text_strips)
from vse_toolbox.scene_cut_detection import detect_scene_change
from vse_toolbox.bl_utils import get_scene_settings, get_strip_settings, get_addon_prefs
@@ -24,7 +24,11 @@ class VSETB_OT_rename(Operator):
#template : StringProperty(name="Strip Name", default="")
#increment : IntProperty(name="Increment", default=0)
selected_only : BoolProperty(name="Selected Only", default=True)
selected_only : BoolProperty(
name="Selected Only",
default=False,
description="Rename only selected strips; disabled renames the whole channel",
)
#start_number : IntProperty(name="Start Number", default=0, min=0)
#by_sequence : BoolProperty(
# name="Reset By Sequence",
@@ -34,9 +38,13 @@ class VSETB_OT_rename(Operator):
@classmethod
def poll(cls, context):
settings = get_scene_settings()
strip = context.active_strip
return settings.active_project and get_channel_name(strip) in ('Shots', 'Sequences')
if strip is None and context.scene.sequence_editor:
strip = next(
(s for s in context.scene.sequence_editor.strips if s.select),
None,
)
return strip is not None
def invoke(self, context, event):
scn = context.scene
@@ -49,19 +57,29 @@ class VSETB_OT_rename(Operator):
scn = context.scene
settings = get_scene_settings()
project = settings.active_project
if project is None and settings.projects:
project = settings.projects[0]
if project is None:
self.report({'ERROR'}, 'No VSE Toolbox project is loaded')
return
episode = project.episode_name
sequence = str(project.sequence_start_number).zfill(project.sequence_padding)
shot = str(project.shot_start_number).zfill(project.shot_padding)
strip = context.active_strip
if strip is None and context.scene.sequence_editor:
strip = next(
(s for s in context.scene.sequence_editor.strips if s.select),
None,
)
channel_name = get_channel_name(strip)
col = layout.column()
col.use_property_split = True
col.use_property_decorate = False
if channel_name == 'Shots':
if channel_name != 'Sequences':
col.prop(project, 'shot_template', text='Shot Name')
col.prop(project, 'shot_start_number', text='Start Number')
col.prop(project, 'shot_increment', text='Increment')
@@ -77,9 +95,9 @@ class VSETB_OT_rename(Operator):
col.prop(self, 'selected_only')
if channel_name == 'Shots':
if channel_name != 'Sequences':
label = project.shot_template.format(episode=episode, sequence=sequence, shot=shot)
elif channel_name == 'Sequences':
else:
label = project.sequence_template.format(episode=episode, sequence=sequence)
col.label(text=f'Renaming {label}')
@@ -88,12 +106,30 @@ class VSETB_OT_rename(Operator):
scn = context.scene
settings = get_scene_settings()
project = settings.active_project
if project is None:
# The config path is set during addon registration, but the
# project data is only populated when Load Settings is pressed.
bpy.ops.vse_toolbox.load_settings()
project = settings.active_project
if project is None and settings.projects:
project = settings.projects[0]
if project is None:
self.report({'ERROR'}, 'No VSE Toolbox project is loaded')
return {'CANCELLED'}
# Keep the scene's active project in sync with the fallback project;
# rename_strips reads it again from scene settings.
settings.project_name = project.name
strip = context.active_strip
if strip is None and context.scene.sequence_editor:
strip = next(
(s for s in context.scene.sequence_editor.strips if s.select),
None,
)
channel_name = get_channel_name(strip)
strips = get_strips(channel=channel_name, selected_only=self.selected_only)
if channel_name == 'Shots':
if channel_name != 'Sequences':
rename_strips(strips,
template=project.shot_template,
increment=project.shot_increment, start_number=project.shot_start_number,
@@ -152,8 +188,13 @@ class VSETB_OT_set_sequencer(Operator):
movie = movies[0]
movie.transform.scale_x = movie.transform.scale_y = 1
elem = movie.strip_elem_from_frame(scn.frame_current)
scn.render.resolution_x = elem.orig_width
scn.render.resolution_y = elem.orig_height
if elem is None and movie.elements:
elem = movie.elements[0]
if elem is not None:
scn.render.resolution_x = elem.orig_width
scn.render.resolution_y = elem.orig_height
else:
self.report({'WARNING'}, 'Cannot determine movie resolution.')
else:
self.report({'INFO'}, f'Cannot set Resolution. No Movie Found.')
@@ -200,16 +241,32 @@ class VSETB_OT_scene_cut_detection(Operator):
movie_type: EnumProperty(
items=[('ANIMATED', 'Animated', 'Use select filter from ffmpeg, best for animated frame'),
('STILL', 'Still', 'Use freezedetect filter from ffmpeg, best for board or text')],
name='Movie Type')
name='Movie Type', default='ANIMATED')
def invoke(self, context, event):
if context.scene.sequence_editor.channels.get('Shots'):
self.destination_channel_name = 'Shots'
sequencer = context.scene.sequence_editor
channels = sequencer.channels if sequencer else None
selected_strip = context.active_strip
if selected_strip is None and context.selected_strips:
selected_strip = context.selected_strips[0]
# Select active channel by default
if (strip := context.active_strip) and get_channel_name(strip) != 'Shots':
self.source_channel_name = get_channel_name(strip)
# Prefer the channel containing the selected strip. With no selected
# strip, use the first available channel (channel 0 in the UI list).
if selected_strip is not None:
source_channel = selected_strip.channel
self.source_channel_name = get_channel_name(selected_strip)
elif channels and len(channels):
source_channel = 0
self.source_channel_name = channels[0].name
else:
source_channel = 0
# Put the result on the immediately following channel when it exists.
# Do not overwrite it when that channel is unavailable.
if channels and source_channel + 1 < len(channels):
destination_channel = channels[source_channel + 1]
self.destination_channel_name = destination_channel.name
self.frame_start = context.scene.frame_start
self.frame_end = context.scene.frame_end
@@ -251,6 +308,12 @@ class VSETB_OT_scene_cut_detection(Operator):
def modal(self, context, event):
scn = context.scene
# Replace the previous detection result instead of mixing it with the
# new one. This is especially important when changing thresholds.
for old_strip in list(scn.sequence_editor.strips):
if old_strip.name.startswith("tmp_shot_"):
scn.sequence_editor.strips.remove(old_strip)
strips = get_strips(channel=self.source_channel_name)
i = 1
@@ -385,7 +448,9 @@ class VSETB_OT_set_stamps(Operator):
#stamps_strip = scn.sequence_editor.sequences.new_meta('Stamps', scn.frame_start, scn.frame_end)
#stamps_strip.channel = get_channel_index('Stamps')
scn.frame_set(scn.frame_current) # For update stamps
# frame_set() does not trigger frame_change_post when the frame does
# not change, so update the stamp text explicitly for the first view.
update_text_strips(scn)
return {"FINISHED"}
@@ -748,7 +813,7 @@ class VSETB_OT_create_sequence_strip(Operator):
sequence_name: StringProperty(
name="Sequence Name",
description="Name of the sequence (e.g. SC010)",
default="SC010",
default="SQ010",
)
@classmethod
@@ -763,6 +828,37 @@ class VSETB_OT_create_sequence_strip(Operator):
return True
def invoke(self, context, event):
selected_shots = sorted(
(s for s in context.selected_strips if get_channel_name(s) == 'Shots'),
key=lambda s: s.frame_final_start,
)
if selected_shots:
first_frame = selected_shots[0].frame_final_start
sequence_channel = get_channel_index('Sequences')
previous = None
if sequence_channel:
previous = max(
(
s for s in context.scene.sequence_editor.strips
if s.channel == sequence_channel
and s.frame_final_end <= first_frame
),
key=lambda s: s.frame_final_end,
default=None,
)
if previous:
match = re.search(r'(\d+)$', previous.name)
if match:
settings = get_scene_settings()
project = settings.active_project
increment = project.sequence_increment if project else 10
number = int(match.group(1)) + increment
self.sequence_name = (
previous.name[:match.start()]
+ str(number).zfill(len(match.group(1)))
)
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
+53 -7
View File
@@ -205,6 +205,7 @@ class VSETB_OT_load_projects(Operator):
#print(metadata_data)
task_status = project.task_statuses.add()
task_status.name = status_data['short_name'].upper()
task_status.is_done = status_data.get('is_done', False)
project.task_types.clear()
for task_type_data in tracker.get_shot_task_types(project_data):
@@ -228,7 +229,10 @@ class VSETB_OT_load_projects(Operator):
if project.name not in project_names:
settings.projects.remove(list(settings.projects).index(project))
if self.ctrl or not settings.get('projects_loaded'):
# ``load_projects`` can also be called programmatically (for example
# from the upload dialog), bypassing ``invoke`` and therefore not
# initializing ``self.ctrl``.
if getattr(self, 'ctrl', False) or not settings.get('projects_loaded'):
bpy.ops.vse_toolbox.load_settings()
if prev_project_name != '/' and prev_project_name in settings.projects:
@@ -293,11 +297,19 @@ class VSETB_OT_new_episode(Operator):
class VSETB_OT_upload_to_tracker(Operator):
bl_idname = "vse_toolbox.upload_to_tracker"
bl_label = "Upload to tracker"
bl_description = "Upload selected strip to tracker"
bl_description = (
"Upload the selected strips on the Shots track to the tracker. "
"Creates missing sequences, shots and tasks, posts a comment "
'(with a preview, rendered if "Render Strips" is enabled) and '
"updates frames, metadata, casting and task comments."
)
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
if not get_strips(channel='Shots', selected_only=True):
cls.poll_message_set('Select at least one shot strip')
return False
return True
def invoke(self, context, event):
@@ -307,7 +319,14 @@ class VSETB_OT_upload_to_tracker(Operator):
tracker = prefs.tracker
tracker.connect()
# Loading the toolbox config can create the selected project locally,
# but does not populate its tracker metadata. In that case the task
# enum has no items even though Kitsu has shot task types configured.
if self.project and not self.project.task_types:
bpy.ops.vse_toolbox.load_projects()
self.project = settings.active_project
#self.bl_label = f"Upload to {settings.tracker_name.title()}"
return context.window_manager.invoke_props_dialog(self, width=350)
@@ -363,10 +382,26 @@ class VSETB_OT_upload_to_tracker(Operator):
format_data = {**settings.format_data, **self.project.format_data}
status = upload_to_tracker.status
if status == 'CURRENT':
keep_current_status = status == 'CURRENT'
if keep_current_status:
status = None
# ``CURRENT`` means preserve an existing task's status. A newly
# created task has no current status, so use Kitsu's done metadata for
# the first upload (prefer DONE when several statuses are marked done).
default_new_task_status = None
if keep_current_status:
done_statuses = [s for s in self.project.task_statuses if s.is_done]
default_new_task_status = next(
(s.name for s in done_statuses if s.name.lower() == 'done'),
done_statuses[0].name if done_statuses else None,
)
shot_strips = get_strips(channel='Shots', selected_only=True)
if not shot_strips:
self.report({'WARNING'}, 'Select at least one shot strip to upload')
return {'CANCELLED'}
context.window_manager.progress_begin(0, len(shot_strips))
for i, strip in enumerate(shot_strips):
@@ -392,7 +427,11 @@ class VSETB_OT_upload_to_tracker(Operator):
task = tracker.get_task(upload_to_tracker.task, entity=shot)
if not task:
task = tracker.new_task(shot, task_type=upload_to_tracker.task)
task = tracker.new_task(
shot,
task_type=upload_to_tracker.task,
status=default_new_task_status or status,
)
preview = None
if upload_to_tracker.add_preview:
@@ -439,9 +478,16 @@ class VSETB_OT_upload_to_tracker(Operator):
if upload_to_tracker.custom_data:
params['custom_data'] = metadata
params['description'] = strip_settings.description
if upload_to_tracker.update_frames:
# Blender's frame_final_end is exclusive, while Kitsu's
# frame_out is inclusive. Keep both the explicit range and
# the duration in sync with the uploaded strip.
params['frames'] = strip.frame_final_duration
params.setdefault('custom_data', {}).update({
'frame_in': strip.frame_final_start,
'frame_out': strip.frame_final_end - 1,
})
if params:
tracker.update_data(shot, **params)