なつねこメモ

主にプログラミング関連のメモ帳 ♪(✿╹ヮ╹)ノ

書いてあるコードは自己責任でご自由にどうぞ。記事本文の無断転載は禁止です。

2021/07/19

Python で任意の点を X, Y, Z 軸基準に回転させたい

カテゴリー:

相変わらず Blender Add-on を書いている私です。
ある座標にある点 (x, y, z) をそれぞれの軸基準に回転させたくなったので、
その方法を書いておきます。

といっても方法は簡単で、回転行列を使ってあげるだけ。
回転する角度は rotation(x, y, z) で、座標は point(x, y, z) で入力する

import math
import numpy as np
 
rotation_x = math.radians(rotation[0]) * -1
rotation_y = math.radians(rotation[1])
rotation_z = math.radians(rotation[2])
 
rot_x = np.array([
    [1, 0, 0],
    [0, math.cos(rotation_x), -math.sin(rotation_x)],
    [0, math.sin(rotation_x), math.cos(rotation_x)]
])
rot_y = np.array([
    [math.cos(rotation_y), 0, -math.sin(rotation_y)],
    [0, 1, 0],
    [math.sin(rotation_y), 0, math.cos(rotation_y)]
])
rot_z = np.array([
    [math.cos(rotation_z), -math.sin(rotation_z), 0],
    [math.sin(rotation_z), math.cos(rotation_z), 0],
    [0, 0, 1]
])
 
r = np.array([point[0], point[1], point[2]])
r = rot_x @ r
r = rot_y @ r
r = rot_z @ r
 
print(r)

おわり

Copyright (c) 2015 - 2023 Natsuneko <me@natsuneko.cat>