blender-voicevox-pl/subtitles.py

114 lines
3.5 KiB
Python

"""
字幕管理
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',
}
all_settings = default_settings.copy()
if extra_settings:
all_settings.update(extra_settings)
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_settings = {}
other_settings = {}
for prop, value in all_settings.items():
if prop.startswith('transform_'):
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)
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
except (AttributeError, TypeError) as e:
print(f"[Subtitle] location プロパティは利用できません: {e}")
if transform_settings and hasattr(text_strip, 'transform'):
for prop, value in transform_settings.items():
try:
setattr(text_strip.transform, prop, value)
except AttributeError:
print(f"[Subtitle] transform.{prop} プロパティは利用できません(スキップ)")
except Exception as e:
print(f"[Subtitle] transform.{prop} の設定に失敗: {e}")
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)