瑞 · 喜欢

I love you as who you are

Blender借助Python建模纤维材料

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import bpy

import bmesh

import numpy as np



np.random.seed(42)



def generate_continuous_fiber(z_base, z_variation, num_fibers, num_segments, step_size, square_size):

verts = []

edges = []

half_square = square_size / 2 # 正方形边界的一半

vert_index = 0

for _ in range(num_fibers):

​ start_point = np.array([

​ np.random.uniform(-half_square, half_square),

​ np.random.uniform(-half_square, half_square),

​ z_base

​ ])

​ verts.append(start_point)

​ for _ in range(1, num_segments):

​ while True:

​ step_direction = np.random.uniform(-1, 1, 3)

​ step_direction[2] = 0

​ step = step_size * step_direction / np.linalg.norm(step_direction)

​ next_point = verts[-1] + step

​ next_point[2] = z_base + np.random.uniform(-z_variation, z_variation)

​ if -half_square <= next_point[0] <= half_square and -half_square <= next_point[1] <= half_square:

​ verts.append(next_point)

​ edges.append((vert_index, vert_index + 1))

​ vert_index += 1

​ break

return verts, edges



\# 参数

z_base = 0.5

z_variation = 0.005

num_fibers = 1

num_segments_per_fiber = 8000

step_size = 0.6

square_size = 2.0



verts, edges = generate_continuous_fiber(z_base, z_variation, num_fibers, num_segments_per_fiber, step_size, square_size)



\# 创建一个新的Mesh数据

mesh = bpy.data.meshes.new("fiber_mesh")

obj = bpy.data.objects.new("Fiber", mesh)



\# 将对象添加到场景中,并确保它是可见的

scene = bpy.context.scene

scene.collection.objects.link(obj)

bpy.context.view_layer.objects.active = obj



\# 使用verts和edges更新mesh

mesh = obj.data

bm = bmesh.new()



for v in verts:

bm.verts.new(v)



for e in edges:

bm.edges.new((bm.verts[e[0]], bm.verts[e[1]]))



bm.to_mesh(mesh)

bm.free()

欢迎关注我的其它发布渠道