[Blender x Python] How to make vertex animation

table of contents

  1. Move one vertex about one axis
  2. Move multiple vertices about one axis
  3. Move multiple vertices with respect to multiple axes
  4. Shift the vertices little by little (Part 1)
  5. Shift the vertices little by little (Part 2)

1. Move one vertex about one axis

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

import bpy
from random import random

#Define vertices and faces that form a plane
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
faces = [(0,1,2,3)]

#Define a mesh
mesh = bpy.data.meshes.new("Plane_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

#Define an object from mesh data
obj = bpy.data.objects.new("Plane", mesh)   
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)

frame_num = 0
#Setting the end frame of the animation
bpy.context.scene.frame_end = 20

for i in range(0,10):
    #Select one vertex from the vertex list and decide on which axis of xyz to move
    mesh.vertices[0].co[2] += 3
    mesh.update(calc_edges=True)
    #Insert the movement of the selected vertex into a keyframe with the selected axis and the number of frames
    mesh.vertices[0].keyframe_insert('co',index = 2,frame = frame_num)
    
    frame_num += 5

Point:co[] Determines which axis to coordinate the vertices with. -1 → xyz axis 0 → x axis 1 → y-axis 2 → z axis

2. Move multiple vertices about one axis

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

import bpy
from random import random

bpy.ops.mesh.primitive_monkey_add()

obj = bpy.context.object
frame_num = 0

for f in range(0,100):
    for v in obj.data.vertices:
        m = random() * 0.02
        #When the number of frames is even
        if f%2:
            #Randomly move in the z-axis direction
            v.co.z += m
        #other than that(Odd number of frames)in the case of
        else:
            #Randomly move in the z-axis direction
            v.co.z -= m
        v.keyframe_insert('co',index = 2,frame = frame_num)
    
    frame_num += 1

3. Move multiple vertices with respect to multiple axes

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

import bpy
from random import random

#Define the vertices and faces that form the cube
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (4,5,6,7), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]

#Define a mesh
mesh = bpy.data.meshes.new("Cube_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

#Define an object from mesh data
obj = bpy.data.objects.new("Cube", mesh)   
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)


frame_num = 0

for f in range(0,100):
    for v in obj.data.vertices:
        m = random()
        if f%2:
            v.co.x += m
            v.co.y += m
        else:
            v.co.x -= m
            v.co.y -= m
        v.keyframe_insert('co',index = -1,frame = frame_num)
    
    frame_num += 3

4. Shift the vertices little by little (Part 1)

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

import bpy
import math

#Specify how many squares
p = 24

#Place the vertices starting from the center
verts = [(0,0,0)]
for i in range(0,p + 1):
    x = 2 *math.pi / p * i
    verts.append([math.cos(x),math.sin(x),0])

#A surface is formed by a total of 3 points, 2 points on the center and the circumference.
faces = []
for i in range(0,p):
    faces.append([0,i + 1,i + 2])

mesh = bpy.data.meshes.new("Polygon_mesh") 
mesh.from_pydata(verts, [], faces) 
obj = bpy.data.objects.new("Polygon", mesh) 
bpy.context.scene.collection.objects.link(obj)

#Put into edit mode
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')

#Remove duplicate vertices
bpy.ops.mesh.remove_doubles()

#Return to object mode
bpy.ops.object.mode_set(mode='OBJECT')

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


for f in range(0,600):
    #Move the vertices except the center up and down
    for i in range(1,p + 1):
        mesh.vertices[i].co[2] += 5
        mesh.update(calc_edges=True)
        mesh.vertices[i].keyframe_insert('co',index = 2,frame = frame_num)
        frame_num += 15
        
        mesh.vertices[i].co[2] -= 5
        mesh.update(calc_edges=True)
        mesh.vertices[i].keyframe_insert('co',index = 2,frame = frame_num)
        frame_num += 1

Shift the vertices little by little (Part 2)

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

import bpy
import math

verts = []
faces = []

#Variables related to mesh
numX = 10
numY = 10

#Generate vertex coordinates
for i in range (0, numX):
    for j in range(0,numY):

        x = scale * i
        y = scale * j
        z = 0

        vert = (x,y,z) 
        verts.append(vert)

#Generate a face from four vertices
count = 0
for i in range (0, numY *(numX-1)):
    if count < numY-1:
        A = i
        B = i+1
        C = (i+numY)+1
        D = (i+numY)

        face = (A,B,C,D)
        faces.append(face)
        count = count + 1
    else:
        count = 0

mesh = bpy.data.meshes.new("Wave_mesh")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

obj = bpy.data.objects.new("Wave",mesh)
obj.location = (0,0,0)
bpy.context.scene.collection.objects.link(obj)

#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
obj.modifiers['subd'].levels = 3

#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
    p.use_smooth = True
    
    

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


for f in range(0,600):
    for i in range(0,numX * numY):
        mesh.vertices[i].co[2] += 5
        mesh.update(calc_edges=True)
        mesh.vertices[i].keyframe_insert('co',index = 2,frame = frame_num)
        frame_num += 1
        
        mesh.vertices[i].co[2] -= 5
        mesh.update(calc_edges=True)
        mesh.vertices[i].keyframe_insert('co',index = 2,frame = frame_num)
        frame_num += 1

Recommended Posts

[Blender x Python] How to make vertex animation
[Blender x Python] How to make an animation
[Blender x Python] How to use modifiers
[Blender x Python] How to create an original object
[Blender x Python] Particle Animation (Part 1)
[Blender] How to make a Blender plugin
[Blender] How to make Blender scripts multilingual
[Python] How to make a class iterable
How to erase Python 2.x on Mac.
How to install Python
How to install python
[Algorithm x Python] How to use the list
[Blender x Python] Blender Python tips (11/100)
How to make Python faster for beginners [numpy]
How to make Python Interpreter changes in Pycharm
How to know the current directory in Python in Blender
Explain in detail how to make sounds with python
How to make a Python package using VS Code
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Convert python 3.x code to python 2.x
How to use Python bytes
[Python] How to make a list of character strings character by character
[Python] How to make an adjacency matrix / adjacency list [Graph theory]
[Circuit x Python] How to solve circuit equations symbolically using sympy
How to make a Python package (written for an intern)
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
[Introduction to Python] How to parse JSON
How to make a slack bot
How to make a crawler --Advanced
How to get the Python version
How to make a recursive function
How to get started with Python
[Python] How to import the library
How to use Mysql in python
How to use OpenPose's Python API
[Python] How to swap array values
How to wrap C in Python
How to use ChemSpider in Python
How to make a deadman's switch
How to use FTP with Python
How to use PubChem in Python