Operate Blender with Python

1. Introduction

Here are the steps to do some simple modeling in Python on blender. The environment is blender version 2.8.5 and OS is Windows 10. Please note that there was a significant change in the specifications from 2.7 to 2.8. You can think that 2.7 and 2.8 are almost incompatible.

2. Japanese setting

If you need to use Japanese, make the following settings. There is nothing particularly difficult. User Preferences → Interface tab Check Translation and set Language to Japanese Turn on all three of Tooltips, Interface and New Data and click Save User Settings. In the current version, Japanese cannot be input directly on the editor. Copy and paste the characters you type in another text editor when you need them.

3. How to use Python on blender

If you run Python code on blender and get an error, you need to display the console screen. To display the console screen

For Windows

Click Toggle System Console from the Window menu at the top to display the console screen.

For Mac

Start blender Instead of double-clicking the icon, enter the following command from the terminal to start blender. /Applications/blender.app/Contents/MacOS/blender (The location of the app folder (/Applications/blender.app) changes depending on the environment)

3. Basic composition of elements

blender places elements called primitives.

models.py


import bpy  #blender import

#1.Cylinder location:Center coordinates of the shape radius:Radius depth of circle:Height rotation: Rotation angle of solid(rad)
bpy.ops.mesh.primitive_cylinder_add(location=(-5, 5, 0), radius=1, depth=3, rotation=(0, 0, 0))

#2.Cube location:Center coordinates of figure size:The length of one side of the cube rotation: The rotation angle of the solid(rad)
bpy.ops.mesh.primitive_cube_add(location=(5, 0, 0), size=1.5, rotation=(0, 0, 0))

#3.Sphere location:Center coordinates of the shape radius:Sphere radius subdivisions: number of divisions
bpy.ops.mesh.primitive_ico_sphere_add(location=(-5, 0, 0), radius=1, subdivisions=5)

#4.monkey location:Center coordinates of figure size:size
bpy.ops.mesh.primitive_monkey_add(location = (-5, -5, 0), size = 3.0)

#5.Donut-shaped location:Center coordinates of figure major_radius:Ring radius minor_radius:Cylinder radius rotation:Rotation angle of solid(rad)
bpy.ops.mesh.primitive_torus_add(location=(0, 5, 0), major_radius=1.0, minor_radius=0.1, rotation=(0, 0, 0))

#6.Flat plate(Circle) location:Center coordinates of figure fill_type:Fill radius:Radius rotation:Rotation angle of solid(rad)
bpy.ops.mesh.primitive_circle_add(location=(5, 5, 0), fill_type="NGON", radius=2, rotation=(0, 0, 0))

#7.Flat plate(square) location:Center coordinates of figure size:Side length rotation:Rotation angle of solid(rad)
bpy.ops.mesh.primitive_plane_add(location=(5, -5, 0), rotation=(0, 0, 0), size=2)

#8.Polygonal pyramid location:Center coordinates of the shape vertices:Number of vertices radius1,radius2:Radius depth of circle:Height rotation: Rotation angle of solid(rad)
bpy.ops.mesh.primitive_cone_add(location=(0,-5,0),vertices=10,radius1=0.5,radius2=1,depth=3, rotation=(0, 0, 0))

sample4.png

4. Various modeling

4.1 Sphere drawing by loop

loop.py


import bpy
import math

#Delete existing element
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)

N = 12
RR1 = 10.0
RR2 = 2.0
for i in range(0, N):
    rad = 2 * math.pi * i /N  #Angle calculation 2π i/n
    xx = RR1 * math.cos(rad) #x coordinate calculation radius*cosθ
    yy = RR1 * math.sin(rad) #y coordinate calculation radius*sinθ
    #Sphere creation
    bpy.ops.mesh.primitive_ico_sphere_add(location=(xx, yy, 0),radius= RR2, subdivisions = 5 )

sample2.png

