【Blender×Python】オブジェクトの頂点グループ情報を取得する方法について徹底解説

Blender

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

# vertex_groupsプロパティによるメッシュの頂点グループ情報の取得
"Object".vertex_groups

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

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にオブジェクトの頂点グループ情報を取得するの?

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

vertex_groupsプロパティによるメッシュの頂点グループ情報の取得

“Object”.vertex_groupsプロパティの基本構文

"Object".vertex_groups
→ VertexGroups: bpy_prop_collection

“Object”.vertex_groupsプロパティの使い方

import bpy

CubeObject = bpy.data.objects["Cube"]
CubeVertexGroups = CubeObject.vertex_groups
CubeMeshVertices = CubeObject.data.vertices

print(f"Cube Vertex Groups:\n{CubeVertexGroups} {type(CubeVertexGroups)}")
for key, value in CubeVertexGroups.items():
    print(f"[{key}] {value}, Name: {value.name}, Index: {value.index}, Lock Wieght: {value.lock_weight}")
    for v_key, v_value in CubeMeshVertices.items():
        print(f"- [{v_key}] {v_value}, {v_value.index}, {v_value.co}, {value.weight(v_value.index)}")

CubeVertexGroupList = [CubeVertexGroup for CubeVertexGroup in CubeVertexGroups]
print(f"Cube Vertex Group List:\n{CubeVertexGroupList}")
Cube Vertex Groups:
<bpy_collection[3], VertexGroups> <class 'bpy_prop_collection'>
[Group] <bpy_struct, VertexGroup("Group") at 0x0000027F247F96C8>, Name: Group, Index: 0, Lock Wieght: False
- [0] <bpy_struct, MeshVertex at 0x0000027F0A948D68>, 0, <Vector (-1.0000, -1.0000, -1.0000)>, 1.0
- [1] <bpy_struct, MeshVertex at 0x0000027F0A948D78>, 1, <Vector (-1.0000, -1.0000, 1.0000)>, 1.0
- [2] <bpy_struct, MeshVertex at 0x0000027F0A948D88>, 2, <Vector (-1.0000, 1.0000, -1.0000)>, 1.0
- [3] <bpy_struct, MeshVertex at 0x0000027F0A948D98>, 3, <Vector (-1.0000, 1.0000, 1.0000)>, 1.0
- [4] <bpy_struct, MeshVertex at 0x0000027F0A948DA8>, 4, <Vector (1.0000, -1.0000, -1.0000)>, 1.0
- [5] <bpy_struct, MeshVertex at 0x0000027F0A948DB8>, 5, <Vector (1.0000, -1.0000, 1.0000)>, 1.0
- [6] <bpy_struct, MeshVertex at 0x0000027F0A948DC8>, 6, <Vector (1.0000, 1.0000, -1.0000)>, 1.0
- [7] <bpy_struct, MeshVertex at 0x0000027F0A948DD8>, 7, <Vector (1.0000, 1.0000, 1.0000)>, 1.0
[Group.001] <bpy_struct, VertexGroup("Group.001") at 0x0000027F0A7ECE88>, Name: Group.001, Index: 1, Lock Wieght: False
- [0] <bpy_struct, MeshVertex at 0x0000027F0A948D68>, 0, <Vector (-1.0000, -1.0000, -1.0000)>, 0.0
- [1] <bpy_struct, MeshVertex at 0x0000027F0A948D78>, 1, <Vector (-1.0000, -1.0000, 1.0000)>, 0.0
- [2] <bpy_struct, MeshVertex at 0x0000027F0A948D88>, 2, <Vector (-1.0000, 1.0000, -1.0000)>, 0.0
- [3] <bpy_struct, MeshVertex at 0x0000027F0A948D98>, 3, <Vector (-1.0000, 1.0000, 1.0000)>, 0.0
- [4] <bpy_struct, MeshVertex at 0x0000027F0A948DA8>, 4, <Vector (1.0000, -1.0000, -1.0000)>, 0.0
- [5] <bpy_struct, MeshVertex at 0x0000027F0A948DB8>, 5, <Vector (1.0000, -1.0000, 1.0000)>, 0.0
- [6] <bpy_struct, MeshVertex at 0x0000027F0A948DC8>, 6, <Vector (1.0000, 1.0000, -1.0000)>, 0.0
- [7] <bpy_struct, MeshVertex at 0x0000027F0A948DD8>, 7, <Vector (1.0000, 1.0000, 1.0000)>, 0.0
[Group.002] <bpy_struct, VertexGroup("Group.002") at 0x0000027F24A2E1A8>, Name: Group.002, Index: 2, Lock Wieght: False
- [0] <bpy_struct, MeshVertex at 0x0000027F0A948D68>, 0, <Vector (-1.0000, -1.0000, -1.0000)>, 0.5
- [1] <bpy_struct, MeshVertex at 0x0000027F0A948D78>, 1, <Vector (-1.0000, -1.0000, 1.0000)>, 0.5
- [2] <bpy_struct, MeshVertex at 0x0000027F0A948D88>, 2, <Vector (-1.0000, 1.0000, -1.0000)>, 0.5
- [3] <bpy_struct, MeshVertex at 0x0000027F0A948D98>, 3, <Vector (-1.0000, 1.0000, 1.0000)>, 0.5
- [4] <bpy_struct, MeshVertex at 0x0000027F0A948DA8>, 4, <Vector (1.0000, -1.0000, -1.0000)>, 0.5
- [5] <bpy_struct, MeshVertex at 0x0000027F0A948DB8>, 5, <Vector (1.0000, -1.0000, 1.0000)>, 0.5
- [6] <bpy_struct, MeshVertex at 0x0000027F0A948DC8>, 6, <Vector (1.0000, 1.0000, -1.0000)>, 0.5
- [7] <bpy_struct, MeshVertex at 0x0000027F0A948DD8>, 7, <Vector (1.0000, 1.0000, 1.0000)>, 0.5
Cube Vertex Group List:
[bpy.data.objects['Cube']...VertexGroup, bpy.data.objects['Cube']...VertexGroup, bpy.data.objects['Cube']...VertexGroup]

まとめ

「bpy」(Blender Python API)は、Pythonを使ったBlenderの開発に非常に役立ちます。この記事では、オブジェクトの頂点グループ情報を取得する方法を紹介しました。ぜひ活用してみてください。

関連検索ワード

How to get vertex groups of object data?

関連キーワード

blender, python, get, vertex group, object, 取得, 頂点グループ, オブジェクト