【Blender×Python】頂点法線をメッシュデータから取得する方法について徹底解説

Blender

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

# vertex_normalsプロパティによるメッシュデータを用いた頂点法線の取得
"Mesh".vertex_normals

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

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様に頂点法線をメッシュデータから取得するの?

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

vertex_normalsプロパティによるメッシュデータを用いた頂点法線の取得

“Mesh”.vertex_normalsプロパティの基本構文

"Mesh".vertex_normals
→ Mesh.vertex_normals: bpy_prop_collection

“Mesh”.vertex_normalsプロパティの使い方

import bpy

CubeMesh = bpy.data.meshes["Cube"]
CubeMeshVertexNormal = CubeMesh.vertex_normals

print(f"Cube Mesh Vertex Normal:\n{CubeMeshVertexNormal} {type(CubeMeshVertexNormal)}")

for key, value in CubeMeshVertexNormal.items():
    print(f"[{key}] {value}, {value.vector}")
Cube Mesh Vertex Normal:
<bpy_collection[8], Mesh.vertex_normals> <class 'bpy_prop_collection'>
[0] <bpy_struct, MeshNormalValue at 0x000002B23C0894B8>, <Vector (-0.5774, -0.5774, -0.5774)>
[1] <bpy_struct, MeshNormalValue at 0x000002B23C0894C4>, <Vector (-0.5774, -0.5774, 0.5774)>
[2] <bpy_struct, MeshNormalValue at 0x000002B23C0894D0>, <Vector (-0.5774, 0.5774, -0.5774)>
[3] <bpy_struct, MeshNormalValue at 0x000002B23C0894DC>, <Vector (-0.5774, 0.5774, 0.5774)>
[4] <bpy_struct, MeshNormalValue at 0x000002B23C0894E8>, <Vector (0.5774, -0.5774, -0.5774)>
[5] <bpy_struct, MeshNormalValue at 0x000002B23C0894F4>, <Vector (0.5774, -0.5774, 0.5774)>
[6] <bpy_struct, MeshNormalValue at 0x000002B23C089500>, <Vector (0.5774, 0.5774, -0.5774)>
[7] <bpy_struct, MeshNormalValue at 0x000002B23C08950C>, <Vector (0.5774, 0.5774, 0.5774)>

まとめ

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

関連検索ワード

How to get vertex normal from mesh data?

関連キーワード

blender, python, get, vertex normal, mesh, 取得, 頂点法線, メッシュ

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny