2023年4月4日火曜日

点 テスト

 


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




注意点として、Blenderの単位はブレンダー単位(BU)であるため、移動速度を秒速1メートルにする場合、移動距離をメートル単位に変換する必要があります。



import bpy
from mathutils import Vector

zion_x = 0
zion_y = 0
end_position = Vector((300, 300, 0))
distance_per_frame = 0.0333  # 1フレームごとの移動距離(メートル単位)
run_simultaneously = False  # True なら同時移動、False なら順次移動

for a in range(10):
    # 新しい球体を作成
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1)
    sphere = bpy.context.object

    # 初期位置のキーフレームを設定
    sphere.location = Vector((zion_x + a * 10, zion_y, 0))
    sphere.keyframe_insert(data_path='location', frame=a * 10)

    # 終了位置のキーフレームを設定
    sphere.location = end_position
    sphere.keyframe_insert(data_path='location', frame=900)

    # 移動方向と距離を設定して位置のキーフレームを設定
    current_frame = a * 10
    current_pos = Vector((zion_x + a * 10, zion_y, 0))
    move_vector = (end_position - current_pos).normalized() * distance_per_frame
    while current_frame <= 90:
        current_pos += move_vector
        sphere.location = current_pos
        sphere.keyframe_insert(data_path='location', frame=current_frame)
        current_frame += 1

    # 終了位置で球体を停止
    sphere.location = end_position
    sphere.keyframe_insert(data_path='location', frame=900)

# アニメーションを再生
bpy.context.scene.frame_end = 500
bpy.ops.screen.animation_play()




zion_ss  = ( 0,0,0)
zion_ee = (30,30,0)

zion_ss = (ssx,ssy,ssz)
zion_ee = (eex,eey,eez)



import mathutils

def distance(point1, point2):
    vec1 = mathutils.Vector(point1)
    vec2 = mathutils.Vector(point2)
    return (vec1 - vec2).length


















# Define the coordinates of the starting point and end points
zion_ss1= (0, 0, 0)
zion_ss2= (30, 0, 0)
zion_ee1 = (30, 30, 0)
zion_ee2 = (30, 30, 30)









改良中 y=-30 中心 円周への球体36個

できた y= -30 中心で z=0平面移動  import bpy import math zion_collection_name = "線路レール 観察者" # コレクションを作成する col = bpy.data.collections.new(zio...