import bpy
#Define vertices and faces that form a plane
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
faces = [(0,1,2,3)]
#Define a mesh
mesh = bpy.data.meshes.new("Plane_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
#Define an object from mesh data
obj = bpy.data.objects.new("Plane", mesh)
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)
import bpy
#Define the vertices and faces that form the cube
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (4,5,6,7), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]
#Define a mesh
mesh = bpy.data.meshes.new("Cube_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
#Define an object from mesh data
obj = bpy.data.objects.new("Cube", mesh)
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)
import bpy
#Define the vertices and faces that form the quadrangular pyramid
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(2.5,2.5,4.5)]
faces = [(0,1,2,3), (0,4,1), (1,4,2), (2,4,3), (3,4,0)]
#Define a mesh
mesh = bpy.data.meshes.new("Pyramid_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
#Define an object from mesh data
obj = bpy.data.objects.new("Pyramid", mesh)
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)
import bpy
import math
#Specify how many squares
p = 5
#Place the vertices starting from the center
verts = [(0,0,0)]
for i in range(0,p + 1):
x = 2 *math.pi / p * i
verts.append([math.cos(x),math.sin(x),0])
#A surface is formed by a total of 3 points, 2 points on the center and the circumference.
faces = []
for i in range(0,p):
faces.append([0,i + 1,i + 2])
mesh = bpy.data.meshes.new("Polygon_mesh")
mesh.from_pydata(verts, [], faces)
obj = bpy.data.objects.new("Polygon", mesh)
bpy.context.scene.collection.objects.link(obj)
#Put into edit mode
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
#Remove duplicate vertices
bpy.ops.mesh.remove_doubles()
#Return to object mode
bpy.ops.object.mode_set(mode='OBJECT')
import bpy
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (7,6,5,4), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]
mesh = bpy.data.meshes.new("Cube_mesh")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
obj = bpy.data.objects.new("Cube", mesh)
obj.location = bpy.context.scene.cursor.location
#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
#Determine the level of subdivision surface
obj.modifiers['subd'].levels = 3
bpy.context.scene.collection.objects.link(obj)
#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
p.use_smooth = True
import bpy
import random
verts = []
faces = []
#Variables related to mesh
numX = 20
numY = 20
#Variables related to wave shape
amp = 0.5
scale = 1
#Generate vertex coordinates
for i in range (0, numX):
for j in range(0,numY):
x = scale * i
y = scale * j
z = (i*random.random())*amp
vert = (x,y,z)
verts.append(vert)
#Generate a face from four vertices
count = 0
for i in range (0, numY *(numX-1)):
if count < numY-1:
A = i
B = i+1
C = (i+numY)+1
D = (i+numY)
face = (A,B,C,D)
faces.append(face)
count = count + 1
else:
count = 0
mesh = bpy.data.meshes.new("random mesh")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
obj = bpy.data.objects.new("random mesh",mesh)
obj.location = bpy.context.scene.cursor.location
bpy.context.scene.collection.objects.link(obj)
#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
obj.modifiers['subd'].levels = 3
#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
p.use_smooth = True
import bpy
import math
verts = []
faces = []
#Variables related to mesh
numX = 10
numY = 10
#Variables related to wave shape
freq = 1
amp = 1
scale = 1
#Generate vertex coordinates
for i in range (0, numX):
for j in range(0,numY):
x = scale * i
y = scale * j
z = scale*((amp*math.cos(i*freq))+(amp*math.sin(j*freq)))
vert = (x,y,z)
verts.append(vert)
#Generate a face from four vertices
count = 0
for i in range (0, numY *(numX-1)):
if count < numY-1:
A = i
B = i+1
C = (i+numY)+1
D = (i+numY)
face = (A,B,C,D)
faces.append(face)
count = count + 1
else:
count = 0
mesh = bpy.data.meshes.new("wave")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
obj = bpy.data.objects.new("wave",mesh)
obj.location = (0,0,0)
bpy.context.scene.collection.objects.link(obj)
#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
obj.modifiers['subd'].levels = 3
#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
p.use_smooth = True
Reference site Nathan's Blender Python Notebook
Recommended Posts