4
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] パーティクルアニメーション(Part1)

Last updated at Posted at 2021-01-11

#目次

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

#0.直線を描く
ezgif.com-gif-maker (17).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

.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

.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

.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

.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

.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()
4
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
4
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?