This is the code to make one of Sae Yamamoto introduced in the previous article appear. This time we will apply this.
import bpy
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
location=(0, 0, 0),
scale=(1, 1, 1)
)
It may seem long at first glance, but it was a story that you can think simply by ** abstracting, compressing and symbolizing information ** (↓)
===>primitive_monkey_add(□,□,□,□,□)
Code meaning: Sae Yamamoto appears. At that time, you can change the position and size.
Iterates using a mechanism called ◯ ** for loop **. The key to understanding iterative processing is
** Number of processes ** When ** Numerical value that changes with processing **
** Think separately **.
I'm not sure right now, but it's okay !!
◯ The point is that the numerical value that changes with processing increases by one.
import bpy
#i is 0 → 1 → 2 → 3 → 4
for i in range(0,5):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, 0, 0),
scale=(1, 1, 1)
)
When the above code is symbolized (abstracted),
i → 0,1,2,3,4 : ===>primitive_monkey_add(□,□,□,□,□)
Call like.
Meaning: Each time i repeats the process, it increases by ** 1. If i satisfies ** 0 <= i <5 **, the process of making Sae Yamamoto appear is performed. Then, substitute the value of i into the value of the x coordinate.
The number of processing is 5 times The numerical value that changes with processing is 0 → 1 → 2 → 3 → 4.
When you do this, it will look like the picture below. Somehow cute .. ??
◯ Point: ** range (from where to where) ** It is used to determine the range of iteration.
◯ Point is the part where the x coordinate of Sae Yamamoto is written as ** i * 3 **. The * symbol is a multiplication symbol.
import bpy
#i is 0 → 1 → 2 → 3 → 4
for i in range(0,5):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i * 3, 0, 0),
scale=(1, 1, 1)
)
The x coordinate of Sae Yamamoto is i * 3. In other words First time of processing (i = 0) → ** i * 3 = 0 ** Second time of processing (i = 1) → ** i * 3 = 3 ** Third time of processing (i = 2) → ** i * 3 = 6 ** 4th processing (i = 3) → ** i * 3 = 9 ** 5th process (i = 4) → ** i * 3 = 12 **
◯ The point is in range ().
import bpy
#i is 0 → 3 → 6 → 9 → 12
for i in range(0,13,3):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, 0, 0),
scale=(1, 1, 1)
)
range () can be range (first value, last value, step)
You can decide how many numbers you want to skip ** in the range from the first number to the last number.
In other words, for i in range (0,13,3):
means that every three numbers from 0 are selected and assigned to i, which is repeated within the range of i <13.
⬇️
0 1 2 3 4 5 6 7 8 9 10 11 12
First time of processing → ** i = 0 ** Second time of processing → ** i = 3 ** Third time of processing → ** i = 6 ** 4th processing → ** i = 9 ** 5th process → ** i = 12 **
◯ The point is that ** another for loop is inside the for loop **.
import bpy
#i is 0 → 3 → 6 → 9 → 12
for i in range(0,13,3):
for j in range(0,13,3):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, j, 0),
scale=(1, 1, 1)
)
When symbolized (abstracted),
i = 0→3→6→9→12:
j = 0→3→6→9→12:
===>primitive_monkey_add(□,□,□,□,□)
Call like.
this is,
( i , j ) = (0,0),(0,3),(0,6),(0,9),(0,12),
(3,0),(3,3),(3,6),(3,9),(3,12),
(6,0),(6,3),(6,6),(9,9),(12,12),
(9,0),(9,3),(9,6),(9,9),(9,12),
(12,0),(12,3),(12,6),(12,9),(12,12)
about it.
◯ It is the application mentioned earlier.
import bpy
for i in range(0,13,3):
for j in range(0,13,3):
for k in range(0,13,3):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, j, k),
scale=(1, 1, 1)
)
◯ Only the sample code is summarized.
import bpy
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
location=(0, 0, 0),
scale=(1, 1, 1)
)
import bpy
#i is 0 → 1 → 2 → 3 → 4
for i in range(0,5):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, 0, 0),
scale=(1, 1, 1)
)
import bpy
#i is 0 → 1 → 2 → 3 → 4
for i in range(0,5):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i * 3, 0, 0),
scale=(1, 1, 1)
)
import bpy
#i is 0 → 3 → 6 → 9 → 12
for i in range(0,13,3):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, 0, 0),
scale=(1, 1, 1)
)
import bpy
#i is 0 → 3 → 6 → 9 → 12
for i in range(0,13,3):
for j in range(0,13,3):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, j, 0),
scale=(1, 1, 1)
)
import bpy
for i in range(0,13,3):
for j in range(0,13,3):
for k in range(0,13,3):
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(i, j, k),
scale=(1, 1, 1)
)
import bpy
import math
#Assign a number to a variable
#To make it easy to change the numbers
n = 12
r = 10.0
for i in range(0, n):
rad = 2 * math.pi * i /n #Angle calculation 2π i/n
x = r * math.cos(rad) #x coordinate calculation radius*cosθ
y = r * math.sin(rad) #y coordinate calculation radius*sinθ
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(x, y, 0),
scale=(1, 1, 1)
)
import bpy
import math
n = 144
r = 10.0
for i in range(0, n):
rad = 2 * math.pi * i /24 #Angle calculation 2π i/24
x = (r * i)/10 * math.cos(rad) #x coordinate calculation radius*cosθ
y = (r * i)/10 * math.sin(rad) #y coordinate calculation radius*sinθ
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(x, y, 0),
scale=(1, 1, 1)
)
import bpy
import math
n = 144
r = 10.0
for i in range(0, n):
rad = 2 * math.pi * i /12 #Angle calculation 2π i/12
x = r * math.cos(rad) #x coordinate calculation radius*cosθ
y = r * math.sin(rad) #y coordinate calculation radius*sinθ
bpy.ops.mesh.primitive_monkey_add(
size=2,
enter_editmode=False,
align='WORLD',
#Attention ↓
location=(x, y, i),
scale=(1, 1, 1)
)
Recommended Posts