[Blender x Python] Let's master rotation !!

table of contents

  1. Let's rotate the cube
  2. Let's rotate the torus
  3. Sample code
  4. Summary of English words

0. Let's rotate the cube

0-0.1 Let's rotate one cube

◯ This is the simplest rotation method.

import bpy
#Contains various things necessary for calculation
import math

#Function to add a cube
bpy.ops.mesh.primitive_cube_add(
    scale=(1, 1, 1),
    #Add argument
    #math.pi is 180 degrees, so math.pi * 1/6 = 30(Every time)
    #Rotate 30 degrees around the X axis
    rotation = (math.pi*1/6,0,0)
    )

スクリーンショット 2020-11-12 13.28.52.png

0-1. Rotate the cubes and line them up

◯ We will shift them little by little.

import bpy
import math

#Iterate 100 times
for i in range(0,100):
    bpy.ops.mesh.primitive_cube_add(
        #Move up little by little
        location=(0, 0, i/50),
        scale=(1, 1, 0.05),
        #180 * i * 36(Every time)Shift one by one
        rotation = (0, 0, math.pi*i*10/360)
        )

スクリーンショット 2020-11-12 13.47.06.png


0-2. Let's add color

◯ It is an application of the above code.

import bpy
import math

#Create a variable called material yourself(material means material)
material = bpy.data.materials.new('Red')
#Define color(R,G,B,A)
material.diffuse_color = (1.0, 0.0, 0.0, 1.0)

 #Repeat 100 times
for i in range(0,100):
    bpy.ops.mesh.primitive_cube_add(
        #Move up little by little
        location=(0, 0, i/50),
        scale=(1, 1, 0.05),
        #Rotate little by little
        rotation = (0, 0, math.pi*i*10/360)
        )
    #Add your own defined color to the object(append means append)
    bpy.context.object.data.materials.append(material)

red.png


1. Let's rotate the torus

1-0. Let's transform the torus

import bpy

#Function to add a torus
bpy.ops.mesh.primitive_torus_add(
    location=(0, 0, 0),
    major_radius=1.0,
    minor_radius=0.01,
    rotation=(0, 0, 0)
    )
#Shrink in the Y-axis direction
bpy.ops.transform.resize(value=(1, 0.3, 1))

スクリーンショット 2020-11-12 15.05.14.png


1-0. Rotate the torus and line it up

◯ It is a method to rotate after making a shape.

import bpy
import math

for i in range(0,36):
    bpy.ops.mesh.primitive_torus_add(
        location=(0, 0, 0),
        major_radius=1.0,
        minor_radius=0.01,
        )
    #Shrink in the Y-axis direction
    bpy.ops.transform.resize(value=(1, 0.3, 1))
    #Rotate around the Z axis
    bpy.ops.transform.rotate(value=math.pi*i*10/360,orient_axis='Z')

スクリーンショット 2020-11-12 16.21.08.png

1-2. Let's color the torus

◯ After rotating around one axis, it is rotated around another axis.

import bpy
import math

#Material definition
material = bpy.data.materials.new('Red')
material.diffuse_color = (1.0, 0.0, 0.0, 1.0)

for i in range(0,36):
    for j in range(0,36):
        bpy.ops.mesh.primitive_torus_add(
            location=(0, 0, 0),
            major_radius=1.0,
            minor_radius=0.01,
            )
        #Shrink in the Y-axis direction
        bpy.ops.transform.resize(value=(1, 0.3, 1))
        #Rotate around the Z axis
        bpy.ops.transform.rotate(value=math.pi*j*10/360,orient_axis='Z')
        #Rotate around the Y axis
        bpy.ops.transform.rotate(value=math.pi*i*10/360,orient_axis='Y')
        #Use your own defined red
        bpy.context.object.data.materials.append(material)

スクリーンショット 2020-11-12 17.28.09.png

2. Sample code

Code to rotate the cube

import bpy
#Contains various things necessary for calculation
import math

#Function to add a cube
bpy.ops.mesh.primitive_cube_add(
    scale=(1, 1, 1),
    #Add argument
    #math.pi is 180 degrees, so math.pi * 1/6 = 30(Every time)
    #Rotate 30 degrees around the X axis
    rotation = (math.pi*1/6,0,0)
    )

A code that rotates and arranges cubes

import bpy
import math

#Iterate 100 times
for i in range(0,100):
    bpy.ops.mesh.primitive_cube_add(
        #Move up little by little
        location=(0, 0, i/50),
        scale=(1, 1, 0.05),
        #180 * i * 36(Every time)Shift one by one
        rotation = (0, 0, math.pi*i*10/360)
        )

Code to color the arranged cubes

import bpy
import math

#Create a variable called material yourself(material means material)
material = bpy.data.materials.new('Red')
#Define color(R,G,B,A)
material.diffuse_color = (1.0, 0.0, 0.0, 1.0)

 #Repeat 100 times
