[Blender x Python] Let's arrange a lot of Susanne neatly !!

table of contents

  1. Make one Sae Yamamoto appear
  2. Arrange Susanne in a row
  3. Arrange Susanne in two dimensions
  4. Arrange Susanne in three dimensions
  5. Sample code

0. Make one Sae Yamamoto appear

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.


1. Arrange Susanne in a row

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 !!

1-0.x Coordinates are staggered by 1

◯ 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 .. ??

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


◯ Point: ** range (from where to where) ** It is used to determine the range of iteration.


1-1.x Coordinates are shifted by 3 (Part 1)

◯ 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)
    )

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

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 **

1-2. Place the x coordinates by 3 (Part 2)

◯ 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)
    )

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

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 **


2. Arrange Susanne in two dimensions

◯ 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)
        )

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

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.


3. Arrange Susanne in three dimensions

◯ 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)
            )

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

4. Sample code

◯ Only the sample code is summarized.

4-0. Code that makes only one Sae Yamamoto appear

import bpy

bpy.ops.mesh.primitive_monkey_add(
      size=2,
      enter_editmode=False,
      align='WORLD',
      location=(0, 0, 0),
      scale=(1, 1, 1)
      )

4-1. Code to arrange Sae Yamamoto in a row (shift by 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)
    )

4-2. Code for arranging Sae Yamamoto in a row (shift by 3: Part 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)
    )

4-3. Code for arranging Sae Yamamoto in a row (shift by 3: Part 2)

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)
    )

4-4. Code for arranging Susanne in two dimensions

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)
        )

4-5. Code for arranging Susanne in three dimensions

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)
            )

4-6. Codes arranged in a circle

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)
            )

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

4-7. Cords arranged in a spiral

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)
            )

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

4-8. Code to make a spiral

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)
            )

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

Recommended Posts

[Blender x Python] Let's arrange a lot of Susanne neatly !!
[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] Let's get started with Blender Python !!
[Blender x Python] Think of code with symbols
[Blender x Python] Blender Python tips (11/100)
Python that merges a lot of excel into one excel
Python + selenium to GW a lot of e-mail addresses
I want to start a lot of processes from python
2.x, 3.x character code of python
Executing a large number of Python3 Executor.submit may consume a lot of memory
I made a lot of files for RDP connection with Python
[Blender Python] Arrange custom property data in template_list () of UI layout
I did a lot of research on how Python is executed
A record of patching a python package
Basics of Python x GIS (Part 3)
A good description of Python decorators
Let's make a GUI with python.
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
[Blender x Python] Particle Animation (Part 1)
Basics of Python x GIS (Part 2)
Let's make a graph with python! !!
Wow Pandas Let's learn a lot
Python: I want to measure the processing time of a function neatly
Let's build a belief propagation method (Python)
Let's make a shiritori game with Python
Make a relation diagram of Python module
Let's create a virtual environment for Python
Let's create a free group with Python
Let's make a voice slowly with Python
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Let's make a web framework with Python! (1)
Let's make a combination calculation in Python
The story of blackjack A processing (python)
Let's make a Twitter Bot with Python!
[Python] Get a list of folders only
[Blender x Python] How to use modifiers
Let's make a web framework with Python! (2)
A memorandum of python string deletion process
[Circuit x Python] How to find the transfer function of a circuit using Lcapy