-
Notifications
You must be signed in to change notification settings - Fork 140
/
op_rectify.py
482 lines (382 loc) · 12.2 KB
/
op_rectify.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
import bpy
import bmesh
from math import hypot
from collections import defaultdict
from . import utilities_uv
precision = 3
class op(bpy.types.Operator):
bl_idname = "uv.textools_rectify"
bl_label = "Rectify"
bl_description = "Align selected UV faces or vertices to rectangular distribution"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if bpy.context.area.ui_type != 'UV':
return False
if not bpy.context.active_object:
return False
if bpy.context.active_object.mode != 'EDIT':
return False
if bpy.context.active_object.type != 'MESH':
return False
if not bpy.context.active_object.data.uv_layers:
return False
if context.scene.tool_settings.use_uv_select_sync:
return False
return True
def execute(self, context):
utilities_uv.multi_object_loop(rectify, self, context)
return {'FINISHED'}
def rectify(self, context, me=None, bm=None, uv_layers=None):
if me is None:
me = bpy.context.active_object.data
bm = bmesh.from_edit_mesh(me)
uv_layers = bm.loops.layers.uv.verify()
# Store selection
faces_loops = utilities_uv.selection_store(bm, uv_layers, return_selected_faces_loops=True)
# Find selection islands
islands = utilities_uv.getSelectionIslands(bm, uv_layers, selected_faces=set(faces_loops.keys()))
for island in islands:
bpy.ops.uv.select_all(action='DESELECT')
utilities_uv.set_selected_faces(island, bm, uv_layers)
main(me, bm, uv_layers, island, faces_loops)
# Restore selection
utilities_uv.selection_restore(bm, uv_layers)
def main(me, bm, uv_layers, selFacesMix, faces_loops, return_discarded_faces=False):
filteredVerts, selFaces, vertsDict, discarded_faces = ListsOfVerts(bm, uv_layers, selFacesMix, faces_loops)
if len(filteredVerts) < 2:
if return_discarded_faces:
return set()
return
if not selFaces:
if discarded_faces:
if return_discarded_faces:
return discarded_faces
else:
# Line is selected -> align on axis
for luv in filteredVerts:
x = round(luv.uv.x, precision)
y = round(luv.uv.y, precision)
if luv not in vertsDict[(x, y)]:
vertsDict[(x, y)].append(luv)
areLinedX = True
areLinedY = True
allowedError = 0.00001
valX = filteredVerts[0].uv.x
valY = filteredVerts[0].uv.y
for v in filteredVerts:
if abs(valX - v.uv.x) > allowedError:
areLinedX = False
if abs(valY - v.uv.y) > allowedError:
areLinedY = False
if not (areLinedX or areLinedY):
verts = filteredVerts
verts.sort(key=lambda x: x.uv[0]) #sort by .x
first = verts[0]
last = verts[len(verts)-1]
horizontal = True
if ((last.uv.x - first.uv.x) > 0.0009):
slope = (last.uv.y - first.uv.y)/(last.uv.x - first.uv.x)
if (slope > 1) or (slope <-1):
horizontal = False
else:
horizontal = False
if horizontal == True:
#scale to 0 on Y
for v in verts:
x = round(v.uv.x, precision)
y = round(v.uv.y, precision)
for luv in vertsDict[(x, y)]:
luv.uv.y = first.uv.y
else:
#scale to 0 on X
verts.sort(key=lambda x: x.uv[1]) #sort by .y
verts.reverse() #reverse because y values drop from up to down
first = verts[0]
last = verts[len(verts)-1]
for v in verts:
x = round(v.uv.x, precision)
y = round(v.uv.y, precision)
for luv in vertsDict[(x, y)]:
luv.uv.x = first.uv.x
else:
# At least one face is selected -> rectify
targetFace = bm.faces.active
# Active face checks
if targetFace is None or len({loop for loop in targetFace.loops}.intersection(filteredVerts)) != len(targetFace.verts) or targetFace.select == False or len(targetFace.verts) != 4:
targetFace = selFaces[0]
ShapeFace(uv_layers, targetFace, vertsDict)
FollowActiveUV(me, targetFace, selFaces)
bmesh.update_edit_mesh(me, loop_triangles=False)
if return_discarded_faces:
return discarded_faces
def ListsOfVerts(bm, uv_layers, selFacesMix, faces_loops):
allEdgeVerts = []
filteredVerts = []
selFaces = []
discarded_faces = set()
vertsDict = defaultdict(list)
for f in selFacesMix:
isFaceSel = True
facesEdgeVerts = [l[uv_layers] for l in faces_loops[f]]
if len(faces_loops[f]) < len(f.loops):
isFaceSel = False
allEdgeVerts.extend(facesEdgeVerts)
if isFaceSel:
if len(f.verts) != 4:
filteredVerts.extend(facesEdgeVerts)
discarded_faces.add(f)
else:
selFaces.append(f)
for luv in facesEdgeVerts:
x = round(luv.uv.x, precision)
y = round(luv.uv.y, precision)
vertsDict[(x, y)].append(luv)
else:
filteredVerts.extend(facesEdgeVerts)
if len(filteredVerts) == 0:
filteredVerts.extend(allEdgeVerts)
return filteredVerts, selFaces, vertsDict, discarded_faces
def ShapeFace(uv_layers, targetFace, vertsDict):
corners = []
for l in targetFace.loops:
luv = l[uv_layers]
corners.append(luv)
if len(corners) != 4:
return
firstHighest = corners[0]
for c in corners:
if c.uv.y > firstHighest.uv.y:
firstHighest = c
corners.remove(firstHighest)
secondHighest = corners[0]
for c in corners:
if (c.uv.y > secondHighest.uv.y):
secondHighest = c
if firstHighest.uv.x < secondHighest.uv.x:
leftUp = firstHighest
rightUp = secondHighest
else:
leftUp = secondHighest
rightUp = firstHighest
corners.remove(secondHighest)
firstLowest = corners[0]
secondLowest = corners[1]
if firstLowest.uv.x < secondLowest.uv.x:
leftDown = firstLowest
rightDown = secondLowest
else:
leftDown = secondLowest
rightDown = firstLowest
verts = [leftUp, leftDown, rightDown, rightUp]
ratioX, ratioY = ImageRatio()
min = float('inf')
minV = verts[0]
for v in verts:
if v is None:
continue
for area in bpy.context.screen.areas:
if area.ui_type == 'UV':
loc = area.spaces[0].cursor_location
hyp = hypot(loc.x/ratioX -v.uv.x, loc.y/ratioY -v.uv.y)
if (hyp < min):
min = hyp
minV = v
MakeUvFaceEqualRectangle(vertsDict, leftUp, rightUp, rightDown, leftDown, minV)
def MakeUvFaceEqualRectangle(vertsDict, leftUp, rightUp, rightDown, leftDown, startv):
ratioX, ratioY = ImageRatio()
ratio = ratioX/ratioY
if startv is None: startv = leftUp.uv
elif AreVertsQuasiEqual(startv, rightUp): startv = rightUp.uv
elif AreVertsQuasiEqual(startv, rightDown): startv = rightDown.uv
elif AreVertsQuasiEqual(startv, leftDown): startv = leftDown.uv
else: startv = leftUp.uv
leftUp = leftUp.uv
rightUp = rightUp.uv
rightDown = rightDown.uv
leftDown = leftDown.uv
if (startv == leftUp):
finalScaleX = hypotVert(leftUp, rightUp)
finalScaleY = hypotVert(leftUp, leftDown)
currRowX = leftUp.x
currRowY = leftUp.y
elif (startv == rightUp):
finalScaleX = hypotVert(rightUp, leftUp)
finalScaleY = hypotVert(rightUp, rightDown)
currRowX = rightUp.x - finalScaleX
currRowY = rightUp.y
elif (startv == rightDown):
finalScaleX = hypotVert(rightDown, leftDown)
finalScaleY = hypotVert(rightDown, rightUp)
currRowX = rightDown.x - finalScaleX
currRowY = rightDown.y + finalScaleY
else:
finalScaleX = hypotVert(leftDown, rightDown)
finalScaleY = hypotVert(leftDown, leftUp)
currRowX = leftDown.x
currRowY = leftDown.y +finalScaleY
#leftUp, rightUp
x = round(leftUp.x, precision)
y = round(leftUp.y, precision)
for v in vertsDict[(x,y)]:
v.uv.x = currRowX
v.uv.y = currRowY
x = round(rightUp.x, precision)
y = round(rightUp.y, precision)
for v in vertsDict[(x,y)]:
v.uv.x = currRowX + finalScaleX
v.uv.y = currRowY
#rightDown, leftDown
x = round(rightDown.x, precision)
y = round(rightDown.y, precision)
for v in vertsDict[(x,y)]:
v.uv.x = currRowX + finalScaleX
v.uv.y = currRowY - finalScaleY
x = round(leftDown.x, precision)
y = round(leftDown.y, precision)
for v in vertsDict[(x,y)]:
v.uv.x = currRowX
v.uv.y = currRowY - finalScaleY
def FollowActiveUV(me, f_act, faces):
bm = bmesh.from_edit_mesh(me)
uv_act = bm.loops.layers.uv.active
# our own local walker
def walk_face_init(faces, f_act):
# first tag all faces True (so we dont uvmap them)
for f in bm.faces:
f.tag = True
# then tag faces arg False
for f in faces:
f.tag = False
# tag the active face True since we begin there
f_act.tag = True
def walk_face(f):
# all faces in this list must be tagged
f.tag = True
faces_a = [f]
faces_b = []
while faces_a:
for f in faces_a:
for l in f.loops:
l_edge = l.edge
if l_edge.is_manifold == True and l_edge.seam == False:
l_other = l.link_loop_radial_next
f_other = l_other.face
if not f_other.tag:
yield (f, l, f_other)
f_other.tag = True
faces_b.append(f_other)
# swap
faces_a, faces_b = faces_b, faces_a
faces_b.clear()
def walk_edgeloop(l):
"""
Could make this a generic function
"""
e_first = l.edge
e = None
while True:
e = l.edge
yield e
# don't step past non-manifold edges
if e.is_manifold:
# walk around the quad and then onto the next face
l = l.link_loop_radial_next
if len(l.face.verts) == 4:
l = l.link_loop_next.link_loop_next
if l.edge is e_first:
break
else:
break
else:
break
def extrapolate_uv(fac,
l_a_outer, l_a_inner,
l_b_outer, l_b_inner):
l_b_inner[:] = l_a_inner
l_b_outer[:] = l_a_inner + ((l_a_inner - l_a_outer) * fac)
def apply_uv(f_prev, l_prev, f_next):
l_a = [None, None, None, None]
l_b = [None, None, None, None]
l_a[0] = l_prev
l_a[1] = l_a[0].link_loop_next
l_a[2] = l_a[1].link_loop_next
l_a[3] = l_a[2].link_loop_next
# l_b
# +-----------+
# |(3) |(2)
# | |
# |l_next(0) |(1)
# +-----------+
# ^
# l_a |
# +-----------+
# |l_prev(0) |(1)
# | (f) |
# |(3) |(2)
# +-----------+
# copy from this face to the one above.
# get the other loops
l_next = l_prev.link_loop_radial_next
if l_next.vert != l_prev.vert:
l_b[1] = l_next
l_b[0] = l_b[1].link_loop_next
l_b[3] = l_b[0].link_loop_next
l_b[2] = l_b[3].link_loop_next
else:
l_b[0] = l_next
l_b[1] = l_b[0].link_loop_next
l_b[2] = l_b[1].link_loop_next
l_b[3] = l_b[2].link_loop_next
l_a_uv = [l[uv_act].uv for l in l_a]
l_b_uv = [l[uv_act].uv for l in l_b]
try:
fac = edge_lengths[l_b[2].edge.index][0] / edge_lengths[l_a[1].edge.index][0]
except ZeroDivisionError:
fac = 1.0
extrapolate_uv(fac,
l_a_uv[3], l_a_uv[0],
l_b_uv[3], l_b_uv[0])
extrapolate_uv(fac,
l_a_uv[2], l_a_uv[1],
l_b_uv[2], l_b_uv[1])
# Calculate average length per loop if needed
bm.edges.index_update()
edge_lengths = [None]*len(bm.edges)
for f in faces:
# we know its a quad
l_quad = f.loops[:]
l_pair_a = (l_quad[0], l_quad[2])
l_pair_b = (l_quad[1], l_quad[3])
for l_pair in (l_pair_a, l_pair_b):
if edge_lengths[l_pair[0].edge.index] is None:
edge_length_store = [-1.0]
edge_length_accum = 0.0
edge_length_total = 0
for l in l_pair:
if edge_lengths[l.edge.index] is None:
for e in walk_edgeloop(l):
if edge_lengths[e.index] is None:
edge_lengths[e.index] = edge_length_store
edge_length_accum += e.calc_length()
edge_length_total += 1
edge_length_store[0] = edge_length_accum / edge_length_total
walk_face_init(faces, f_act)
for f_triple in walk_face(f_act):
apply_uv(*f_triple)
def ImageRatio():
ratioX, ratioY = 256,256
for a in bpy.context.screen.areas:
if a.type == 'IMAGE_EDITOR':
img = a.spaces[0].image
if img and img.size[0] != 0:
ratioX, ratioY = img.size[0], img.size[1]
break
return ratioX, ratioY
def AreVertsQuasiEqual(v1, v2, allowedError = 0.00001):
if abs(v1.uv.x -v2.uv.x) < allowedError and abs(v1.uv.y -v2.uv.y) < allowedError:
return True
return False
def hypotVert(v1, v2):
hyp = hypot(v1.x - v2.x, v1.y - v2.y)
return hyp