2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Blender×Python] 3D Bouncing Ballとその他アニメーション

Last updated at Posted at 2021-01-09

#目次
0.振り出しにもどる動き
1.いったりきたりする動き
2.位置によって大きさを変える
3.3DのBouncingBall

#0.振り出しにもどる動き

ezgif.com-gif-maker (9).gif

.py
import bpy
#動かしたいオブジェクトを準備する
bpy.ops.mesh.primitive_uv_sphere_add()

#変数の準備
x = 0
y = 0
z = 0
x_speed = 5

ob = bpy.data.objects["Sphere"]
frame_num = 0

for i in range(0,100):
    
    bpy.context.scene.frame_set(frame_num)
    
    #もし、オブジェクトのx座標が100より大きいならば
    if (x > 100):
        #スタート地点に戻す
        x = 0
    
    #オブジェクトのx座標をx_speedだけずらす
    x += x_speed

    ob.location = (x,y,z)
    ob.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1

#1.いったりきたりする動き

ezgif.com-gif-maker (10).gif

.py
import bpy
bpy.ops.mesh.primitive_uv_sphere_add()

x = 0
y = 0
z = 0
x_speed = 5

ob = bpy.data.objects["Sphere"]
frame_num = 0

for i in range(0,100):
    
    bpy.context.scene.frame_set(frame_num)

    #オブジェクトのx座標が100より大きい、または、-100より小さいならば
    if (x > 100 or x < -100):
        #反転する
        x_speed *= -1 
    
    x += x_speed

    ob.location = (x,y,z)
    ob.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1

#2.位置によって大きさを変える

ezgif.com-gif-maker (7).gif

.py
import bpy
bpy.ops.mesh.primitive_uv_sphere_add()

x = 0
y = 0
z = 0
x_size = 10
y_size = 10
z_size = 10
x_speed = 2
s = 3

ob = bpy.data.objects["Sphere"]
frame_num = 0

for i in range(0,120):
    bpy.context.scene.frame_set(frame_num)

    もしオブジェクトのx座標が30より小さいならば
    if(x < 30):
        s = 3
    もしオブジェクトのx座標が60より小さいならば
    elif(x < 60):
        s = 6 
    もしオブジェクトのx座標が90より小さいならば
    elif(x < 90):
        s = 9
    もしオブジェクトのx座標が120より大きいならば
    elif(x > 120) :
        x = 0
    
    x += x_speed

    ob.location = (x,y,z)
    #サイズをsに応じて変化させる
    ob.scale = (x_size * s,y_size * s,z_size * s)
    ob.keyframe_insert(data_path = "location",index = -1)
    ob.keyframe_insert(data_path = "scale",index = -1)
    frame_num += 1

#3.3DのBouncingBall

ezgif.com-gif-maker (8).gif

.py
import bpy
bpy.ops.mesh.primitive_uv_sphere_add()

#r = 円の半径
r = 10
#動き回る範囲
x_domain = 50
y_domain = 50
z_domain = 50
x = 0
y = 0
z = 0
x_speed = 7
y_speed = 3
z_speed = 5


ob = bpy.data.objects["Sphere"]
frame_num = 0

for i in range(0,100):
    
    bpy.context.scene.frame_set(frame_num)
    
    #それぞれの閾値に達したらボールの向きを反転させる
    if (x > x_domain-r or x < -x_domain+r):
        x_speed*= -1
    if (y > y_domain-r or y < -y_domain+r):
        y_speed*= -1
    if (z > z_domain-r or z < -z_domain+r):
        z_speed*= -1
    
    x += x_speed
    y += y_speed
    z += z_speed

    ob.location = (x,y,z)
    ob.keyframe_insert(data_path = "location",index = -1)
    
    frame_num += 1
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?