4.2 Primitive transformation / rotation / movement

rectangle.py



import bpy

#Delete existing element
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)
#Draw a cube
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0), size=1.5, rotation=(0, 0, 0))
#Transform shapes(2 times in the X direction, 1 time in the Y direction, 0 in the thickness direction.5 times)
bpy.ops.transform.resize(value=(2.0,1.0,0.1))
#Rotate shapes(30 ° around the Y axis)
bpy.ops.transform.rotate(value=3.1415/6 ,orient_axis='Y')
#Move shapes(Move 5 in the Z-axis direction)
bpy.ops.transform.translate(value=(0,0,5))

sample5.png

4.3 Material setting

material.py


import bpy

# 1.Delete existing element
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)

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

# 3.Material definition(Blue)
mat2 = bpy.data.materials.new('blue')
mat2.diffuse_color = (0.0, 0.0, 1.0, 1.0)

# 4.Sphere creation
bpy.ops.mesh.primitive_ico_sphere_add(location=(0, 0, 1), radius = 0.5, subdivisions=5 )
bpy.context.object.data.materials.append(mat1) #Material(Red)Designation

# 5.Flat plate creation
bpy.ops.mesh.primitive_cube_add(location=(0, -0.5, 0), size=2.5)
bpy.ops.transform.resize(value=(2.0,2.0,0.05)) #Transform shapes(Double in the X direction, double in the Y direction, 0 in the thickness direction.05 times)
bpy.context.object.data.materials.append(mat2) #Material(Blue)Designation

sample6.png

4.4 Creating a mesh 1

mesh1.py


import bpy

#Delete existing element
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)

#Creating vertex data
verts = [[-2.0, -2.0, 0.0], [-2.0, 2.0,  0.0], [2.0, 2.0,  0.0] , [2.0, -2.0,  0.0], [2.0, -2.0, 4.0] , [2.0, 2.0, 4.0]  ]
#Creation of surface data
faces = [[0,1,2,3], [2,3,4,5]]

msh = bpy.data.meshes.new("cubemesh") #Declaration of Mesh data
msh.from_pydata(verts, [], faces) #Create a mesh with vertex coordinates and vertex information for each face
obj = bpy.data.objects.new("cube", msh) #Create an object with mesh data
bpy.context.scene.collection.objects.link(obj) #Place objects in the scene

sample7.png

4.5 Creating a mesh 2

mesh2.py


import bpy
import math

#Delete existing element
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)

#Creating vertices
verts = []
for i in range(0,21):
    x = 2 *math.pi / 20 * i
    verts.append([x, -1, math.sin(x)])

for i in range(0,21):
    x = 2 * math.pi / 20 * i
    verts.append([x,  1, math.sin(x)])

#Creation of surface data
faces = []
for i in range(0,20):
    faces.append([i, i+1, i+22, i+21])

msh = bpy.data.meshes.new("sinmesh") 
msh.from_pydata(verts, [], faces) 
obj = bpy.data.objects.new("sin", msh) 
bpy.context.scene.collection.objects.link(obj) 

sample8.png

5. Finally

Blender is also open source freeware, so it's not bad for modeling. The spec change from 2.7 to 2.8 was shocking, so I decided not to go deeper into blender anymore.

Recommended Posts

Operate Blender with Python
Operate Kinesis with Python
Run Blender with python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
[Note] Operate MongoDB with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Operate a receipt printer with python
Try to operate Facebook with Python
Run mruby with Python or Blender
Operate ECHONET Lite appliances with Python
Easy modeling with Blender and Python
Get started with Python in Blender
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Blender 2.9 Python Extrude extrude
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Operate smartlife power supply with python (de-IFTTT)
[GCP] Operate Google Cloud Storage with Python
[Pyto] Operate iPhone Taptic Engine with Python
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
[Blender x Python] Let's get started with Blender Python !!
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Operate Filemaker from Python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python