[Blender x Python] 3D Bouncing Ball and other animations

table of contents

  1. Movement to return to the beginning
  2. Moving back and forth
  3. Change the size depending on the position 3.3D Bouncing Ball

0. Movement to return to the beginning

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

import bpy
#Prepare the object you want to move
bpy.ops.mesh.primitive_uv_sphere_add()

#Variable preparation
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)
    
    #If the x coordinate of the object is greater than 100
    if (x > 100):
        #Return to the starting point
        x = 0
    
    #The x coordinate of the object is x_Shift only speed
    x += x_speed

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

1. Moving back and forth

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

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)

    #The x coordinate of the object is greater than 100 or-If less than 100
    if (x > 100 or x < -100):
        #Invert
        x_speed *= -1 
    
    x += x_speed

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

2. Change the size depending on the position

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

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)

If the x coordinate of the object is less than 30
    if(x < 30):
        s = 3
If the x coordinate of the object is less than 60
    elif(x < 60):
        s = 6 
If the x coordinate of the object is less than 90
    elif(x < 90):
        s = 9
If the x coordinate of the object is greater than 120
    elif(x > 120) :
        x = 0
    
    x += x_speed

    ob.location = (x,y,z)
    #Change the size according to 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 Bouncing Ball

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

import bpy
bpy.ops.mesh.primitive_uv_sphere_add()

#r =Radius of circle
r = 10
#Range to move around
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)
    
    #Invert the direction of the ball when each threshold is reached
    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

Recommended Posts

[Blender x Python] 3D Bouncing Ball and other animations
[Blender x Python] Blender Python tips (11/100)
Install Python 2.7.9 and Python 3.4.x with pip.
[Blender x Python] Particle Animation (Part 1)
[Blender x Python] Let's master random !!
[Blender x Python] Let's master rotation !!
Easy modeling with Blender and Python
[Blender x Python] Let's master the material !!
Python> link> 2D array initialization and assignment
Differences between Numpy 1D array [x] and 2D array [x, 1]
[Blender x Python] How to use modifiers
[Blender x Python] Let's get started with Blender Python !!
[Blender x Python] How to make an animation
[Blender x Python] How to make vertex animation
[Blender x Python] Think of code with symbols