Blenderでは3D空間上に文字のオブジェクトを置いたりする機能はありますが
2D画像に文字を描画する機能は用意されていません
スクリプトで自動化するにあたって文字入れの操作をしたい場合もあったので
できる操作がないか探ってみました
直接的にフォントをラスタライズした画像を得る手段はないようなので
画面に文字を描画するためのblfモジュールを使ってオフスクリーンバッファ上に文字を描画させて
その画像を取得するという手順をとってみました
import bpy
import blf
import gpu
import numpy as np
# 作成する画像のサイズ
image_width = 1920
image_height = 1080
# テキストの描画位置(左下基準)
draw_pos_x = 100
draw_pos_y = 200
draw_text = "Hello, Blender! ハローブレンダー"
# テキスト画像を作成する関数
# Creating an image of text with python in Blender
def create_text_image(image_width, image_height, position_x, position_y, font_size, text):
# オフスクリーンバッファを作成
offscreen = gpu.types.GPUOffScreen(image_width, image_height)
# フレームバッファにバインド
with offscreen.bind():
fb = gpu.state.active_framebuffer_get()
# blfでテキストを描画
font_id = 0 # デフォルトフォント
blf.position(font_id, position_x, position_y, 0) # スクリーン座標(X, Y, Z)
blf.size(font_id, font_size, 72) # フォントサイズ
blf.color(font_id, 1.0, 1.0, 1.0, 1.0) # 白色
blf.draw(font_id, text) # テキストの描画
buffer = fb.read_color(0, 0, image_width, image_height, 4, 0, 'UBYTE')
buffer.dimensions = image_width * image_height *4
# オフスクリーンバッファを解放
offscreen.free()
return [v / 255 for v in buffer]
# 描画を実行
text_image = create_text_image(image_width, image_height, draw_pos_x, draw_pos_y, 80, draw_text)
# 新しい画像を作成
image_out = bpy.data.images.new("OutputImage", image_width, image_height)
image_out.pixels = text_image
# 一時画像を削除
#bpy.data.images.remove(image_out)
処理について海外を含め検索してみても質問はあるものの、処理例は見当たらなかったこともあって
今回コードは入力した文字列の描かれた画像オブジェクトを作成するシンプルな内容になっています
何かの参考になれば幸いです