-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
620 lines (461 loc) · 17.3 KB
/
main.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import bpy
import bmesh
import math
import mathutils
from mathutils import Matrix, Vector
from collections import defaultdict
import time
from .render import create_vao
from .render import set_udims
from .prefs import debug
UV_MOUSE = None
last_update = 0
updating = False
scene_update = False
translate_active = False
INIT = None
def heartbeat():
global updating, scene_update, last_update
delta = (time.perf_counter() - last_update)
# print(delta)
if delta > 1.0 / 10.0 and scene_update:
update()
scene_update = False
def handle_scene_update(context):
global scene_update, last_update, updating, UV_MOUSE, SCRIPT_RELOAD
start()
try:
delta = (time.perf_counter() - last_update)
if delta > 1.0 / 10.0 and scene_update:
scene_update = False
update()
return
edit_obj = bpy.context.edit_object
if edit_obj is not None and edit_obj.is_updated_data is True:
# UV_MOUSE = None
last_update = time.perf_counter()
scene_update = True
except AttributeError as e:
pass
selected_verts = []
hidden_edges = []
selected_edges = []
selected_faces = []
closest_vert = None
other_vert = None
closest_edge = None
other_edge = None
closest_face = None
vert_count = 0
vert_select_count = 0
uv_select_count = 0
bm_instance = None
def start():
global INIT
if INIT:
return
INIT = True
from . import operators
if not operators.MOUSE_UPDATE:
# pass
#bpy.ops.uv.uv_highlight_heartbeat('INVOKE_DEFAULT')
bpy.ops.uv.uv_mouse_position('INVOKE_DEFAULT')
def reset():
global hidden_edges, vert_count, vert_select_count, uv_select_count, bm_instance, hidden_edges
vert_count = 0
vert_select_count = 0
uv_select_count = 0
bm_instance = None
selected_verts.clear()
selected_faces.clear()
selected_edges.clear()
hidden_edges.clear()
create_vao("closest_faces", [])
create_vao("closest_face_uvs", [])
create_vao("hidden_edges", [])
create_vao("selected_verts", [])
create_vao("selected_edges", [])
create_vao("selected_faces", [])
def update(update_cache=True):
#t1 = time.perf_counter()
global hidden_edges, vert_count, vert_select_count, uv_select_count, bm_instance, hidden_edges
# print("update")
if not isEditingUVs():
reset()
return False
settings = bpy.context.scene.uv_highlight
prefs = bpy.context.user_preferences.addons[__package__].preferences
mesh = bpy.context.active_object.data
force_cache_rebuild = False
if not bm_instance or not bm_instance.is_valid:
force_cache_rebuild = True
bm_instance = bmesh.from_edit_mesh(mesh)
# this gets slow, so I bail out :X
if len(bm_instance.verts) > prefs.max_verts:
return
uv_layer = bm_instance.loops.layers.uv.verify()
bm_instance.faces.layers.tex.verify()
verts_updated, verts_selection_changed, uv_selection_changed = detect_mesh_changes(bm_instance, uv_layer)
if force_cache_rebuild or verts_selection_changed or update_cache:
if debug:
print("-- uv highlight rebuild cache --")
create_chaches(bm_instance, uv_layer)
if bpy.context.scene.uv_highlight.boundaries_as_seams:
bpy.ops.uv.seams_from_islands(mark_sharp=bpy.context.scene.uv_highlight.boundaries_as_sharp)
tag_redraw_all_views()
if UV_MOUSE and settings.show_preselection:
#clear preselection drawing
create_vao("closest_faces", [])
create_vao("closest_face_uvs", [])
try:
if not translate_active:
#print ("-- update preselection -- " + str(time.time()))
update_preselection(bm_instance, uv_layer)
# tag_redraw_all_views()
except ReferenceError as e:
if debug:
print("-- uv highlight rebuild cache --")
bm_instance = None
update()
else:
from . import operators
if not operators.MOUSE_UPDATE:
bpy.ops.uv.uv_mouse_position('INVOKE_DEFAULT')
t_uv_selection_changed_start = time.perf_counter()
if uv_selection_changed:
collect_selected_elements(bm_instance, uv_layer)
t_uv_selection_changed_end = time.perf_counter()
# print("..............")
# print("edges: ", len(edges))
# print("edges selection boundary: ", len(list(filter(lambda x: x[1], edges))))
# print("edges island boundary: ", len(list(filter(lambda x: x[2], edges))))
#t2 = time.perf_counter()
# print("update cost: %.3f - selection change: %.3f" % (
# t2 - t1, t_uv_selection_changed_end - t_uv_selection_changed_start))
return True
# caches
kdtree = None
uv_to_loop = {}
faces_to_uvs = defaultdict(set)
uvs_to_faces = defaultdict(set)
def create_chaches(bm, uv_layer):
global kdtree, uv_to_loop, hidden_edges
udims = set()
edges_to_draw = set()
hidden_edges.clear()
uv_to_loop.clear()
faces_to_uvs.clear()
uvs_to_faces.clear()
for f in bm.faces:
if not f.select:
for edge in f.edges:
for el in edge.link_loops:
uv = el[uv_layer].uv.copy().freeze()
nextuv = el.link_loop_next[uv_layer].uv.copy().freeze()
uv_edges = set()
uv_edges.add((uv, nextuv))
uv_edges.add((nextuv, uv))
if edges_to_draw.isdisjoint(uv_edges):
edges_to_draw.update(uv_edges)
hidden_edges.append(uv.x)
hidden_edges.append(uv.y)
hidden_edges.append(nextuv.x)
hidden_edges.append(nextuv.y)
for l in f.loops:
uv = l[uv_layer].uv.copy()
# collect udim info
x = math.ceil(uv.x)
y = math.ceil(uv.y) - 1
if x >= 1 and x <= 10 and y >= 0:
udims.add(x + (y * 100 + 1000))
if f.select:
uv.resize_3d()
uv.freeze()
uv_to_loop[uv] = l
id = uv.to_tuple(8), l.vert.index
faces_to_uvs[f.index].add(id)
uvs_to_faces[id].add(f.index)
kdtree = mathutils.kdtree.KDTree(len(uv_to_loop))
i = 0
for k, v in uv_to_loop.items():
kdtree.insert(k, i)
i += 1
kdtree.balance()
set_udims(udims)
create_vao("hidden_edges", hidden_edges)
def update_preselection(bm, uv_layer):
global closest_vert, other_vert, closest_edge, other_edge, closest_face, UV_MOUSE
closest_vert = None
other_vert = None
closest_edge = None
closest_face = None
edgeDistance = 1000000
mode = bpy.context.scene.tool_settings.uv_select_mode
# collect closest vert
closestUV = kdtree.find(UV_MOUSE.resized(3))[0]
if closestUV:
closestUV.freeze()
closest_loop = uv_to_loop[closestUV]
closest_vert = (closest_loop.vert.co.copy(), closest_loop.vert.normal.copy()), closestUV
for l in closest_loop.vert.link_loops:
if l == closest_loop:
continue
uv = l[uv_layer]
if uv.uv != closest_loop[uv_layer].uv:
other_vert = uv.uv.copy().freeze()
else:
# if there is no closest vert, then there are just no elements at all
create_vao("closest_faces", [])
create_vao("closest_face_uvs", [])
return
# find closet edge
for edge in closest_loop.vert.link_edges:
if not edge.select:
continue
for l in edge.link_loops:
uv = l[uv_layer]
next_uv = l.link_loop_next[uv_layer]
d = distanceToLine(uv.uv, next_uv.uv, UV_MOUSE)
if d < edgeDistance:
edgeDistance = d
closest_edge = edge, uv.uv, next_uv.uv
if closest_edge:
edge, uv, next_uv = closest_edge
edge_coord = (
(edge.verts[0].co.copy(), edge.verts[0].normal.copy()),
(edge.verts[1].co.copy(), edge.verts[1].normal.copy()))
closest_edge = (edge_coord, (uv.copy(), next_uv.copy()))
# search the other uv edge
other_edge = None
for l in edge.link_loops:
other_uv = l[uv_layer].uv
other_nextuv = l.link_loop_next[uv_layer].uv
if (l.edge.select and other_uv != uv and other_nextuv != next_uv): # and
# other_uv != next_uv and other_nextuv != uv):
other_edge_coord = ((l.edge.verts[0].co.copy(), l.edge.verts[0].normal.copy()),
(l.edge.verts[1].co.copy(), l.edge.verts[1].normal.copy()))
other_edge = (other_edge_coord, (other_uv.copy(), other_nextuv.copy()))
break
for f in closest_loop.vert.link_faces: # potential_faces:
if not f.select:
continue
face_uvs = []
for l in f.loops:
face_uvs.append(l[uv_layer].uv)
if point_in_polygon(UV_MOUSE, face_uvs):
closest_face = [f]
break
if mode == "ISLAND" and closest_face:
faces_left = set(faces_to_uvs.keys())
if len(faces_left) > 0:
closest_face = parse_uv_island(bm, closest_face[0].index)
# print(len(closestFace))
if closest_face != None and len(closest_face) > 0:
faces = []
for f in closest_face:
faces.append(f.index)
verts, uvs = get_triangulated_faces(bm, faces, collect_uvs=True)
create_vao("closest_faces", verts)
create_vao("closest_face_uvs", uvs)
def isEditingUVs():
context = bpy.context
obj = context.active_object
if obj == None or obj.mode != "EDIT":
return False
'''
# if context.active_object.data.total_vert_sel == 0:
# return False
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type == 'IMAGE_EDITOR':
if (area.spaces.active.mode == 'VIEW'
and context.scene.tool_settings.use_uv_select_sync == False):
return True
'''
# if not context.scene.tool_settings.use_uv_select_sync:
# return True
return True
def detect_mesh_changes(bm, uv_layer):
global vert_count, uv_select_count, vert_select_count
verts_updated = False
verts_selection_changed = False
uv_selection_changed = False
if vert_count != len(bm.verts):
vert_count = len(bm.verts)
verts_updated = True
verts_selected = sum([v.index for v in bm.verts if v.select])
if verts_selected != vert_select_count:
vert_select_count = verts_selected
verts_selection_changed = True
uv_count = 0
for f in bm.faces:
if f.select:
for l in f.loops:
if l[uv_layer].select:
uv_count += l.index
if uv_select_count != uv_count:
uv_select_count = uv_count
uv_selection_changed = True
return (verts_updated, verts_selection_changed, uv_selection_changed)
def collect_selected_elements(bm, uv_layer):
# global selected_verts, selected_edges # , selected_faces
selected_verts = []
selected_edges = []
selected_faces = set()
matrix = bpy.context.active_object.matrix_world
# collect selected elements
for f in bm.faces:
if not f.select:
continue
start = f.loops[0]
current = None
face_uvs_selected = True
f_verts = []
while start != current:
if current == None:
current = start
uv = current[uv_layer]
next_uv = current.link_loop_next[uv_layer]
if uv.select:
v = (current.vert.co.copy().freeze(), current.vert.normal.copy().freeze())
# selected_verts.add(v)
vert = matrix * v[0]
selected_verts.append(vert.x)
selected_verts.append(vert.y)
selected_verts.append(vert.z)
f_verts.append(v)
elif face_uvs_selected:
face_uvs_selected = False
if uv.select and next_uv.select:
island_boundary = current.edge.is_boundary
selection_boundary = False
for link_face in current.edge.link_faces:
if link_face == f:
continue
for linkloop in link_face.loops:
linkuv = linkloop[uv_layer]
if not linkuv.select:
selection_boundary = True
break
# v1 = (current.edge.verts[0].co.copy().freeze(), current.edge.verts[0].normal.copy().freeze())
# v2 = (current.edge.verts[1].co.copy().freeze(), current.edge.verts[1].normal.copy().freeze())
# selected_edges.add(((v1, v2), selection_boundary, island_boundary))
a = matrix * current.edge.verts[0].co
b = matrix * current.edge.verts[1].co
selected_edges.append(a.x)
selected_edges.append(a.y)
selected_edges.append(a.z)
selected_edges.append(b.x)
selected_edges.append(b.y)
selected_edges.append(b.z)
current = current.link_loop_next
if face_uvs_selected and f.select:
selected_faces.add(f.index)
create_vao("selected_verts", selected_verts)
create_vao("selected_edges", selected_edges)
# create a triangulated vertex array of the selected faces
if len(selected_faces) > 0:
triangulated_verts = get_triangulated_faces(bm, selected_faces)
create_vao("selected_faces", triangulated_verts)
else:
create_vao("selected_faces", [])
def get_triangulated_faces(bm, face_selection, collect_uvs=False):
clone = bm.copy()
unselected = []
for f in clone.faces:
if f.index not in face_selection:
unselected.append(f)
bmesh.ops.delete(clone, geom=unselected, context=5)
triangulated = clone.calc_tessface()
if collect_uvs:
uv_layer = clone.loops.layers.uv.verify()
clone.faces.layers.tex.verify()
matrix = bpy.context.active_object.matrix_world
triangulated_verts = []
triangulated_uvs = []
for triangle in triangulated:
for loop in triangle:
pos = matrix * loop.vert.co
triangulated_verts.append(pos.x)
triangulated_verts.append(pos.y)
triangulated_verts.append(pos.z)
if collect_uvs:
triangulated_uvs.append(loop[uv_layer].uv.x)
triangulated_uvs.append(loop[uv_layer].uv.y)
del clone
if collect_uvs:
return triangulated_verts, triangulated_uvs
return triangulated_verts
# a non recursive rewrite of https://github.com/nutti/Magic-UV/blob/develop/uv_magic_uv/muv_packuv_ops.py
def parse_uv_island(bm, face_idx):
# print(len(faces_to_uvs), len(uvs_to_faces))
faces_left = set(faces_to_uvs.keys()) # all faces
island = []
candidates = set([face_idx])
next_candidates = set()
bm.faces.ensure_lookup_table()
while len(candidates) > 0:
for current in candidates:
if current in faces_left:
faces_left.remove(current)
island.append(bm.faces[current])
for uv in faces_to_uvs[current]:
connected_faces = uvs_to_faces[uv]
if connected_faces:
for cf in connected_faces:
next_candidates.add(cf)
candidates.clear()
candidates.update(next_candidates)
next_candidates.clear()
return island
def collect_faces(faces, bmedges, depth, max_depth):
for e in bmedges:
for f in e.link_faces:
if not f.select:
continue
faces.add(f)
depth += 1
if depth <= max_depth:
collect_faces(faces, f.edges, depth, max_depth)
def distanceToLine(start, end, point):
line_vec = start - end
point_vec = start - point
line_unitvec = line_vec.normalized()
if line_vec.length == 0:
return 0
point_vec_scaled = point_vec * (1.0 / line_vec.length)
t = line_unitvec.dot(point_vec_scaled)
if t < 0.0:
t = 0.0
elif t > 1.0:
t = 1.0
nearest = line_vec * t
dist = (nearest - point_vec).length
return dist
def point_in_polygon(p, polygon):
x, y = p[0], p[1]
inside = False
j = len(polygon) - 1
for i in range(len(polygon)):
xi = polygon[i][0]
yi = polygon[i][1]
xj = polygon[j][0]
yj = polygon[j][1]
intersect = (((yi > y) != (yj > y)) and (x < (xj - xi) * (y - yi) / (yj - yi) + xi))
if (intersect):
inside = not inside
j = i
return inside;
# some code here is from space_view3d_math_vis
def tag_redraw_all_views():
# print("redraw")
all_views(lambda region: region.tag_redraw())
def all_views(func):
context = bpy.context
# Py cant access notifers
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type == 'VIEW_3D' or area.type == 'IMAGE_EDITOR':
for region in area.regions:
if region.type == 'WINDOW':
func(region)