for i in range(0,100):
    bpy.ops.mesh.primitive_cube_add(
        #Move up little by little
        location=(0, 0, i/50),
        scale=(1, 1, 0.05),
        #Rotate little by little
        rotation = (0, 0, math.pi*i*10/360)
        )
    #Add your own defined color to the object(append means append)
    bpy.context.object.data.materials.append(material)

Code that transforms the torus

import bpy

#Function to add a torus
bpy.ops.mesh.primitive_torus_add(
    location=(0, 0, 0),
    major_radius=1.0,
    minor_radius=0.01,
    rotation=(0, 0, 0)
    )
#Shrink in the Y-axis direction
bpy.ops.transform.resize(value=(1, 0.3, 1))

A code that rotates and arranges the torus

import bpy
import math

for i in range(0,36):
    bpy.ops.mesh.primitive_torus_add(
        location=(0, 0, 0),
        major_radius=1.0,
        minor_radius=0.01,
        )
    #Shrink in the Y-axis direction
    bpy.ops.transform.resize(value=(1, 0.3, 1))
    #Rotate around the Z axis
    bpy.ops.transform.rotate(value=math.pi*i*10/360,orient_axis='Z')

A code that colors the torus and rotates it around two axes

import bpy
import math

#Material definition
material = bpy.data.materials.new('Red')
material.diffuse_color = (1.0, 0.0, 0.0, 1.0)

for i in range(0,36):
    for j in range(0,36):
        bpy.ops.mesh.primitive_torus_add(
            location=(0, 0, 0),
            major_radius=1.0,
            minor_radius=0.01,
            )
        #Shrink in the Y-axis direction
        bpy.ops.transform.resize(value=(1, 0.3, 1))
        #Rotate around the Z axis
        bpy.ops.transform.rotate(value=math.pi*j*10/360,orient_axis='Z')
        #Rotate around the Y axis
        bpy.ops.transform.rotate(value=math.pi*i*10/360,orient_axis='Y')
        #Use your own defined red
        bpy.context.object.data.materials.append(material)

3. Summary of English words

English words Japanese translation
import to import/to obtain
math Math
mesh A set of vertices, edges, and faces
scale size
primitive Primitive
add to add
rotation rotation
pi π( =180 degrees)
range width
cube cube
material Material
diffuse Diffusion, scattering
context Context, environment
object Things, objects
data information
append Add, add
major big
minor small
radius radius
transform conversion
resize Resize
axis axis
orient direction
rotate Rotate
rotation rotation
value value
location Position, coordinates
operation Operation, operation

Recommended Posts

[Blender x Python] Let's master rotation !!
[Blender x Python] Let's master random !!
[Blender x Python] Let's master the material !!
[Blender x Python] Let's get started with Blender Python !!
[Blender x Python] Blender Python tips (11/100)
[Python] Let's master all and any
[Blender x Python] Particle Animation (Part 1)
[Blender x Python] Let's arrange a lot of Susanne neatly !!
[Blender x Python] How to use modifiers
Blender 2.9 Python Extrude extrude
[Blender x Python] How to make an animation
[Blender x Python] How to make vertex animation
[Blender x Python] Think of code with symbols
[Blender x Python] How to create an original object
[Blender x Python] 3D Bouncing Ball and other animations
Python string manipulation master
Run Blender with python
blender, python, sphere behavior
Blender 2.8, Python Cube Scaling
Operate Blender with Python
Aim python library master (48) autopep8
Aim python library master (36) json2html
Let's use def in python
Let's use python janome easily
Aim python library master (26) easyxml
x86 compiler self-made with python
Aim python library master (29) table_printer
Aim python library master (55) namespaces
Aim python library master (30) chronyk
Aim python library master (3) workalendar
Aim python library master (42) speedrecorder
Aim python library master (37) slimurl
Aim python library master (8) rolex
Aim python library master (52) marktime
Aim python library master (7) numparser
Aim python library master (21) hy
Aim python library master (18) requests
Blender 2.9, Python Buildings, Cars, Videos
Aim python library master (13) easydev
Aim python library master (20) pyyaml
Aim python library master (34) concurrent
Aim python library master (40) wordsegmentation
blender, python, spiral staircase, color
Aim python library master (68) pazudorasolver
Aim python library master (58) faker
Aim python library master (11) nlist
Aim python library master (38) beautiful_print
Aim python library master (65) geopy
Aim python library master (2) vincenty
Aim python library master (59) logbook
Aim python library master (51) pyautogui
Put Python 3.x on Ubuntu
Aim python library master (10) timeit
Aim python library master (0) Links
Run Tensorflow 2.x on Python 3.7
Aim python library master (66) youtube-dl
Aim python library master (53) psutil
Aim python library master (22) htmltag
Aim python library master (67) httpie
2.x, 3.x character code of python
Blender 2.9, Python Odd Even Building