トーラスを 傾ける
# 傾ける前の トーラス√2
import bpy
import math
# トーラス(Torus)オブジェクトを作成
bpy.ops.mesh.primitive_torus_add(
align='WORLD',
location=(0, 0, 0), # トーラスの中心座標 (x, y, z) を (0, 0, 0) に設定
rotation=(0, 0, 0), # トーラスの回転 (オイラー角: x, y, z) を (0, 0, 0) に設定
major_radius=math.sqrt(2), # 主半径(外径)を√2に設定
minor_radius=0.06, # 副半径(内径)を 0.06 に設定
major_segments=48, # 主セグメント数(円周方向の分割数)を 48 に設定
minor_segments=12 # 副セグメント数(管の円周方向の分割数)を 12 に設定
)
# オブジェクトを選択
obj = bpy.context.active_object
# オブジェクト名を変更
radius_value = round(math.sqrt(2), 2) # √2を小数点以下2桁で丸める
obj.name = f'半径長さ "radius = {radius_value} 中心(0,0,0)"'
import bpy
import mathutils
import math
def tilt_torus_to_point(torus_obj, target_point):
# トーラスの位置
torus_location = torus_obj.location
# トーラスの方向を点 (1, 0, 1) に向けるクォータニオンを求める
direction = (target_point - torus_location).normalized()
up = mathutils.Vector*1 # トーラスの上方向をz軸方向として設定
rotation_quaternion = direction.rotation_difference(up)
# トーラスを回転させる
torus_obj.rotation_mode = 'QUATERNION'
torus_obj.rotation_quaternion = rotation_quaternion
# トーラス(Torus)オブジェクトを作成
bpy.ops.mesh.primitive_torus_add(
align='WORLD',
location=(0, 0, 0), # トーラスの中心座標 (x, y, z) を (0, 0, 0) に設定
rotation=(0, 0, 0), # トーラスの回転 (オイラー角: x, y, z) を (0, 0, 0) に設定
major_radius=math.sqrt(2), # 主半径(外径)を√2に設定
minor_radius=0.06, # 副半径(内径)を 0.06 に設定
major_segments=48, # 主セグメント数(円周方向の分割数)を 48 に設定
minor_segments=12 # 副セグメント数(管の円周方向の分割数)を 12 に設定
)
# オブジェクトを選択
obj = bpy.context.active_object
# トーラスを点 (1, 0, 1) に向けるように傾ける
tilt_torus_to_point(obj, mathutils.Vector((1, 0, 1)))
# オブジェクト名に傾き情報を追加
euler_rotation = obj.rotation_euler
rx_deg = math.degrees(euler_rotation.x)
ry_deg = math.degrees(euler_rotation.y)
rz_deg = math.degrees(euler_rotation.z)
obj.name = f'半径長さ "radius = {round(math.sqrt(2), 2)} 中心(0,0,0)" 傾き(rx={rx_deg:.2f}, ry={ry_deg:.2f}, rz={rz_deg:.2f})"'
*1:0, 0, 1