0
0

Blenderファイルのサムネイルを取得するには

Posted at

はじめに

Blenderファイルのサムネイルを取得する方法を検索すると、BlenderのPythonでファイルを開いてレンダリングする方法が出てきたりします。
実は、Blenderファイル自体がサムネイルを持っているので、レンダリングする必要はありません。

サムネイルの設定

サムネイルの設定は、プリファレンスのセーブ&ロードのBlendファイルのファイルプレビュータイプです。

image1.png

「なし」を選んで保存したファイルからは、サムネイルが取得できないのでご注意ください。

サムネイルを取得する準備

Pythonを使いますが、Blenderに付属するPythonを使う必要はありません。
次のように必要なものをインストールしてください。

pip install blender-asset-tracer pillow

サムネールを取得してファイルに保存

sample.blendというBlenderファイルのサムネイルを取得してthmbnail.pngに保存するには、次のようにします。

import struct
from pathlib import Path
from PIL import Image
from blender_asset_tracer import blendfile

bf = blendfile.open_cached(Path("sample.blend"))
data = bf.find_blocks_from_code(b'TEST')[0].raw_data()
w, h = struct.unpack('>BxxxBxxx', data[:8])
image = Image.frombytes("RGBA", (w, h), data[8:])
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save("thmbnail.png")

transpose(Image.FLIP_TOP_BOTTOM)は、画像の上下を反転させます。

Blender4.2のファイルを試した限りでは次のように取得できましたが、完全に理解しているわけではないので、環境によっては動かないかもしれません。

thmbnail.png

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0