【Blender×Python】メッシュデータを名称で取得する方法について徹底解説

Blender

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

# meshesプロパティによる名称を用いたメッシュデータの取得
bpy.data.meshes[MESH_NAME]

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

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にメッシュデータを名称で取得するの?

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

meshesプロパティによる名称を用いたメッシュデータの取得

bpy.data.meshesプロパティの基本構文

bpy.data.meshes[MESH_NAME: str]
→ Mesh: bpy_types.Mesh

bpy.data.meshesプロパティの使い方

import bpy

SampleMesh = bpy.data.meshes["Cube"]
print(f"Mesh:\n{SampleMesh} {type(SampleMesh)}")

print(SampleMesh.name)
print(SampleMesh.name_full)

print(SampleMesh.vertices)
for key, value in SampleMesh.vertices.items():
    print(f"[{key}] {value}, {value.index}, {value.co}")

print(SampleMesh.edges)
for key, value in SampleMesh.edges.items():
    print(f"[{key}] {value}, {value.index}, {value.vertices}, {[vertex_id for vertex_id in value.vertices]}")

print(SampleMesh.polygons)
for key, value in SampleMesh.polygons.items():
    print(f"[{key}] {value}, {value.index}, {value.vertices}, {[vertex_id for vertex_id in value.vertices]}")

print(SampleMesh.materials)
for key, value in SampleMesh.materials.items():
    print(f"[{key}] {value}, {[node.name for node in value.node_tree.nodes]}")

print(SampleMesh.vertex_normals)
for key, value in SampleMesh.vertex_normals.items():
    print(f"[{key}] {value}, {value.vector}")

print(SampleMesh.polygon_normals)
for key, value in SampleMesh.polygon_normals.items():
    print(f"[{key}] {value}, {value.vector}")

print(SampleMesh.vertex_colors)
for key, value in SampleMesh.vertex_colors.items():
    print(f"[{key}] {value}")
