[Blender x Python] How to make an animation

table of contents

  1. Animation operations
  2. Move the object to the specified coordinates
  3. Move the object to random coordinates
  4. Use trigonometric functions to move objects
  5. Change the size of the object
  6. Change the position and size of the object at the same time Finally

0. Animation operations

0-1. Creating animation

Open Scripting ↓ Name it like ~ .py ↓ Write code and press execute button

スクリーンショット 2021-01-05 10.14.36.png

0-2. Execution of animation

Go to animation and press the play button

0-3. Delete animation

In 3Dviewport, press N ↓ Right-click at the key insertion point (green) ↓ Press clear key frame ↓ Press the N key to close the tab

スクリーンショット 2021-01-05 10.23.44.png

0-4. Creating a new script

Press the Text button ↓ Press the New button ↓ Name it like ~ .py

0-5. Select a script

Press the button with a pattern like a document ↓ Select a script

スクリーンショット 2021-01-05 11.06.26.png

1. Move the object to the specified coordinates

ezgif.com-gif-maker.gif

anim01.py


import bpy

#Specify the coordinates of the target
positions = (0,3,2),(4,1,6),(3,-5,1),(3,10,1),(1,8,1)
#Specify the starting coordinates
start_pos = (0,0,0)

#Assign the object in the 3D viewport to the variable ob
#ob = bpy.data.objects["Cube"]It is also possible to move a specific thing like
ob = bpy.context.object

#Specify the start frame
frame_num = 0

#Repeat the process of moving to the target coordinates
for p in positions:
    #Set the start frame
    bpy.context.scene.frame_set(frame_num)
    #Substitute the target coordinate value for the variable location
    ob.location = p
    #Insert keyframe
    ob.keyframe_insert(data_path = "location",index = -1)
    #Shift the frame by 20
    frame_num += 20

○Point : keyframe_insert A function that inserts keyframes. For the argument, use the information to be used (data_path), array format (index), etc. index is

-1 → x, y, z all 0 → x 1 → y 2 → z

It is also possible to operate with reference to a specific axis.

2. Move the object to random coordinates

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

anim02.py


import bpy
#Use the randint function in a random library
from random import randint

#Use objects in 3D viewport(This time Sphere)
ob = bpy.data.objects["Sphere"]
frame_number = 0

#Repeat the process 50 times
for i in range(0,50):
    
    #Randomly select an integer from the range from the first argument to the second argument
    x = randint(-20,20)
    y = randint(-40,20)
    z = randint(-20,60)
    
    bpy.context.scene.frame_set(frame_number)
    ob.location = (x,y,z)
    ob.keyframe_insert(data_path = "location",index = -1)
    frame_number += 5

3. Use trigonometric functions to move objects

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

anim03.py


import bpy
#Prepare the sin function of the math library
from math import sin

start_pos = (0,0,0)
ob = bpy.context.object
frame_num = 0

for i in range(0,50):
    bpy.context.scene.frame_set(frame_num)
    #Specify the coordinates in the variable location
    ob.location = (0,i,sin(i))
    ob.keyframe_insert(data_path = "location",index = -1)
    frame_num += 10

4. Change the size of the object

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

anim04.py


import bpy

scales = (1,3,2),(4,1,6),(3,5,1),(3,10,1),(1,8,1)
start_pos = (0,0,0)
ob = bpy.context.object
frame_num = 0

for s in scales:
    bpy.context.scene.frame_set(frame_num)
    ob.scale = s
    #Insert the key based on the size information
    ob.keyframe_insert(data_path = "scale",index = -1)
    frame_num += 20

5. Change the position and size of the object at the same time

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

anim05.py


import bpy
from math import sin

start_pos = (0,0,0)
ob = bpy.context.object
frame_num = 0

for i in range(0,100):
    bpy.context.scene.frame_set(frame_num)
    ob.location = (0,-i,sin(i) * 3)
    ob.scale = (1,1,1 + sin(i))
    ob.keyframe_insert(data_path = "location",index = -1)
    ob.keyframe_insert(data_path = "scale",index = -1)
    frame_num += 5

Finally

Thank you for reading. Please feel free to contact us if you have any corrections!

Recommended Posts

[Blender x Python] How to make an animation
[Blender x Python] How to make vertex animation
[Blender x Python] How to create an original object
[Blender x Python] How to use modifiers
[Blender] How to make a Blender plugin
[Blender] How to make Blender scripts multilingual
[Python] How to make an adjacency matrix / adjacency list [Graph theory]
How to make a Python package (written for an intern)
[Python] How to make a class iterable
How to convert Python to an exe file
How to make an embedded Linux device driver (11)
How to make an embedded Linux device driver (8)
How to make an embedded Linux device driver (1)
How to make an embedded Linux device driver (4)
How to make an embedded Linux device driver (7)
How to make an embedded Linux device driver (2)
How to crop an image with Python + OpenCV
How to install python
How to make an embedded Linux device driver (3)
[Algorithm x Python] How to use the list
How to make an embedded Linux device driver (6)
How to make Substance Painter Python plugin (Introduction)
How to make an embedded Linux device driver (5)
[Blender x Python] Blender Python tips (11/100)
How to make an embedded Linux device driver (10)
How to make Python faster for beginners [numpy]
How to make Python Interpreter changes in Pycharm
How to make an embedded Linux device driver (9)
Python + chatwork + google extension = How to make an easy and funny chat BOT
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
How to make an HTTPS server with Go / Gin
How to make an embedded Linux device driver (12) (Complete)
[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
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
An introduction to Python Programming
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
How to convert an array to a dictionary with Python [Application]
How to swap elements in an array in Python, and how to reverse an array.
[Python] How to write an if statement in one sentence.
How to run an app built with Python + py2app built with Anaconda
[Python Kivy] How to create an exe file with pyinstaller
[Python] How to output a pandas table to an excel file
How to read an Excel file (.xlsx) with Pandas [Python]