【Blender×Python】メッシュのシェイプキー情報を取得する方法について徹底解説

Blender

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

# shape_keys.key_blocksプロパティによるメッシュのシェイプキー情報の取得
"Mesh".shape_keys.key_blocks

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

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にメッシュのシェイプキー情報を取得するの?

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

shape_keys.key_blocksプロパティによるメッシュのシェイプキー情報の取得

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

"Mesh".shape_keys.key_blocks
→ Key.key_blocks: bpy_prop_collection

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

import bpy

CubeMesh = bpy.data.meshes["Cube"]
CubeMeshShapeKeys = CubeMesh.shape_keys.key_blocks

print(f"Cube Mesh Shape Keys:\n{CubeMeshShapeKeys} {type(CubeMeshShapeKeys)}")
for key, value in CubeMeshShapeKeys.items():
    print(f"[{key}] {value}")
    CubeMeshShapeKeyData = value.data
    for data_key, data_value in CubeMeshShapeKeyData.items():
        print(f"- [{data_key}] {data_value.co}")
Cube Mesh Shape Keys:
<bpy_collection[4], Key.key_blocks> <class 'bpy_prop_collection'>
[Basis] <bpy_struct, ShapeKey("Basis") at 0x000001A3C240F888>
- [0] <Vector (-1.0000, -1.0000, -1.0000)>
- [1] <Vector (-1.0000, -1.0000, 1.0000)>
- [2] <Vector (-1.0000, 1.0000, -1.0000)>
- [3] <Vector (-1.0000, 1.0000, 1.0000)>
- [4] <Vector (1.0000, -1.0000, -1.0000)>
- [5] <Vector (1.0000, -1.0000, 1.0000)>
- [6] <Vector (1.0000, 1.0000, -1.0000)>
- [7] <Vector (1.0000, 1.0000, 1.0000)>
[Key 1] <bpy_struct, ShapeKey("Key 1") at 0x000001A3C240F7C8>
- [0] <Vector (-1.0000, -1.0000, -1.0000)>
- [1] <Vector (-1.0000, -1.0000, 1.0000)>
- [2] <Vector (-1.0000, 1.0000, -1.0000)>
- [3] <Vector (-1.0000, 1.0000, 1.0000)>
- [4] <Vector (1.0000, -1.0000, -1.0000)>
- [5] <Vector (1.0000, -1.0000, 1.0000)>
- [6] <Vector (1.0000, 1.0000, -1.0000)>
- [7] <Vector (1.0000, 1.0000, 1.0000)>
[Key 2] <bpy_struct, ShapeKey("Key 2") at 0x000001A3C240F708>
- [0] <Vector (-1.0000, -1.0000, -1.0000)>
- [1] <Vector (-1.0000, -1.0000, 1.0000)>
- [2] <Vector (-1.0000, 1.0000, -1.0000)>
- [3] <Vector (-1.0000, 1.0000, 1.0000)>
- [4] <Vector (1.0000, -1.0000, -1.0000)>
- [5] <Vector (1.0000, -1.0000, 1.0000)>
- [6] <Vector (1.0000, 1.0000, -1.0000)>
- [7] <Vector (1.0000, 1.0000, 1.0000)>
[Key 3] <bpy_struct, ShapeKey("Key 3") at 0x000001A3C240F648>
- [0] <Vector (-1.0000, -1.0000, -1.0000)>
- [1] <Vector (-1.0000, -1.0000, 1.0000)>
- [2] <Vector (-1.0000, 1.0000, -1.0000)>
- [3] <Vector (-1.0000, 1.0000, 1.0000)>
- [4] <Vector (1.0000, -1.0000, -1.0000)>
- [5] <Vector (1.0000, -1.0000, 1.0000)>
- [6] <Vector (1.0000, 1.0000, -1.0000)>
- [7] <Vector (1.0000, 1.0000, 1.0000)>

まとめ

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

関連検索ワード

How to get shape keys of mesh data?

関連キーワード

blender, python, get, shape key, 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