2023年4月4日火曜日

 

このスクリプトでは、球体2と3のアニメーション部分が修正されています。1フレームあたりの移動距離は、移動する距離(30)をフレーム数で割って計算しています。そして、forループを使って各フレームでの位置を計算し、キーフレームを挿入しています。





import bpy


# 最初の球体を作成

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0, 0, 0))

sphere1 = bpy.context.object


# 2番目の球体を作成

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(10, 0, 0))

sphere2 = bpy.context.object


# アニメーションを作成

# 最初の球体のアニメーション

sphere1.location = (0, 30, 0)

sphere1.keyframe_insert(data_path="location", frame=1)


# 2番目の球体のアニメーション

sphere2.location = (10, 0, 0)

sphere2.keyframe_insert(data_path="location", frame=100)

sphere2.location = (10, 30, 0)

# sphere1が到着するフレームと同じフレームで到着するようにフレーム数を計算

frames_to_arrive = 500 - 100 - int(sphere2.location.x)

sphere2.keyframe_insert(data_path="location", frame=100 + frames_to_arrive)


# タイムラインを設定

bpy.context.scene.frame_start = 0

bpy.context.scene.frame_end = 500













import bpy

from mathutils import Vector


# 球体の個数

num_spheres = 11


# 最終到達地点

final_destination = Vector((0, 500, 0))


# 初期位置

initial_position = Vector((10, -50, 0))


# 1フレームあたりの移動距離

speed_per_frame = 0.2


# 同時移動か順次移動か

run_simultaneously = False


# 移動にかかる総時間(フレーム)

total_frames = num_spheres * 10 + 1


# アニメーション開始位置

start_frame = 0


# アニメーション終了位置

end_frame = total_frames - 1


# 各球体の移動開始位置を調整するための変数

starting_frames = [i * 10 for i in range(num_spheres)]


# 各球体を作成して初期設定を行う

for i in range(num_spheres):

    bpy.ops.mesh.primitive_uv_sphere_add(radius=1)

    sphere = bpy.context.object


    # 初期位置のキーフレームを設定

    sphere.location = initial_position + Vector((i * 10, 0, 0))

    sphere.keyframe_insert(data_path='location', frame=starting_frames[i])


    # 終了位置のキーフレームを設定

    sphere.location = final_destination

    sphere.keyframe_insert(data_path='location', frame=end_frame)


# 各球体の移動アニメーションを設定する

for i in range(num_spheres):

    sphere = bpy.context.scene.objects[i]


    # 位置のキーフレームを設定

    if run_simultaneously:

        for j in range(total_frames):

            frame = j - starting_frames[i]

            if frame >= 0:

                sphere.location = initial_position + Vector((i * 10, frame * speed_per_frame, 0))

                sphere.keyframe_insert(data_path='location', frame=j)

    else:

        for j in range(starting_frames[i], total_frames):

            sphere.location = initial_position + Vector((i * 10, (j - starting_frames[i]) * speed_per_frame, 0))

            sphere.keyframe_insert(data_path='location', frame=j)


# アニメーションを再生

bpy.context.scene.frame_end = end_frame

bpy.ops.screen.animation_play()


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

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