2023年4月4日火曜日

速度一緒の失敗





import bpy

import mathutils


# Define the coordinates of the starting point and end points

zion_ss = (0, 0, 0)

zion_ee1 = (30, 0, 0)

zion_ee2 = (30, 30, 0)


# 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


# Compute the speeds of the spheres

speed1 = zion_speed * dist2 / dist1

speed2 = zion_speed * dist1 / 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) * speed1

    sphere2.location = vec1.lerp(vec3, i / 100.0) * speed2

    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 = 1000


# Play the animation

bpy.ops.screen.animation_play()



To make sure that both spheres move the same distance per frame, you can adjust the speed1 and speed2 calculations to use the larger of dist1 and dist2 instead of computing the speeds independently based on each distance.


Here's the modified code:


python


With this modification, both spheres should move the same distance per frame, ensuring that they finish their journeys at the same time.


import bpy

import mathutils


# Define the coordinates of the starting point and end points

zion_ss = (0, 0, 0)

zion_ee1 = (30, 0, 0)

zion_ee2 = (30, 30, 0)




# 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


# Determine the larger distance

larger_dist = max(dist1, dist2)


zion_speed = 1


# Compute the speeds of the spheres

speed1 = zion_speed * larger_dist / dist1

speed2 = zion_speed * larger_dist / 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) * speed1

    sphere2.location = vec1.lerp(vec3, i / 100.0) * speed2

    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 = 1000


# 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, 0, 0)
zion_ee2 = (30, 30, 0)

# 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 of the spheres
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) * speed1
    sphere2.location = vec1.lerp(vec3, i / 100.0) * speed2
    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 = 1000

# Play the animation
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...