[Blender x Python] Particle Animation (Part 1)

table of contents

  1. Draw a straight line
  2. Draw a spiral (Part 1)
  3. Draw a spiral (Part 2)
  4. Draw a tornado
  5. Change instance
  6. Random walk

0. Draw a straight line

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

import bpy
import math as m

#Determine the final frame
end_frame = 100

#Create a function for particles
def particle_road(p_count = 50000,p_end = end_frame,p_lifetime = end_frame,p_normal_factor = 0,p_gravity = 0):   
    #Add a particle system
    bpy.ops.object.particle_system_add()

    #Make various settings
    p_s = bpy.context.active_object.particle_systems[0].settings
    #Total number of particles
    p_s.count = p_count
    #Particle end frame
    p_s.frame_end = p_end
    #Particle duration
    p_s.lifetime = p_lifetime
    #Particle velocity
    p_s.normal_factor = p_normal_factor
    #Gravity on particles
    p_s.effector_weights.gravity = p_gravity
    

#Add an emitter
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

#Use the defined function
particle_road()

0. Draw a spiral

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

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
#Radius of spiral
r = 30
#How to divide the circumference into equal parts
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 spiral (Part 2)

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

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
#Radius of spiral
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):
        #Reverse the direction of z
        z_speed *= -1
        #Expand the radius
        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. Draw a tornado

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

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. Change the instance

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

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
    #Instantiate an object
    p_s.render_type = 'OBJECT'
    #Instantiate Cube
    p_s.instance_object = bpy.data.objects["Cube"]
    #Determine the size
    p_s.particle_size = 10
    #How to adapt random to size
    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. Random walker

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

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):
    
    #Randomly select an integer from the range from the first argument to the second argument
    m = randint(-20,20)
    
    #If the number of frames divided by 3 is 0
    if(i%3 == 0):
        x += m
    #If the number of frames divided by 3 is 1
    elif(i%3 == 1):
        y += m
    #Otherwise
    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()

Recommended Posts

[Blender x Python] Particle Animation (Part 1)
[Blender x Python] Blender Python tips (11/100)
[Blender x Python] How to make an animation
[Blender x Python] How to make vertex animation
Basics of Python x GIS (Part 3)
Basics of Python x GIS (Part 2)
[Blender x Python] Let's master random !!
[Blender x Python] Let's master rotation !!
[Blender x Python] Let's master the material !!
[Blender x Python] How to use modifiers
[Blender x Python] Let's get started with Blender Python !!
QGIS + Python Part 2
QGIS + Python Part 1
Blender 2.9 Python Extrude extrude
Python: Scraping Part 1
Python3 Beginning Part 1
Python: Scraping Part 2
EV3 x Python Machine Learning Part 2 Linear Regression
[Blender x Python] Think of code with symbols
[Blender x Python] How to create an original object
blender, python, spiral staircase
Run Blender with python
blender, python, sphere behavior
Python basic memorandum part 2
Python basic memo --Part 2
Blender 2.8, Python Cube Scaling
Operate Blender with Python
Python basic memo --Part 1
[Blender x Python] Let's arrange a lot of Susanne neatly !!
Image processing with Python (Part 2)
Studying Python with freeCodeCamp part1
Register DynamoDB x Python / Decimal
Bordering images with python Part 1
Python application: Pandas Part 2: Series
Python: Ship Survival Prediction Part 2
Blender 2.9, Python Buildings, Cars, Videos
Python: Supervised Learning: Hyperparameters Part 1
blender, python, spiral staircase, color
Python Basic Grammar Memo (Part 1)
Python: Ship Survival Prediction Part 1
Put Python 3.x on Ubuntu
Studying Python with freeCodeCamp part2
Run Tensorflow 2.x on Python 3.7
Image processing with Python (Part 1)
2.x, 3.x character code of python
Blender 2.9, Python Odd Even Building
Solving Sudoku with Python (Part 2)
Image processing with Python (Part 3)
Python: Ship Survival Prediction Part 3
Python: Stock Price Forecast Part 2
UI Automation Part 2 in Python
Blender 2.9, Python, hexadecimal color specification
Python implementation of particle filters
Python: Supervised Learning: Hyperparameters Part 2
Blender 2.8, Python, light Spot lighting
Scraping with Selenium + Python Part 2
Blender Python API in Houdini (Python 3)
Generate 8 * 8 (64) cubes in Blender Python
Basics of Python × GIS (Part 1)
Convert python 3.x code to python 2.x
Python: Stock Price Forecast Part 1