-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex_grid_curve.py
330 lines (280 loc) · 12.3 KB
/
hex_grid_curve.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import bpy
import bmesh
import math
import mathutils
from bpy.props import (
BoolProperty,
IntProperty,
EnumProperty,
FloatProperty,
FloatVectorProperty)
bl_info = {
"name": "Create Hex Grid Curve",
"author": "Jeremy Behreandt",
"version": (0, 1),
"blender": (4, 1, 0),
"category": "Add Curve",
"description": "Creates a hexagon grid curve.",
"tracker_url": "https://github.com/behreajj/HexGrid"
}
class HexGridCurveMaker(bpy.types.Operator):
"""Creates a grid of hexagons"""
bl_idname = "curve.primitive_hexgrid_add"
bl_label = "Hex Grid"
bl_options = {"REGISTER", "UNDO"}
rings: IntProperty(
name="Rings",
description="Number of rings in grid",
min=1,
soft_max=32,
default=4,
step=1) # type: ignore
cell_radius: FloatProperty(
name="Cell Radius",
description="Radius of each hexagon cell",
min=0.0001,
soft_max=100.0,
step=1,
precision=3,
default=0.5) # type: ignore
cell_margin: FloatProperty(
name="Cell Margin",
description="Margin between each hexagon cell",
min=0.0,
soft_max=99.0,
step=1,
precision=3,
default=0.0325) # type: ignore
rounding: FloatProperty(
name="Rounding",
description="Percentage by which to round corners.",
default=0.0,
step=1,
precision=3,
min=0.0,
max=1.0,
subtype="FACTOR") # type: ignore
straight_edge: EnumProperty(
items=[
("ALIGNED", "Aligned", "Aligned", 1),
("FREE", "Free", "Free", 2),
("VECTOR", "Vector", "Vector", 3)],
name="Handle Type",
default="FREE",
description="Handle type to use for straight edges") # type: ignore
res_u: IntProperty(
name="Resolution",
description="Corner resolution",
min=1,
soft_max=64,
default=12) # type: ignore
fill_mode: EnumProperty(
items=[
("NONE", "None", "None", 1),
("BACK", "Back", "Back", 2),
("FRONT", "Front", "Front", 3),
("BOTH", "Both", "Both", 4)],
name="Fill Mode",
default="BOTH",
description="Fill mode to use") # type: ignore
extrude_thick: FloatProperty(
name="Extrude",
description="Extrusion thickness",
min=0.0,
soft_max=1.0,
step=1,
precision=3,
default=0.0) # type: ignore
extrude_off: FloatProperty(
name="Offset",
description="Extrusion offset",
min=-1.0,
max=1.0,
step=1,
precision=3,
subtype="FACTOR",
default=0.0) # type: ignore
def execute(self, context):
# Constants.
eps = 0.000001
o_3 = 1.0 / 3.0
t_3 = 2.0 / 3.0
sqrt_3 = 1.7320508075688772 # 3.0 ** 0.5
k = 0.5522847498307936 # 1.0 / (3.0 ** 0.5)
handle_fac = k * 1.1547005383792515
one_h_fac = 1.0 - handle_fac
# Unpack arguments.
verif_rings = 1 if self.rings < 1 else self.rings
verif_rad = max(eps, self.cell_radius)
verif_margin = max(0.0, self.cell_margin)
verif_rounding = self.rounding
straight_handle_type = self.straight_edge
is_straight = verif_rounding <= 0.0
is_circle = verif_rounding >= 1.0
if is_straight and straight_handle_type == "ALIGNED":
straight_handle_type = "VECTOR"
corner_handle_type = "FREE"
if straight_handle_type == "ALIGNED":
corner_handle_type = "ALIGNED"
# Intermediate calculations.
extent = sqrt_3 * verif_rad
rad_1_5 = verif_rad * 1.5
pad_rad = max(eps, verif_rad - verif_margin)
half_ext = extent * 0.5
one_round = 1.0 - verif_rounding
# Added to hexagon center to find corners.
half_rad = pad_rad * 0.5
rad_rt3_2 = half_rad * sqrt_3
i_max = verif_rings - 1
i_min = -i_max
crv_data = bpy.data.curves.new("Hex.Grid", "CURVE")
crv_data.dimensions = "2D"
crv_data.fill_mode = self.fill_mode
crv_data.extrude = self.extrude_thick
crv_data.offset = self.extrude_off
crv_splines = crv_data.splines
# See https://www.redblobgames.com/grids/hexagons/implementation.html#shape-hexagon
for i in range(i_min, i_max + 1):
j_min = max(i_min, i_min - i)
j_max = min(i_max, i_max - i)
i_ext = i * extent
for j in range(j_min, j_max + 1):
# Hexagon center.
x = i_ext + j * half_ext
y = j * rad_1_5
# Hexagon edges.
left = x - rad_rt3_2
right = x + rad_rt3_2
top = y + half_rad
bottom = y - half_rad
# Hexagon vertices, beginning at top center
# moving counter clockwise.
v = [
(x, y + pad_rad, 0.0),
(left, top, 0.0),
(left, bottom, 0.0),
(x, y - pad_rad, 0.0),
(right, bottom, 0.0),
(right, top, 0.0) ]
spline = crv_splines.new("BEZIER")
spline.use_cyclic_u = True
spline.resolution_u = self.res_u
bz_pts = spline.bezier_points
if is_straight:
bz_pts.add(5)
kn_idx_curr = 0
for kn in bz_pts:
kn_idx_prev = (kn_idx_curr - 1) % 6
kn_idx_next = (kn_idx_curr + 1) % 6
co_curr = v[kn_idx_curr]
co_prev = v[kn_idx_prev]
co_next = v[kn_idx_next]
kn.co = co_curr
kn.handle_left_type = straight_handle_type
kn.handle_right_type = straight_handle_type
kn.handle_left = (
t_3 * co_curr[0] + o_3 * co_prev[0],
t_3 * co_curr[1] + o_3 * co_prev[1],
t_3 * co_curr[2] + o_3 * co_prev[2])
kn.handle_right = (
t_3 * co_curr[0] + o_3 * co_next[0],
t_3 * co_curr[1] + o_3 * co_next[1],
t_3 * co_curr[2] + o_3 * co_next[2])
kn_idx_curr = kn_idx_curr + 1
else:
# Calculate midpoints.
mp = [(0.0, 0.0, 0.0)] * 6
for mp_idx in range(0, 6):
mp_idx_next = (mp_idx + 1) % 6
v_curr = v[mp_idx]
v_next = v[mp_idx_next]
mp[mp_idx] = (
(v_curr[0] + v_next[0]) * 0.5,
(v_curr[1] + v_next[1]) * 0.5,
(v_curr[2] + v_next[2]) * 0.5)
if is_circle:
bz_pts.add(5)
kn_idx_curr = 0
for kn in bz_pts:
v_idx_next = (kn_idx_curr + 1) % 6
v_prev = v[kn_idx_curr]
v_next = v[v_idx_next]
co = mp[kn_idx_curr]
kn.co = co
kn.handle_left_type = corner_handle_type
kn.handle_right_type = corner_handle_type
kn.handle_left = (
one_h_fac * co[0] + handle_fac * v_prev[0],
one_h_fac * co[1] + handle_fac * v_prev[1],
one_h_fac * co[2] + handle_fac * v_prev[2])
kn.handle_right = (
one_h_fac * co[0] + handle_fac * v_next[0],
one_h_fac * co[1] + handle_fac * v_next[1],
one_h_fac * co[2] + handle_fac * v_next[2])
kn_idx_curr = kn_idx_curr + 1
else:
bz_pts.add(11)
kn_idx_curr = 0
for kn in bz_pts:
v_idx_curr = kn_idx_curr // 2
v_idx_prev = (v_idx_curr - 1) % 6
v_idx_next = (v_idx_curr + 1) % 6
v_curr = v[v_idx_curr]
v_prev = v[v_idx_prev]
v_next = v[v_idx_next]
mp_curr = mp[v_idx_curr]
mp_prev = mp[v_idx_prev]
is_even = kn_idx_curr % 2 != 1
if is_even:
co_curr = (
one_round * v_curr[0] + verif_rounding * mp_prev[0],
one_round * v_curr[1] + verif_rounding * mp_prev[1],
one_round * v_curr[2] + verif_rounding * mp_prev[2])
co_prev = (
one_round * v_prev[0] + verif_rounding * mp_prev[0],
one_round * v_prev[1] + verif_rounding * mp_prev[1],
one_round * v_prev[2] + verif_rounding * mp_prev[2])
kn.co = co_curr
kn.handle_left_type = straight_handle_type
kn.handle_right_type = corner_handle_type
kn.handle_left = (
t_3 * co_curr[0] + o_3 * co_prev[0],
t_3 * co_curr[1] + o_3 * co_prev[1],
t_3 * co_curr[2] + o_3 * co_prev[2])
kn.handle_right = (
one_h_fac * co_curr[0] + handle_fac * v_curr[0],
one_h_fac * co_curr[1] + handle_fac * v_curr[1],
one_h_fac * co_curr[2] + handle_fac * v_curr[2])
else:
co_curr = (
one_round * v_curr[0] + verif_rounding * mp_curr[0],
one_round * v_curr[1] + verif_rounding * mp_curr[1],
one_round * v_curr[2] + verif_rounding * mp_curr[2])
co_next = (
one_round * v_next[0] + verif_rounding * mp_curr[0],
one_round * v_next[1] + verif_rounding * mp_curr[1],
one_round * v_next[2] + verif_rounding * mp_curr[2])
kn.co = co_curr
kn.handle_left_type = corner_handle_type
kn.handle_right_type = straight_handle_type
kn.handle_left = (
one_h_fac * co_curr[0] + handle_fac * v_curr[0],
one_h_fac * co_curr[1] + handle_fac * v_curr[1],
one_h_fac * co_curr[2] + handle_fac * v_curr[2])
kn.handle_right = (
t_3 * co_curr[0] + o_3 * co_next[0],
t_3 * co_curr[1] + o_3 * co_next[1],
t_3 * co_curr[2] + o_3 * co_next[2])
kn_idx_curr = kn_idx_curr + 1
crv_obj = bpy.data.objects.new(crv_data.name, crv_data)
crv_obj.location = context.scene.cursor.location
context.scene.collection.objects.link(crv_obj)
return {"FINISHED"}
def menu_func(self, context):
self.layout.operator(HexGridCurveMaker.bl_idname, icon="SEQ_CHROMA_SCOPE")
def register():
bpy.utils.register_class(HexGridCurveMaker)
bpy.types.VIEW3D_MT_curve_add.append(menu_func)
def unregister():
bpy.utils.unregister_class(HexGridCurveMaker)
bpy.types.VIEW3D_MT_curve_add.remove(menu_func)