[Blender×Python]   パーティクルアニメーション(Part1)

#目次

0.直線を描く
1.螺旋を描く(Part1)
2.螺旋を描く(Part2)
3.トルネードを描く
4.インスタンスの変更
5.ランダムウォーク


#0.直線を描く
![ezgif.com-gif-maker (17).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/219a2819-62af-b56e-1ab1-7fbf698a500f.gif)


```.py
import bpy
import math as m

#最終フレームを決定する
end_frame = 100

#パーティクルに関する関数をつくる
def particle_road(p_count = 50000,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0):   
    #パーティクルシステムを追加する
    bpy.ops.object.particle_system_add()

    #各種設定を行う
    p_s = bpy.context.active_object.particle_systems[0].settings
    #パーティクルの総数
    p_s.count = p_count
    #パーティクルの終了フレーム
    p_s.frame_end = p_end
    #パーティクルの継続時間
    p_s.lifetime = p_lifetime
    #パーティクルの速度
    p_s.normal_factor = p_normal_factor
    #パーティクルにかかる重力
    p_s.effector_weights.gravity = p_gravity
    

#エミッターを追加する
bpy.ops.mesh.primitive_cube_add()
obj = bpy.data.objects["Cube"]

frame_num = 0
bpy.context.scene.frame_end = end_frame
x = 0
y = 0

for i in range(0,end_frame):
    bpy.context.scene.frame_set(frame_num)
    x += 1
    y += 1
    obj.location = (x,y,0)
    obj.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1

#定義した関数を使う
particle_road()
```


#0.螺旋を描く

![ezgif.com-gif-maker (18).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/e8ab90dc-1350-3db1-b698-765264f1cd6e.gif)


```.py
import bpy
import math as m

end_frame = 300

def particle_road(p_count = 50000,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0): 

    bpy.ops.object.particle_system_add()

    p_s = bpy.context.active_object.particle_systems[0].settings
    p_s.count = p_count
    p_s.frame_end = p_end
    p_s.lifetime = p_lifetime
    p_s.normal_factor = p_normal_factor
    p_s.effector_weights.gravity = p_gravity
    

bpy.ops.mesh.primitive_cube_add()
obj = bpy.data.objects["Cube"]

frame_num = 0
bpy.context.scene.frame_end = end_frame
#螺旋の半径
r = 30
#円周を何等分するか
detail = 24

for i in range(0,end_frame):
    rad = 2 * m.pi * i/detail
    x = m.cos(rad) * r
    y = m.sin(rad) * r
    z = i/2
    
    bpy.context.scene.frame_set(frame_num)
    obj.location = (x,y,z)
    obj.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1
    
particle_road()
```



#2螺旋(Part2)

![ezgif.com-gif-maker (19).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/2186e1f4-023b-afb1-9d8f-65d7a5f7742b.gif)


```.py
import bpy
import math as m

end_frame = 700

def particle_road(p_count = 300000,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0):   
    
    bpy.ops.object.particle_system_add()

    p_s = bpy.context.active_object.particle_systems[0].settings
    p_s.count = p_count
    p_s.frame_end = p_end
    p_s.lifetime = p_lifetime
    p_s.normal_factor = p_normal_factor
    p_s.effector_weights.gravity = p_gravity
    

bpy.ops.mesh.primitive_uv_sphere_add()
obj = bpy.data.objects["Sphere"]

frame_num = 0
bpy.context.scene.frame_end = end_frame
#螺旋の半径
r = 1
z = 0
z_speed = 3
detail = 24

for i in range(0,end_frame):
    rad = 2 * m.pi * i/detail
    x = m.cos(rad) * r
    y = m.sin(rad) * r
    z += z_speed
    
    if(z > 100 or z < 0):
        #zの向きを反転する
        z_speed *= -1
        #半径を広げていく
        r += 10
    
    bpy.context.scene.frame_set(frame_num)
    obj.location = (x,y,z)
    obj.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1
    
particle_road()
```


#3.トルネードを描く

