【Blender×Python】テクスチャ座標をメッシュデータから取得する方法について徹底解説

Blender

「bpy」とは、PythonからBlenderの機能を呼び出すことができるBlender Python APIのことです。Blenderはオープンソースの3Dアニメーションソフトウェアであり、「bpy」を使うことでBlenderの様々な機能をPythonスクリプトから呼び出して利用することができます。本記事では、「bpy」を用いたテクスチャ座標をメッシュデータから取得する方法を紹介します。

# uv_layersプロパティによるメッシュデータを用いたテクスチャ座標の取得
"Mesh".uv_layers["UVLayerName"].data

下記の様な内容で悩んでいる/困っている場合に使える方法を参考までにご共有させて頂きます。

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にテクスチャ座標をメッシュデータから取得するの?

また、「bpy」を使用する上で基礎的な情報は下記の記事で紹介しております。
他の「bpy」のクラスや関数について気になる方はこちらの記事をご覧ください。

uv_layersプロパティによるメッシュデータを用いたテクスチャ座標の取得

“Mesh”.uv_layers[“UVLayerName”].dataプロパティの基本構文

"Mesh".uv_layers["UVLayerName"].data
→ MeshUVLoopLayer.data: bpy_prop_collection

“Mesh”.uv_layers[“UVLayerName”].dataプロパティの使い方

import bpy

CubeMesh = bpy.data.meshes["Cube"]
CubeUVLayer = CubeMesh.uv_layers["UVMap"]
# CubeUVLayer = CubeMesh.uv_layers[0]
CubeMeshVertexTexture = CubeUVLayer.data

print(f"Cube Mesh Vertex Texture:\n{CubeMeshVertexTexture} {type(CubeMeshVertexTexture)}")

for key, value in CubeMeshVertexTexture.items():
    print(f"[{key}] {value}, {value.uv}")
Cube Mesh Vertex Texture:
<bpy_collection[24], MeshUVLoopLayer.data> <class 'bpy_prop_collection'>
[0] <bpy_struct, MeshUVLoop at 0x000002B223456988>, <Vector (0.3750, 0.0000)>
[1] <bpy_struct, MeshUVLoop at 0x000002B223456994>, <Vector (0.6250, 0.0000)>
[2] <bpy_struct, MeshUVLoop at 0x000002B2234569A0>, <Vector (0.6250, 0.2500)>
[3] <bpy_struct, MeshUVLoop at 0x000002B2234569AC>, <Vector (0.3750, 0.2500)>
[4] <bpy_struct, MeshUVLoop at 0x000002B2234569B8>, <Vector (0.3750, 0.2500)>
[5] <bpy_struct, MeshUVLoop at 0x000002B2234569C4>, <Vector (0.6250, 0.2500)>
[6] <bpy_struct, MeshUVLoop at 0x000002B2234569D0>, <Vector (0.6250, 0.5000)>
[7] <bpy_struct, MeshUVLoop at 0x000002B2234569DC>, <Vector (0.3750, 0.5000)>
[8] <bpy_struct, MeshUVLoop at 0x000002B2234569E8>, <Vector (0.3750, 0.5000)>
[9] <bpy_struct, MeshUVLoop at 0x000002B2234569F4>, <Vector (0.6250, 0.5000)>
[10] <bpy_struct, MeshUVLoop at 0x000002B223456A00>, <Vector (0.6250, 0.7500)>
[11] <bpy_struct, MeshUVLoop at 0x000002B223456A0C>, <Vector (0.3750, 0.7500)>
[12] <bpy_struct, MeshUVLoop at 0x000002B223456A18>, <Vector (0.3750, 0.7500)>
[13] <bpy_struct, MeshUVLoop at 0x000002B223456A24>, <Vector (0.6250, 0.7500)>
[14] <bpy_struct, MeshUVLoop at 0x000002B223456A30>, <Vector (0.6250, 1.0000)>
[15] <bpy_struct, MeshUVLoop at 0x000002B223456A3C>, <Vector (0.3750, 1.0000)>
[16] <bpy_struct, MeshUVLoop at 0x000002B223456A48>, <Vector (0.1250, 0.5000)>
[17] <bpy_struct, MeshUVLoop at 0x000002B223456A54>, <Vector (0.3750, 0.5000)>
[18] <bpy_struct, MeshUVLoop at 0x000002B223456A60>, <Vector (0.3750, 0.7500)>
[19] <bpy_struct, MeshUVLoop at 0x000002B223456A6C>, <Vector (0.1250, 0.7500)>
[20] <bpy_struct, MeshUVLoop at 0x000002B223456A78>, <Vector (0.6250, 0.5000)>
[21] <bpy_struct, MeshUVLoop at 0x000002B223456A84>, <Vector (0.8750, 0.5000)>
[22] <bpy_struct, MeshUVLoop at 0x000002B223456A90>, <Vector (0.8750, 0.7500)>
[23] <bpy_struct, MeshUVLoop at 0x000002B223456A9C>, <Vector (0.6250, 0.7500)>

まとめ

「bpy」(Blender Python API)は、Pythonを使ったBlenderの開発に非常に役立ちます。この記事では、テクスチャ座標をメッシュデータから取得する方法を紹介しました。ぜひ活用してみてください。

関連検索ワード

How to get vertex texture from mesh data?

関連キーワード

blender, python, get, vertex texture, mesh, 取得, テクスチャ座標, メッシュ