""" 字幕管理 Blenderのシーケンサーに字幕を追加 """ import bpy class SubtitleManager: """字幕の追加と管理を担当""" def add_subtitle( self, context, text: str, frame_start: int, duration_frames: int, font_size: int = 48, position_y: float = 0.1, channel: int = 2, **extra_settings, ): """ シーケンサーに字幕を追加 Args: context: Blenderコンテキスト text: 字幕テキスト frame_start: 開始フレーム duration_frames: 表示期間(フレーム数) font_size: フォントサイズ position_y: 垂直位置 (0.0-1.0) channel: チャンネル番号 """ scene = context.scene if not scene.sequence_editor: scene.sequence_editor_create() seq_editor = scene.sequence_editor # テキストストリップを追加 text_strip = seq_editor.strips.new_effect( name="Subtitle", type='TEXT', channel=channel, frame_start=frame_start, length=duration_frames, ) # テキスト内容を設定 text_strip.text = text # デフォルト設定 default_settings = { 'use_shadow': True, 'shadow_color': (0.0, 0.0, 0.0, 0.8), 'color': (1.0, 1.0, 1.0, 1.0), 'blend_type': 'ALPHA_OVER', 'align_x': 'CENTER', 'align_y': 'BOTTOM', } # デフォルト設定から開始し、extra_settingsで上書き all_settings = default_settings.copy() if extra_settings: all_settings.update(extra_settings) # font_sizeは常にパラメータの値を使用 all_settings['font_size'] = font_size # 位置の処理 position_x = all_settings.pop('position_x', 0.5) position_y_val = all_settings.pop('position_y', position_y) # Transform関連のプロパティを分離 transform_settings = {} other_settings = {} for prop, value in all_settings.items(): if prop.startswith('transform_'): # transform_offset_x → offset_x transform_prop_name = prop[len('transform_'):] transform_settings[transform_prop_name] = value else: other_settings[prop] = value # 通常のプロパティを設定 for prop, value in other_settings.items(): try: setattr(text_strip, prop, value) print(f"[Subtitle] 設定: {prop} = {value}") except AttributeError: print(f"[Subtitle] {prop} プロパティは利用できません(スキップ)") except Exception as e: print(f"[Subtitle] {prop} の設定に失敗: {e}") # 位置を設定 try: text_strip.location[0] = position_x text_strip.location[1] = position_y_val print(f"[Subtitle] location = ({position_x}, {position_y_val})") except (AttributeError, TypeError) as e: print(f"[Subtitle] location プロパティは利用できません: {e}") # Transformプロパティを設定 if transform_settings and hasattr(text_strip, 'transform'): for prop, value in transform_settings.items(): try: setattr(text_strip.transform, prop, value) print(f"[Subtitle] 設定: transform.{prop} = {value}") except AttributeError: print(f"[Subtitle] transform.{prop} プロパティは利用できません(スキップ)") except Exception as e: print(f"[Subtitle] transform.{prop} の設定に失敗: {e}") print(f"Subtitle added: '{text}' at frame {frame_start}") return text_strip def update_subtitle(self, text_strip, **kwargs): """既存の字幕を更新""" for key, value in kwargs.items(): if hasattr(text_strip, key): setattr(text_strip, key, value) def remove_subtitle(self, seq_editor, text_strip): """字幕を削除""" seq_editor.strips.remove(text_strip)