円柱をz軸からx軸に回転させるには、次の手順を実行することができます。
- 「cylinder.rotation_euler」を次のように変更します。
luacylinder.rotation_euler = (math.pi/2, 0, math.pi/2)
これにより、円柱がz軸とx軸の間に90度回転し、x軸に平行になります。
- 「start_pos」と「end_pos」も次のように変更します。
makefilestart_pos = (0.0, -120.0, 0.0) end_pos = (0.0, -30.0, 0.0)
import bpy
import math
# シリンダーを作成する
bpy.ops.mesh.primitive_cylinder_add(radius=0.5, depth=60.0, location=(-120.0, 0.0, 0.0))
cylinder = bpy.context.object
# シリンダーの回転と位置を設定する
cylinder.rotation_euler = (math.pi/2, 0, 0)
cylinder.location = (0.0, 0.0, 120.0)
# Set start and end frames
start_frame = 1
end_frame = start_frame + 200
# Define the start and end positions
start_pos = (-120.0, 0.0, 0.0)
end_pos = (-30.0, 0.0, 0.0)
# Calculate the distance between start_pos and end_pos
distance = abs(math.sqrt((end_pos[0]-start_pos[0])**2 + (end_pos[1]-start_pos[1])**2 + (end_pos[2]-start_pos[2])**2))
# Set the speed multiplier
vvv_velocity = 2.0
# Calculate the velocity required to move the sphere at a constant speed
velocity = distance / (end_frame - start_frame) * vvv_velocity
# Move the sphere and cylinder from start_pos to end_pos at a constant speed
for i in range(start_frame, end_frame + 1):
bpy.context.scene.frame_set(i)
bpy.context.object.location = tuple(s + (e-s)*((i-start_frame)*velocity/distance) for s,e in zip(start_pos, end_pos))
bpy.context.object.keyframe_insert(data_path="location", index=-1)
bpy.context.object.keyframe_insert(data_path="scale", index=-1)
# Stop the sphere and cylinder from moving after reaching the end position
bpy.context.scene.frame_set(end_frame)
bpy.context.object.keyframe_insert(data_path="location", index=-1)
bpy.context.object.keyframe_insert(data_path="scale", index=-1)
# Set the waiting period
wait_frames = 40
end_frame += wait_frames
# Set the current frame to the end_frame
current_frame = end_frame
# Keep the sphere and cylinder at the end position and repeat the animation
repeat_frames = end_frame + 200
while True:
current_frame += 1
# Insert a keyframe at the current frame for the sphere and cylinder's location and scale
bpy.context.scene.frame_set(current_frame)
bpy.context.object.location = end_pos
bpy.context.object.keyframe_insert(data_path="location", index=-1)
bpy.context.object.keyframe_insert(data_path="scale", index=-1)
# Stop the loop if current_frame reaches repeat_frames
if current_frame == repeat_frames:
break
bbb