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.
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.
If you run Python code on blender and get an error, you need to display the console screen. To display the console screen
Click Toggle System Console from the Window menu at the top to display the console screen.
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)
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))
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 )
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))
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
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
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)
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