0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Blender オブジェクトの頂点の軌道をファイル出力する

Posted at

Blenderでオブジェクトをアニメーションさせた時の頂点位置をcsvファイル出力する

Blenderの醍醐味は,アニメーション作りですよね!
せっかく作ったアニメーション.

他のソフトウェアで利用したり,
物理計算と比べたりします.
そして,グラフエディタでアニメーションの動きを
再調整する時に,オブジェクトの頂点がフレームごとに
どんな位置へ移動したのか,
数値として把握したいですよね!

そこで,オブジェクトの任意頂点(このPythonスクリプトでは,頂点 ID=2)
のグローバル座標値をcsvファイルとして,1から60フレーム目までを
出力するサンプルプログラムを作成した.

cube_anim.gif

タイムラインにキーイン.jpg

Blender内で下記のPythonスクリプトを実行すると,
デスクトップディレクトリに
position_allframes_matrix.csv
として保存されます.

object_position.py
# bpyインポート
import bpy
# メッシュ操作のため
import bmesh
import csv
import os

# 指定オブジェクトの頂点情報をCSVに出力する(全フレーム、オブジェクトマトリックスで取得)
def mesh_globalverts_allframes_matrix_to_csv(arg_objectname="Default", end_frame=60, output_filename="position_allframes_matrix.csv", vertex_id=2):
    """指定オブジェクトの頂点情報を1フレーム目から指定フレームまでの各フレームから取得し、CSVファイルに出力する(オブジェクトマトリックスを使用)

    Keyword Arguments:
        arg_objectname {str} -- 指定オブジェクト名 (default: {"Default"})
        end_frame {int} -- 終了フレーム (default: {60})
        output_filename {str} -- 出力CSVファイル名 (default: {"position_allframes_matrix.csv"})
        vertex_id {int} -- 出力する頂点のID (default: {2})

    Returns:
        bool -- 実行正否
    """

    # 指定オブジェクトを取得する
    selectob = bpy.data.objects.get(arg_objectname)

    # 指定オブジェクトが存在するか確認する
    if selectob is None:
        print(f"Error: Object '{arg_objectname}' not found.")
        return False

    # デスクトップディレクトリのパスを取得 (macOS)
    desktop_path = os.path.expanduser("~/Desktop")
    output_filepath = os.path.join(desktop_path, output_filename)

    # CSVファイルを開いてヘッダーを書き込む
    with open(output_filepath, 'w', newline='') as csvfile:
        csv_writer = csv.writer(csvfile)
        csv_writer.writerow(['Frame', 'Vertex ID', 'X', 'Y', 'Z'])

        # フレーム範囲をループ (1からend_frameまで)
        for frame in range(1, end_frame + 1):
            # フレームを移動
            bpy.context.scene.frame_set(frame)

            # Dependency Graphの更新
            bpy.context.view_layer.update()

            # グローバル座標への変換行列を取得する
            matrix_world = selectob.matrix_world

            # メッシュデータを取得 (編集モードではなく、オブジェクトデータから直接取得)
            mesh = selectob.data
            if vertex_id < len(mesh.vertices):
                vertex = mesh.vertices[vertex_id]
                global_coords = matrix_world @ vertex.co
                csv_writer.writerow([frame, vertex_id, global_coords.x, global_coords.y, global_coords.z])
            else:
                print(f"Warning: Vertex ID {vertex_id} not found in object '{arg_objectname}'.")

    print(f"Vertex information for '{arg_objectname}', Vertex ID {vertex_id}, frames 1 to {end_frame} exported to: {output_filepath}")
    return True

# 関数の実行例
print("mesh_globalverts_allframes_matrix_to_csv")
mesh_globalverts_allframes_matrix_to_csv("Cube", end_frame=60, output_filename="position_allframes_matrix.csv", vertex_id=2)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?