blender-mesh基础

Blender mesh 创建点线面 uOQ-CPcaqMo AxazJi3x6js

任务:创建一个半径为2圆环,圆环上均布32个点,显示为点或者线。

1. 点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# give Python access to Blender's functionality
import bpy
# extend Python's math functionality
import math
# extend Python's print functionality
import pprint

# initialize paramaters
vert_count = 32 # show with 16 and 64
angle_step = math.tau / vert_count
radius = 2

# create a list of vert coordinates
vert_coordinates = list()

# repeat code in a loop
for i in range(vert_count):

# calculate current current_angle
current_angle = angle_step * i

# calculate coordinate
x = radius * math.cos(current_angle)
y = radius * math.sin(current_angle)

# visualize what we are doing
bpy.ops.mesh.primitive_ico_sphere_add(radius=0.05, location=(x, y, 0))

# add current coordinate to list
vert_coordinates.append((x, y, 0))

pprint.pprint(vert_coordinates)

2. 线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# give Python access to Blender's functionality
import bpy
# extend Python's math functionality
import math
import pprint


def get_circle_verts(vert_count, radius):
# initialize paramaters
angle_step = math.tau / vert_count

# create a list of vert coordinates
vert_coordinates = list()

# repeat code in a loop
for i in range(vert_count):

# calculate current current_angle
current_angle = angle_step * i

# calculate coordinate
x = radius * math.cos(current_angle)
y = radius * math.sin(current_angle)

# visualize what we are doing
# bpy.ops.mesh.primitive_ico_sphere_add(radius=0.05, location=(x, y, 0))

# add current coordinate to list
vert_coordinates.append((x, y, 0))

return vert_coordinates


def create_circle_mesh(coordinates, vert_count):
verts = coordinates
faces = []
edges = []

for i in range(vert_count - 1):
current_vert_index = i
next_vert_index = i + 1
edges.append((current_vert_index, next_vert_index))

edges.append((vert_count - 1, 0))

# create a mesh from the vert, edge, and face data
mesh_data = bpy.data.meshes.new("circle_data")
mesh_data.from_pydata(verts, edges, faces)

# create a object using the mesh data
mesh_obj = bpy.data.objects.new("circle_object", mesh_data)

bpy.context.collection.objects.link(mesh_obj)


# initialize paramaters
vert_count = 32
radius = 2

coordinates = get_circle_verts(vert_count, radius)

mesh_obj = create_circle_mesh(coordinates, vert_count)


Final Code:

circle_mesh_part_1 https://gist.github.com/CGArtPython/6e98beb424b6c58bbda0469b2c0434a9
circle_mesh_part_2 https://gist.github.com/CGArtPython/a4dff81d8c233601759dffd48917f296


ref:
Beginner Blender Python Exercise: Circle mesh from scratch
https://www.youtube.com/watch?v=uOQ-CPcaqMo
https://www.youtube.com/watch?v=AxazJi3x6js