-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathop_set_vertex_curve.py
543 lines (399 loc) · 16.9 KB
/
op_set_vertex_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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import math
import bpy
from bpy.props import IntProperty, FloatProperty, BoolProperty
import bmesh
import mathutils
from . import interpolate
from . import dijkstra
def collect_vert_path(bm, selected, use_topology_distance):
'''
Find the shortest paths from the selected verts this is based on input order.
[a,b,c] -> ([a,b],[b,c])
'''
current = len(selected) - 1
path = []
while current > 0:
start = bm.verts[selected[current]]
end = bm.verts[selected[current-1]]
current -= 1
nodes = dijkstra.find_path(
bm, start, end, use_topology_distance=use_topology_distance)
path.append((start, end, nodes))
vert_path = []
for p in path:
start, end, nodes = p
node = nodes[end]
start_end_path = node.shortest_path
if start not in vert_path:
vert_path.append(start)
for e in start_end_path:
if e.verts[0] not in vert_path:
vert_path.append(e.verts[0])
elif e.verts[1] not in vert_path:
vert_path.append(e.verts[1])
vert_path = list(reversed(vert_path))
return vert_path
def split_vert_path_into_segments(bm, selected, vert_path):
'''
Splits the vert path into segments based on selected vertices
selected [a1,b1,c1,d1] vert_path[a1, a2, a3, b, b2, c1, c2, c3, d1] -> [a1,b1,c1,d1], ([a1,a2,a3,b], [b1,b2,c], [c1,c2,3,d])
'''
knots = []
segments = [[]]
for v in vert_path:
current_knot_index = len(knots)
current_knot = bm.verts[selected[current_knot_index]]
if v == current_knot:
knots.append(current_knot)
segments[-1].append(v)
if current_knot_index != 0 and current_knot_index != len(selected)-1:
segments.append([v])
else:
segments[-1].append(v)
return knots, segments
def map_segment_onto_spline(segment, positions):
'''
Calculates the total arc length, and evenly distributes the points based on this.
'''
if len(segment) == 1:
return
total_lenght = 0
for index in range(1, len(positions)):
total_lenght += (positions[index] - positions[index-1]).magnitude
segment_part_length = total_lenght / float(len(segment)-1)
current_segment_index = 1
current_length = 0
for index in range(1, len(positions)):
current_length += (positions[index] - positions[index-1]).magnitude
if current_length >= segment_part_length:
remainder = current_length - segment_part_length
current_length = current_length % segment_part_length
p1 = positions[index-1]
p2 = positions[index]
p = p1 + (p1-p2).normalized() * remainder
if current_segment_index != 0 and current_segment_index != len(segment)-1:
v = segment[current_segment_index]
v.co = p
current_segment_index += 1
def curve_hermite(bm, selected, vert_path, tension, space_evenly):
knots, segments = split_vert_path_into_segments(bm, selected, vert_path)
if len(knots) == 1:
return 1, 'Path found is too short - try toggling "Edge Distance"'
total_spline = []
for index, segment in enumerate(segments):
is_start = index == 0
is_end = index == len(segments)-1
# print("--")
if not is_start and not is_end:
# print("middle segment:")
p0 = knots[index-1].co
p1 = knots[index].co
p2 = knots[index+1].co
p3 = knots[index+2].co
elif is_start:
# print("start segment:")
p1 = knots[index].co
p2 = knots[index+1].co
delta = (p1-p2)
p3 = knots[index+2].co
p3 = p2 - ((p2-p3).normalized() * delta.magnitude)
# do a topology search to find the 'previous' edge
for corner in knots[index].link_loops:
if corner.link_loop_next.vert == segment[index+1]:
p0 = corner.link_loop_prev.link_loop_radial_prev.link_loop_prev
break
elif corner.link_loop_prev.vert == segment[index+1]:
p0 = corner.link_loop_radial_prev.link_loop_next.link_loop_next
break
p0 = p0.vert.co
elif is_end:
# print("end segment:")
p1 = knots[-2].co
p2 = knots[-1].co
delta = (p0-p1)
p0 = knots[-3].co
p0 = p1 - ((p1-p0).normalized() * delta.magnitude)
# do a topology search to find the 'previous' edge
for corner in knots[-1].link_loops:
if corner.link_loop_next.vert == segment[-2]:
p3 = corner.link_loop_prev.link_loop_radial_prev.link_loop_prev
break
elif corner.link_loop_prev.vert == segment[-2]:
p3 = corner.link_loop_radial_prev.link_loop_next.link_loop_next
break
p3 = p3.vert.co
bias = 0
spline_points = []
precision = 1000
for i in range(precision):
mu = i / float(precision)
spline_pos = interpolate.hermite_3d(
p0, p1, p2, p3, mu, -tension, bias)
v = mathutils.Vector(spline_pos)
spline_points.append(v)
if not space_evenly:
map_segment_onto_spline(segment, spline_points)
else:
total_spline.extend(spline_points)
if space_evenly:
map_segment_onto_spline(vert_path, total_spline)
return 0, ""
def curve_bezier(bm, selected, vert_path):
knots, segments = split_vert_path_into_segments(bm, selected, vert_path)
for index, segment in enumerate(segments):
is_start = index == 0
is_end = index == len(segments)-1
if not is_start and not is_end:
p0 = knots[index-1]
p1 = knots[index]
p2 = knots[index+1]
p3 = knots[index+2]
center_dir_left = (p1.co-p0.co + p1.co-p2.co).normalized()
up_dir_left = center_dir_left.cross(p2.co - p1.co).normalized()
tangent_left = up_dir_left.cross(center_dir_left).normalized()
dot_left = center_dir_left.dot((p1.co-p2.co).normalized())
center_dir_right = (p1.co-p2.co + p3.co-p2.co).normalized()
up_dir_right = center_dir_right.cross(p2.co - p3.co).normalized()
tangent_right = up_dir_right.cross(center_dir_right).normalized()
dot_right = center_dir_right.dot((p1.co-p2.co).normalized())
length_left = (p2.co - p1.co).magnitude * dot_left * 0.5
length_right = (p3.co - p2.co).magnitude * dot_right * 0.5
elif is_start:
p1 = knots[index]
p2 = knots[index+1]
p3 = knots[index+2]
center_dir_right = (p2.co-p1.co + p2.co-p3.co).normalized()
up_dir_right = center_dir_right.cross(p2.co - p3.co).normalized()
tangent_right = up_dir_right.cross(center_dir_right).normalized()
dot_right = center_dir_right.dot((p2.co-p1.co).normalized())
tangent_left = center_dir_right
length_right = (p3.co - p2.co).magnitude * dot_right * 0.5
length_left = length_right
elif is_end:
p0 = knots[-3]
p1 = knots[-2]
p2 = knots[-1]
center_dir_left = (p1.co-p0.co + p1.co-p2.co).normalized()
up_dir_left = center_dir_left.cross(p2.co - p1.co).normalized()
tangent_left = up_dir_left.cross(center_dir_left).normalized()
dot_left = center_dir_left.dot((p1.co-p2.co).normalized())
print(dot_left, dot_right)
tangent_right = center_dir_left
length_left = (p1.co - p2.co).magnitude * dot_left * 0.5
length_right = length_left
p1_knot = p1.co + tangent_left * length_left
p2_knot = p2.co + tangent_right * length_right
print(f"(p1: {p1.co}, p1_knot: {p1_knot}")
print(f"(p2: {p2.co}, p2_knot: {p2_knot}")
precision = 100
positions = mathutils.geometry.interpolate_bezier(
p1.co, p1_knot, p2_knot, p2.co, len(segment) * precision)
total_lenght = 0
for index in range(len(positions)):
if index == 0:
continue
total_lenght += (positions[index] - positions[index-1]).magnitude
segment_part_length = total_lenght / float(len(segment)-1)
print("total_lenght:", total_lenght)
print("segment_part_length:", segment_part_length)
current_segment_index = 1
current_length = 0
for index in range(len(positions)):
if index == 0:
continue
current_length += (positions[index] - positions[index-1]).magnitude
if current_length > segment_part_length:
current_length = 0 # segment_part_length
# todo maybe interpolate between these two points?
print(current_segment_index, "/", len(segment))
p = positions[index]
if current_segment_index != 0 and current_segment_index != len(segment)-1:
v = segment[current_segment_index]
v.co = p
current_segment_index += 1
# positions = mathutils.geometry.interpolate_bezier(p1.co, p1_knot, p2_knot, p2.co, len(segment))
# for index, v in enumerate(segment):
# if index != 0 and index != len(segment)-1:
# v.co = positions[index]
# print("." * 66)
return 0
def circle_3_points(bm, selected, vert_path, tension, space_evenly):
knots, segments = split_vert_path_into_segments(bm, selected, vert_path)
vert_a = knots[0]
vert_b = knots[1]
vert_c = knots[2]
a = vert_a.co
b = vert_b.co
c = vert_c.co
ac_center = (a+c) * 0.5
b = b + (b-ac_center).normalized() * tension
ab = b-a
bc = c-b
up = ab.cross(bc).normalized()
p1 = a + ab * 0.5
p3 = b + bc * 0.5
p2 = ab.cross(up).normalized()
p4 = bc.cross(up).normalized()
intersection = mathutils.geometry.intersect_line_line(
p1, p1 + p2, p3, p3 + p4)
if not intersection:
return False
center = intersection[0]
start = a - center
middle = b - center
end = c - center
radius = (a - center).magnitude
if space_evenly:
positions = []
samples = len(vert_path)*100
for sample in range(samples):
mu = sample / samples
if mu <= 0.5:
interpolated = start.slerp(middle, mu * 2.0)
interpolated = interpolated.normalized() * radius
else:
interpolated = middle.slerp(end, (mu-0.5) * 2.0)
interpolated = interpolated.normalized() * radius
positions.append(interpolated + center)
map_segment_onto_spline(vert_path, positions)
else:
for index, vert in enumerate(segments[0]):
mu = index / (len(segments[0])-1)
interpolated = start.slerp(middle, mu)
interpolated = interpolated.normalized() * radius
vert.co = interpolated + center
for index, vert in enumerate(segments[1]):
mu = index / (len(segments[1])-1)
interpolated = middle.slerp(end, mu)
interpolated = interpolated.normalized() * radius
vert.co = interpolated + center
return 0, ""
def circle_2_points(bm, selected, vert_path, tension, flip):
'''
Spaces the vertices into a half circle between two points, orientation is based on the topology of the first vert
'''
vert_a = bm.verts[selected[0]]
vert_b = bm.verts[selected[1]]
a = vert_a.co
c = vert_b.co
center = (a + c) * 0.5
radius = (a - c).magnitude * 0.5
# n = None
# for corner in vert_a.link_loops:
# if corner.link_loop_next.vert == vert_path[1]:
# n = (vert_a.co - corner.link_loop_prev.vert.co).normalized()
# break
# elif corner.link_loop_prev.vert == vert_path[1]:
# n = (corner.link_loop_next.vert.co - vert_a.co).normalized()
# break
n = vert_a.normal.cross(c-a).normalized()
# n = (vert_a.normal + vert_b.normal).normalized()
# n = vert_a.normal.cross(a-c).normalized()
if flip:
n = -n
b = center + n * radius
u = c - center
v = b - center
positions = []
samples = len(vert_path)*100
for sample in range(samples):
mu = sample / samples
t = math.pi - (mu * math.pi)
p = center + math.cos(t) * u + math.sin(t) * v * (1.0 + tension)
positions.append(p)
map_segment_onto_spline(vert_path, positions)
return 0, ""
'''
OPERATOR
'''
class SetVertexCurveOp(bpy.types.Operator):
bl_idname = "mesh.align_vertex_curve"
bl_label = "Set Vertex Curve"
bl_options = {'REGISTER', 'UNDO'}
bl_description = '''Curves vertices between the selected vertices in picking order of the selected vertices.
2 vertices selected: placed on a half circle between endpoints.
3 vertices selected: placed onto a circle segment between endpoints.
4+ vertices selected: placed onto a spline going through selected vertices
ALT: reuse last settings
'''
mix: FloatProperty(name="Mix", default=1.0, min=0.0, max=1.0, subtype='FACTOR',
description="Interpolate between inital position and the calculated end position")
tension: IntProperty(name="Tension", default=0, min=-500, max=500,
description="Tension can be used to tighten up the curvature")
use_topology_distance: BoolProperty(name="Use Topology Distance", default=False,
description="Use the edge count instead of edge lengths for distance measure")
flip: BoolProperty(name="Flip Half Circle", default=False,
description="Flip the half circle into other direction")
space_evenly: BoolProperty(name="Space evenly", default=False,
description="Spread the vertices in even distances")
def draw(self, context):
layout = self.layout
layout.use_property_split = True
column = layout.column()
column.prop(self, "mix")
column = layout.column(align=True)
column.prop(self, "tension")
column.prop(self, "use_topology_distance")
if self.vert_count == 2:
column.prop(self, "flip")
if self.vert_count >= 3:
column.prop(self, "space_evenly")
@classmethod
def poll(cls, context):
if (context.space_data.type == 'VIEW_3D'
and context.active_object is not None
and context.active_object.type == "MESH"
and context.active_object.mode == 'EDIT'):
mesh_select_mode = context.scene.tool_settings.mesh_select_mode[:3]
return mesh_select_mode == (True, False, False)
else:
return False
def __init__(self):
self.intial_vert_positions = []
def get_bm(self, me):
bm = bmesh.from_edit_mesh(me)
bm.verts.ensure_lookup_table()
return bm
def get_selected(self, bm):
maybe_selected = [elem.index for elem in bm.select_history if isinstance(
elem, bmesh.types.BMVert)]
selected = list(filter(lambda x: bm.verts[x].select, maybe_selected))
return selected
def invoke(self, context, event):
# print ("-" * 66)
self.vert_count = 0
if event and not event.alt:
self.mix = 1.0
self.tension = 0
self.use_topology_distance = False
self.space_evenly = False
return self.execute(context)
def execute(self, context):
bm = self.get_bm(context.object.data)
selected = self.get_selected(bm)
self.vert_count = len(selected)
if len(selected) < 2:
self.report(
{'WARNING'}, f"Align vertex curve: Please select 2, 3 or more vertices")
return {'CANCELLED'}
# print ("#" * 66)
vert_path = collect_vert_path(bm, selected, self.use_topology_distance)
if len(self.intial_vert_positions) == 0:
for vert in vert_path:
self.intial_vert_positions.append(vert.co.copy())
tension = self.tension / 100.0
if len(selected) == 2:
result, msg = circle_2_points(
bm, selected, vert_path, tension, self.flip)
elif len(selected) == 3:
result, msg = circle_3_points(
bm, selected, vert_path, tension, self.space_evenly)
else:
result, msg = curve_hermite(
bm, selected, vert_path, tension, self.space_evenly)
if result > 0:
self.report({'INFO'}, msg)
for i, vert in enumerate(vert_path):
vert.co = self.intial_vert_positions[i].lerp(vert.co, self.mix)
bmesh.update_edit_mesh(context.object.data, loop_triangles=True)
return {'FINISHED'}