Mesh:
<bpy_struct, Mesh("Cube") at 0x000001A037DE8B88> <class 'bpy_types.Mesh'>
Cube
Cube
<bpy_collection[8], MeshVertices>
[0] <bpy_struct, MeshVertex at 0x000001A037709C68>, 0, <Vector (-1.0000, -1.0000, -1.0000)>
[1] <bpy_struct, MeshVertex at 0x000001A037709C78>, 1, <Vector (-1.0000, -1.0000, 1.0000)>
[2] <bpy_struct, MeshVertex at 0x000001A037709C88>, 2, <Vector (-1.0000, 1.0000, -1.0000)>
[3] <bpy_struct, MeshVertex at 0x000001A037709C98>, 3, <Vector (-1.0000, 1.0000, 1.0000)>
[4] <bpy_struct, MeshVertex at 0x000001A037709CA8>, 4, <Vector (1.0000, -1.0000, -1.0000)>
[5] <bpy_struct, MeshVertex at 0x000001A037709CB8>, 5, <Vector (1.0000, -1.0000, 1.0000)>
[6] <bpy_struct, MeshVertex at 0x000001A037709CC8>, 6, <Vector (1.0000, 1.0000, -1.0000)>
[7] <bpy_struct, MeshVertex at 0x000001A037709CD8>, 7, <Vector (1.0000, 1.0000, 1.0000)>
<bpy_collection[12], MeshEdges>
[0] <bpy_struct, MeshEdge at 0x000001A03770A848>, 0, <bpy_int[2], MeshEdge.vertices>, [2, 0]
[1] <bpy_struct, MeshEdge at 0x000001A03770A854>, 1, <bpy_int[2], MeshEdge.vertices>, [0, 1]
[2] <bpy_struct, MeshEdge at 0x000001A03770A860>, 2, <bpy_int[2], MeshEdge.vertices>, [1, 3]
[3] <bpy_struct, MeshEdge at 0x000001A03770A86C>, 3, <bpy_int[2], MeshEdge.vertices>, [3, 2]
[4] <bpy_struct, MeshEdge at 0x000001A03770A878>, 4, <bpy_int[2], MeshEdge.vertices>, [6, 2]
[5] <bpy_struct, MeshEdge at 0x000001A03770A884>, 5, <bpy_int[2], MeshEdge.vertices>, [3, 7]
[6] <bpy_struct, MeshEdge at 0x000001A03770A890>, 6, <bpy_int[2], MeshEdge.vertices>, [7, 6]
[7] <bpy_struct, MeshEdge at 0x000001A03770A89C>, 7, <bpy_int[2], MeshEdge.vertices>, [4, 6]
[8] <bpy_struct, MeshEdge at 0x000001A03770A8A8>, 8, <bpy_int[2], MeshEdge.vertices>, [7, 5]
[9] <bpy_struct, MeshEdge at 0x000001A03770A8B4>, 9, <bpy_int[2], MeshEdge.vertices>, [5, 4]
[10] <bpy_struct, MeshEdge at 0x000001A03770A8C0>, 10, <bpy_int[2], MeshEdge.vertices>, [0, 4]
[11] <bpy_struct, MeshEdge at 0x000001A03770A8CC>, 11, <bpy_int[2], MeshEdge.vertices>, [5, 1]
<bpy_collection[6], MeshPolygons>
[0] <bpy_struct, MeshPolygon at 0x000001A0376FCFE8>, 0, <bpy_int[4], MeshPolygon.vertices>, [0, 1, 3, 2]
[1] <bpy_struct, MeshPolygon at 0x000001A0376FCFF4>, 1, <bpy_int[4], MeshPolygon.vertices>, [2, 3, 7, 6]
[2] <bpy_struct, MeshPolygon at 0x000001A0376FD000>, 2, <bpy_int[4], MeshPolygon.vertices>, [6, 7, 5, 4]
[3] <bpy_struct, MeshPolygon at 0x000001A0376FD00C>, 3, <bpy_int[4], MeshPolygon.vertices>, [4, 5, 1, 0]
[4] <bpy_struct, MeshPolygon at 0x000001A0376FD018>, 4, <bpy_int[4], MeshPolygon.vertices>, [2, 6, 4, 0]
[5] <bpy_struct, MeshPolygon at 0x000001A0376FD024>, 5, <bpy_int[4], MeshPolygon.vertices>, [7, 3, 1, 5]
<bpy_collection[1], IDMaterials>
[Material] <bpy_struct, Material("Material") at 0x000001A043186208>, ['Principled BSDF', 'Material Output', 'Image Texture']
<bpy_collection[8], Mesh.vertex_normals>
[0] <bpy_struct, MeshNormalValue at 0x000001A04D29BEB8>, <Vector (-0.5774, -0.5774, -0.5774)>
[1] <bpy_struct, MeshNormalValue at 0x000001A04D29BEC4>, <Vector (-0.5774, -0.5774, 0.5774)>
[2] <bpy_struct, MeshNormalValue at 0x000001A04D29BED0>, <Vector (-0.5774, 0.5774, -0.5774)>
[3] <bpy_struct, MeshNormalValue at 0x000001A04D29BEDC>, <Vector (-0.5774, 0.5774, 0.5774)>
[4] <bpy_struct, MeshNormalValue at 0x000001A04D29BEE8>, <Vector (0.5774, -0.5774, -0.5774)>
[5] <bpy_struct, MeshNormalValue at 0x000001A04D29BEF4>, <Vector (0.5774, -0.5774, 0.5774)>
[6] <bpy_struct, MeshNormalValue at 0x000001A04D29BF00>, <Vector (0.5774, 0.5774, -0.5774)>
[7] <bpy_struct, MeshNormalValue at 0x000001A04D29BF0C>, <Vector (0.5774, 0.5774, 0.5774)>
<bpy_collection[6], Mesh.polygon_normals>
[0] <bpy_struct, MeshNormalValue at 0x000001A04D5FBF68>, <Vector (-1.0000, 0.0000, 0.0000)>
[1] <bpy_struct, MeshNormalValue at 0x000001A04D5FBF74>, <Vector (0.0000, 1.0000, 0.0000)>
[2] <bpy_struct, MeshNormalValue at 0x000001A04D5FBF80>, <Vector (1.0000, 0.0000, 0.0000)>
[3] <bpy_struct, MeshNormalValue at 0x000001A04D5FBF8C>, <Vector (0.0000, -1.0000, 0.0000)>
[4] <bpy_struct, MeshNormalValue at 0x000001A04D5FBF98>, <Vector (0.0000, 0.0000, -1.0000)>
[5] <bpy_struct, MeshNormalValue at 0x000001A04D5FBFA4>, <Vector (0.0000, 0.0000, 1.0000)>
<bpy_collection[0], LoopColors>

まとめ

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

関連検索ワード

How to get a mesh data by name?

関連キーワード

blender, python, get, mesh, name, 取得, メッシュ, 名称, 名前