![ezgif.com-gif-maker (20).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/e52d0a5c-dc47-a382-4005-89b77a25a4d0.gif)


```.py
import bpy
import math as m

end_frame = 700

def particle_road(p_count = 50000,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0):   
    bpy.ops.object.particle_system_add()

    p_s = bpy.context.active_object.particle_systems[0].settings

    p_s.count = p_count
    p_s.frame_end = p_end
    p_s.lifetime = p_lifetime
    p_s.normal_factor = p_normal_factor
    p_s.effector_weights.gravity = p_gravity
    

bpy.ops.mesh.primitive_uv_sphere_add()
obj = bpy.data.objects["Sphere"]

frame_num = 0
bpy.context.scene.frame_end = end_frame 
r = 10
z = 0
z_speed = 3
detail = 24

for i in range(0,end_frame):
    rad = 2 * m.pi * i/detail
    x = (r * i)/30 *  m.cos(rad)
    y = (r * i)/30 *  m.sin(rad)
    z = i
    
    bpy.context.scene.frame_set(frame_num)
    obj.location = (x,y,z)
    obj.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1
    
particle_road()

```



#4.インスタンスを変更する


![ezgif.com-gif-maker (22).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/564ac058-087e-ada4-7d71-5d1c8fc9e300.gif)


```.py
import bpy
import math as m

end_frame = 500
bpy.ops.mesh.primitive_cube_add()


def particle_road(p_count = 500,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0):   
    
    bpy.ops.object.particle_system_add()
    p_s = bpy.context.active_object.particle_systems[0].settings

    p_s.count = p_count
    p_s.frame_end = p_end
    p_s.lifetime = p_lifetime
    p_s.normal_factor = p_normal_factor
    p_s.effector_weights.gravity = p_gravity
    #オブジェクトをインスタンスにする
    p_s.render_type = 'OBJECT'
    #Cubeをインスタンスにする
    p_s.instance_object = bpy.data.objects["Cube"]
    #大きさを決定する
    p_s.particle_size = 10
    #大きさにランダムをどれほど適応するか
    p_s.size_random = 1

bpy.ops.mesh.primitive_uv_sphere_add()
obj = bpy.data.objects["Sphere"]

frame_num = 0
bpy.context.scene.frame_end = end_frame
r = 70
detail = 24

for i in range(0,end_frame):
    rad = 2 * m.pi * i/detail
    x = m.cos(rad) * r
    y = m.sin(rad) * r
    z = i * 3
    
    bpy.context.scene.frame_set(frame_num)
    obj.location = (x,y,z)
    obj.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1
    
particle_road()
```



#5.ランダムウォーカー
![ezgif.com-gif-maker (23).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/630bae65-c603-06c3-b2d0-595f86eecde1.gif)


```.py
import bpy
from random import randint

end_frame = 5000

def particle_road(p_count = 500000,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0):   
    bpy.ops.object.particle_system_add()

    p_s = bpy.context.active_object.particle_systems[0].settings

    p_s.count = p_count
    p_s.frame_end = p_end
    p_s.lifetime = p_lifetime
    p_s.normal_factor = p_normal_factor
    p_s.effector_weights.gravity = p_gravity
    

bpy.ops.mesh.primitive_cube_add()
obj = bpy.data.objects["Cube"]

frame_num = 0
bpy.context.scene.frame_end = end_frame

x = 0
y = 0
z = 0

for i in range(0,end_frame):
    
    #第一引数から第二引数までの範囲からランダムに整数を選択する
    m = randint(-20,20)
    
    #フレーム数を3で割ったあまりが0ならば
    if(i%3 == 0):
        x += m
    #フレーム数を3で割ったあまりが1ならば
    elif(i%3 == 1):
        y += m
    #それ以外ならば
    else:
        z += m
    
    bpy.context.scene.frame_set(frame_num)
    obj.location = (x,y,z)
    obj.keyframe_insert(data_path = "location",index = -1)
    frame_num += 1
    
particle_road()
```