UI周りの改善

This commit is contained in:
Keisuke Hirata 2026-02-19 12:55:30 +09:00
parent a3de61d5ce
commit d67265aa39
3 changed files with 197 additions and 115 deletions

View File

@ -50,27 +50,8 @@ def _set_strip_source(strip, filepath: str):
_reload_movie_strip(strip)
class SEQUENCER_OT_bake_and_swap_blur_source(Operator):
"""Bake masked blur and replace active strip source with baked video."""
bl_idname = "sequencer.bake_and_swap_blur_source"
bl_label = "Bake & Swap Source"
bl_description = "Bake masked blur to video and swap active strip source"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
if not context.scene.sequence_editor:
return False
# Prevent overlapping heavy tasks
if get_mask_generator().is_running:
return False
if get_bake_generator().is_running:
return False
strip = context.scene.sequence_editor.active_strip
return bool(strip and strip.type == "MOVIE")
def execute(self, context):
def _start_bake_impl(operator, context, force: bool = False):
"""Bakeの共通実装。force=True でキャッシュを無視して再Bakeする。"""
seq_editor = context.scene.sequence_editor
scene = context.scene
video_strip = seq_editor.active_strip
@ -78,10 +59,10 @@ class SEQUENCER_OT_bake_and_swap_blur_source(Operator):
video_path = bpy.path.abspath(video_strip.filepath)
detections_path = get_detections_path_for_strip(video_strip.name)
if not os.path.exists(video_path):
self.report({"ERROR"}, f"Source video not found: {video_path}")
operator.report({"ERROR"}, f"Source video not found: {video_path}")
return {"CANCELLED"}
if not os.path.exists(detections_path):
self.report({"ERROR"}, f"Detection cache not found: {detections_path}")
operator.report({"ERROR"}, f"Detection cache not found: {detections_path}")
return {"CANCELLED"}
bake_format = scene.facemask_bake_format
@ -89,7 +70,8 @@ class SEQUENCER_OT_bake_and_swap_blur_source(Operator):
blur_size = int(scene.facemask_bake_blur_size)
display_scale = float(scene.facemask_bake_display_scale)
# Reuse baked cache when parameters match and file still exists.
if not force:
# パラメータが一致するキャッシュがあればswapのみ
cached_baked_path = video_strip.get(KEY_BAKED)
cached_format = video_strip.get(KEY_FORMAT)
cached_blur_size = video_strip.get(KEY_BLUR_SIZE)
@ -112,7 +94,7 @@ class SEQUENCER_OT_bake_and_swap_blur_source(Operator):
if video_strip.get(KEY_MODE) != "baked":
video_strip[KEY_MODE] = "baked"
_set_strip_source(video_strip, cached_baked_path)
self.report({"INFO"}, "Using cached baked blur")
operator.report({"INFO"}, "Using cached baked blur")
return {"FINISHED"}
bake_generator = get_bake_generator()
@ -168,10 +150,85 @@ class SEQUENCER_OT_bake_and_swap_blur_source(Operator):
on_progress=on_progress,
)
except Exception as e:
self.report({"ERROR"}, f"Failed to start bake: {e}")
operator.report({"ERROR"}, f"Failed to start bake: {e}")
return {"CANCELLED"}
self.report({"INFO"}, "Started blur bake in background")
operator.report({"INFO"}, "Started blur bake in background")
return {"FINISHED"}
class SEQUENCER_OT_bake_and_swap_blur_source(Operator):
"""Bake masked blur (reuse cache if parameters match)."""
bl_idname = "sequencer.bake_and_swap_blur_source"
bl_label = "Bake"
bl_description = "Bake masked blur to video and swap active strip source"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
if not context.scene.sequence_editor:
return False
if get_mask_generator().is_running:
return False
if get_bake_generator().is_running:
return False
strip = context.scene.sequence_editor.active_strip
return bool(strip and strip.type == "MOVIE")
def execute(self, context):
return _start_bake_impl(self, context, force=False)
class SEQUENCER_OT_force_rebake_blur(Operator):
"""Force re-bake, ignoring any existing cached result."""
bl_idname = "sequencer.force_rebake_blur"
bl_label = "Re-bake"
bl_description = "Discard cached bake and re-bake from scratch"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
if not context.scene.sequence_editor:
return False
if get_mask_generator().is_running:
return False
if get_bake_generator().is_running:
return False
strip = context.scene.sequence_editor.active_strip
return bool(strip and strip.type == "MOVIE")
def execute(self, context):
return _start_bake_impl(self, context, force=True)
class SEQUENCER_OT_swap_to_baked_blur(Operator):
"""Swap active strip source to already-baked video (no re-bake)."""
bl_idname = "sequencer.swap_to_baked_blur"
bl_label = "Swap to Baked"
bl_description = "Switch active strip source to the baked video without re-baking"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
if not context.scene.sequence_editor:
return False
if get_bake_generator().is_running:
return False
strip = context.scene.sequence_editor.active_strip
if not strip or strip.type != "MOVIE":
return False
baked_path = strip.get(KEY_BAKED)
return bool(baked_path and os.path.exists(baked_path))
def execute(self, context):
strip = context.scene.sequence_editor.active_strip
baked_path = strip.get(KEY_BAKED)
_set_strip_source(strip, baked_path)
strip[KEY_MODE] = "baked"
self.report({"INFO"}, "Swapped to baked source")
return {"FINISHED"}
@ -179,7 +236,7 @@ class SEQUENCER_OT_restore_original_source(Operator):
"""Restore active strip source filepath to original video."""
bl_idname = "sequencer.restore_original_source"
bl_label = "Restore Original Source"
bl_label = "Restore Original"
bl_description = "Restore active strip to original source filepath"
bl_options = {"REGISTER", "UNDO"}
@ -192,6 +249,8 @@ class SEQUENCER_OT_restore_original_source(Operator):
strip = context.scene.sequence_editor.active_strip
if not strip or strip.type != "MOVIE":
return False
if strip.get(KEY_MODE, "original") == "original":
return False
return bool(strip.get(KEY_ORIGINAL))
def execute(self, context):
@ -215,7 +274,7 @@ class SEQUENCER_OT_apply_mask_blur(Operator):
bl_idname = "sequencer.apply_mask_blur"
bl_label = "Apply Mask Blur"
bl_description = "Compatibility alias for Bake & Swap Source"
bl_description = "Compatibility alias for Bake"
bl_options = {"REGISTER", "UNDO"}
@classmethod
@ -246,6 +305,8 @@ class SEQUENCER_OT_cancel_bake_blur(Operator):
classes = [
SEQUENCER_OT_bake_and_swap_blur_source,
SEQUENCER_OT_force_rebake_blur,
SEQUENCER_OT_swap_to_baked_blur,
SEQUENCER_OT_restore_original_source,
SEQUENCER_OT_cancel_bake_blur,
SEQUENCER_OT_apply_mask_blur,

View File

@ -58,10 +58,16 @@ class SEQUENCER_OT_generate_face_mask(Operator):
self.report({'INFO'}, f"Using cached detections from {output_dir}")
return {'FINISHED'}
# Get frame range
start_frame = strip.frame_final_start
end_frame = strip.frame_final_end
fps = scene.render.fps / scene.render.fps_base
# 動画の実際のフレーム数を取得Blenderプロジェクトのfpsと動画のfpsが
# 異なる場合にタイムライン上のフレーム数では不足するため)
import cv2 as _cv2
_cap = _cv2.VideoCapture(video_path)
total_video_frames = int(_cap.get(_cv2.CAP_PROP_FRAME_COUNT))
fps = _cap.get(_cv2.CAP_PROP_FPS) or (scene.render.fps / scene.render.fps_base)
_cap.release()
if total_video_frames <= 0:
self.report({'ERROR'}, f"Could not read frame count from video: {video_path}")
return {'CANCELLED'}
# Start async generation
generator = get_generator()
@ -105,7 +111,7 @@ class SEQUENCER_OT_generate_face_mask(Operator):
# Initialize progress
wm = context.window_manager
wm.mask_progress = 0
wm.mask_total = end_frame - start_frame + 1
wm.mask_total = total_video_frames
# Get parameters from scene properties
conf_threshold = scene.facemask_conf_threshold
@ -115,8 +121,8 @@ class SEQUENCER_OT_generate_face_mask(Operator):
generator.start(
video_path=video_path,
output_dir=output_dir,
start_frame=0, # Frame indices in video
end_frame=end_frame - start_frame,
start_frame=0,
end_frame=total_video_frames - 1,
fps=fps,
conf_threshold=conf_threshold,
iou_threshold=iou_threshold,

View File

@ -227,24 +227,39 @@ class SEQUENCER_PT_face_mask(Panel):
col.prop(context.scene, "facemask_bake_display_scale")
col.prop(context.scene, "facemask_bake_format")
# Source status
source_mode = strip.get("facemask_source_mode", "original")
if source_mode == "baked":
box.label(text="Source: Baked", icon='CHECKMARK')
else:
box.label(text="Source: Original", icon='FILE_MOVIE')
box.separator()
# Bake and restore buttons
baked_path = strip.get("facemask_baked_filepath", "")
has_baked = bool(baked_path and os.path.exists(bpy.path.abspath(baked_path)))
source_mode = strip.get("facemask_source_mode", "original")
if not has_baked:
# 初回: Bakeのみ
box.operator(
"sequencer.bake_and_swap_blur_source",
text="Bake & Swap Source",
text="Bake",
icon='RENDER_STILL',
)
box.operator(
else:
# Bake済み: ソース切り替え + Re-bake
row = box.row(align=True)
if source_mode == "baked":
row.operator(
"sequencer.restore_original_source",
text="Restore Original Source",
text="Restore Original",
icon='LOOP_BACK',
)
else:
row.operator(
"sequencer.swap_to_baked_blur",
text="Swap to Baked",
icon='PLAY',
)
row.operator(
"sequencer.force_rebake_blur",
text="Re-bake",
icon='FILE_REFRESH',
)
# Registration