MRが楽しい

MRやVRについて学習したことを書き残す

Blender3.0で利用可能なpythonスクリプトを作る その129(オイラー角(degrees)で回転情報を取得する)

本日は Blender の技術調査枠です。
Blender3.0で利用可能なpythonスクリプトを作ります。

オブジェクトの回転をオイラー角(degrees)で回転情報を取得する

オブジェクトの回転のオイラー角は rotation_quaternion 変数から取得した Quaternion 型を to_euler で変換するか rotation_euler 変数から参照できます。
docs.blender.org

この際、取得した回転情報はラジアン(弧度)で取得されるので度数法の角度に変更するには math.degrees を利用する必要があります。
docs.python.org

サンプルスクリプト

Quaternion 型をスクリプトの関数に渡すと度数法の角度でオイラー角が返ります。
・Script_convert_rotation_eulerdegrees.py

# bpyインポート
import bpy
# 型インポート
from mathutils import Quaternion
# 角度計算のため
import math

# 指定オブジェクトの回転情報をオイラー角(degree)を返す
def convert_rotation_eulerdegrees(arg_rotation:Quaternion) -> list:
    """指定オブジェクトの回転情報をオイラー角(degree)を返す

    Keyword Arguments:
        arg_rotation {Quaternion} -- 回転情報(Quaternion)

    Returns:
        list -- 回転(オイラー角)
    """

    # グローバル座標のトランスフォームを取得する
    rotation_euler = arg_rotation.to_euler('XYZ')

    rotation_euler_degrees = [math.degrees(rotation_euler[0]), math.degrees(rotation_euler[1]), math.degrees(rotation_euler[2])]

    return rotation_euler_degrees

# 関数の実行例
# 回転(オイラー角)を取得する
converteulerdegrees = convert_rotation_eulerdegrees(bpy.data.objects.get("Cube").rotation_quaternion)
# 返却値をコンソールに表示する
print(type(converteulerdegrees))
print(converteulerdegrees)