blender-voicevox-pl/subtitles.py
2026-02-04 19:51:39 +09:00

105 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
字幕管理
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,
):
"""
シーケンサーに字幕を追加
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
# Blender 5.0で利用可能なプロパティのみ設定
try:
text_strip.font_size = font_size
except AttributeError:
print(f"[Subtitle] font_size プロパティは利用できません")
try:
text_strip.use_shadow = True
text_strip.shadow_color = (0.0, 0.0, 0.0, 0.8)
except AttributeError:
print(f"[Subtitle] shadow プロパティは利用できません")
# 位置を設定
try:
text_strip.location[0] = 0.5 # 水平中央
text_strip.location[1] = position_y # 垂直位置
except (AttributeError, TypeError):
print(f"[Subtitle] location プロパティは利用できません")
# テキストの配置Blender 5.0では変更された可能性あり)
try:
text_strip.align_x = 'CENTER'
text_strip.align_y = 'BOTTOM'
except AttributeError:
# Blender 5.0では align_x/align_y が削除または変更された
print(f"[Subtitle] align_x/align_y プロパティは利用できません")
# テキストの色(白)
try:
text_strip.color = (1.0, 1.0, 1.0, 1.0)
except AttributeError:
print(f"[Subtitle] color プロパティは利用できません")
# ブレンドモード
try:
text_strip.blend_type = 'ALPHA_OVER'
except AttributeError:
print(f"[Subtitle] blend_type プロパティは利用できません")
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)