import mathutils
zion_ss = (0, 0, 0) # 始点の座標
zion_ee = (30, 30, 0) # 終点の座標
def distance(point1, point2):
vec1 = mathutils.Vector(point1)
vec2 = mathutils.Vector(point2)
return (vec1 - vec2).length
dist = distance(zion_ss, zion_ee)
print(dist)
以下は、Blenderで球体を作成し、始点と終点に向かって移動する2つの球体を描画するスクリプトです。両方の球体の速度は 1/dist となるように設定されています。
import bpy
import mathutils
# Define the coordinates of the starting point and end points
zion_ss = (0, 0, 0)
zion_ee1 = (30, 30, 0)
zion_ee2 = (30, 30, 30)
# Convert the points to vectors
vec1 = mathutils.Vector(zion_ss)
vec2 = mathutils.Vector(zion_ee1)
vec3 = mathutils.Vector(zion_ee2)
# Compute the distances between the vectors
dist1 = (vec1 - vec2).length
dist2 = (vec1 - vec3).length
zion_speed =1.0
zion_base = dist1 * dist2 * zion_speed
# Compute the speeds
speed1 = 1 / dist1
speed2 = 1 / dist2
# Create two spheres with a radius of 1
bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, enter_editmode=False, location=vec1)
sphere1 = bpy.context.active_object
bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, enter_editmode=False, location=vec1)
sphere2 = bpy.context.active_object
# Add keyframes to the spheres to move them to the end points
for i in range(0, 101):
bpy.context.scene.frame_set(i)
sphere1.location = vec1.lerp(vec2, i / 100.0)
sphere2.location = vec1.lerp(vec3, i / 100.0)
sphere1.keyframe_insert(data_path="location")
sphere2.keyframe_insert(data_path="location")
# Set the animation playback speed
bpy.context.scene.render.fps = 30
bpy.context.scene.render.fps_base = 1
bpy.context.scene.frame_end = 100
# Play the animation
bpy.ops.screen.animation_play()
import bpy
import mathutils
# Define the coordinates of the starting point and end points
zion_ss = (0, 0, 0)
zion_ee1 = (30, 30, 0)
zion_ee2 = (30, 30, 30)
# Convert the points to vectors
vec1 = mathutils.Vector(zion_ss)
vec2 = mathutils.Vector(zion_ee1)
vec3 = mathutils.Vector(zion_ee2)
# Compute the distances between the vectors
dist1 = (vec1 - vec2).length
dist2 = (vec1 - vec3).length
# Compute the speeds
speed1 = 1 / dist1
speed2 = 1 / dist2
# Create two spheres with a radius of 1
bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, enter_editmode=False, location=vec1)
sphere1 = bpy.context.active_object
bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, enter_editmode=False, location=vec1)
sphere2 = bpy.context.active_object
# Add keyframes to the spheres to move them to the end points
for i in range(0, 101):
bpy.context.scene.frame_set(i)
sphere1.location = vec1.lerp(vec2, i / 100.0)
sphere2.location = vec1.lerp(vec3, i / 100.0)
sphere1.keyframe_insert(data_path="location")
sphere2.keyframe_insert(data_path="location")
# Set the animation playback speed
bpy.context.scene.render.fps = 30
bpy.context.scene.render.fps_base = 1
bpy.context.scene.frame_end = 100
# Play the animation
bpy.ops.screen.animation_play()
以下は、2つの半径1の球体を作り、それぞれを始点と終点に移動させるBlender Pythonのスクリプトです。移動の速度は、2点間の距離に反比例するように設定されています。
import bpy
import mathutils
# 始点と終点の座標
zion_ss_1 = (0, 0, 0)
zion_ee_1 = (30, 30, 0)
zion_ss_2 = (0, 0, 0)
zion_ee_2 = (30, 30, 30)
# 2点間の距離を計算
def distance(point1, point2):
vec1 = mathutils.Vector(point1)
vec2 = mathutils.Vector(point2)
return (vec1 - vec2).length
dist_1 = distance(zion_ss_1, zion_ee_1)
dist_2 = distance(zion_ss_2, zion_ee_2)
# 始点と終点の座標から、移動ベクトルを計算
vec_1 = mathutils.Vector(zion_ee_1) - mathutils.Vector(zion_ss_1)
vec_2 = mathutils.Vector(zion_ee_2) - mathutils.Vector(zion_ss_2)
# シーンにオブジェクトを追加
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=zion_ss_1)
sphere1 = bpy.context.active_object
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=zion_ss_2)
sphere2 = bpy.context.active_object
# フレームごとに移動
for i in range(100):
# 進捗率を計算
progress_1 = i / 100
progress_2 = i / 100
# 進捗率に応じて、現在の位置を計算
pos_1 = mathutils.Vector(zion_ss_1) + vec_1 * progress_1
pos_2 = mathutils.Vector(zion_ss_2) + vec_2 * progress_2
# 現在位置までオブジェクトを移動
sphere1.location = pos_1
sphere2.location = pos_2
# オブジェクトをフレームに追加
sphere1.keyframe_insert(data_path="location", frame=i)
sphere2.keyframe_insert(data_path="location", frame=i)
# 2点間の距離に反比例して、次のフレームまでの待ち時間を計算
bpy.context.scene.frame_set(i)
bpy.context.scene.render.fps = 24
bpy.context.scene.render.frame_duration = 100
wait_time = 1 / (dist_1 * (1 - progress_1) + dist_2 * (1 - progress_2))
# 次のフレームに進む前に、一定時間待機
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1, time_limit=wait_time)
移動中の速度を一定にするには、移動距離をフレーム数で割ることで、1フレームごとに進む距離を求めることができます。
以下は、速度を1ブレンダー単位(BU)/秒に設定する例です。
python