-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperators.py
969 lines (745 loc) · 34 KB
/
operators.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
import typing
from typing import List, Set
import bpy
from bpy.props import FloatProperty, BoolProperty, IntProperty
import bmesh
from bpy.types import Context, Event
from .uv import *
from .bbox import BBoxUV
expand_modes = (
("CONTINUOS", "Continous", ""),
("EXPAND", "Expand", "Expand by one edge"),
("SHRINK", "Shrink", "Shrink by one edge"),
)
align_directions = (
("X", "X", "Align in X direction"),
("Y", "Y", "Align in Y direction"),
("AUTO", "Auto", "Calculate align direction based on bounds"),
)
align_modes = (
("AVERAGE", "Average", ""),
("MIN", "Minimun", ""),
("MAX", "Maximum", ""),
)
straighten_modes = (
("GEOMETRY", "Geometry", ""),
("EVEN", "Even", ""),
("PROJECT", "Project", ""),
)
unwrap_modes = (
("ANGLE_BASED", "Angle based", ""),
("CONFORMAL", "Conformal", ""),
)
def is_uv_edit_mode():
if not bpy.context.active_object:
return False
if bpy.context.active_object.type != 'MESH':
return False
if bpy.context.active_object.mode != 'EDIT':
return False
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
return True
class UV_OT_uvkit_select_uv_edgeloop(bpy.types.Operator):
bl_idname = "view2d.uvkit_select_uv_edgeloop"
bl_label = "uvkit select uv edge loop"
bl_options = {"REGISTER", "UNDO"}
bl_description = "Select uv edgeloop"
mode: bpy.props.EnumProperty(name="Mode", items=expand_modes)
@classmethod
def poll(cls, context):
return is_uv_edit_mode()
def execute(self, context):
# print("#" * 66)
for obj in context.selected_objects:
if obj.mode == "EDIT" and obj.type == "MESH":
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
selected_uv_loops = get_selected_uv_edge_loops(bm, uv_layer)
if self.mode == "CONTINUOS":
edge_loops = find_uv_edgeloops(selected_uv_loops, uv_layer)
# print(f"number of edgeloops: {len(edge_loops)}")
for edgeloop in edge_loops:
select_uv_edgeloop(edgeloop, uv_layer)
elif self.mode == 'EXPAND':
edge_loops = find_uv_edgeloops(
selected_uv_loops, uv_layer, constrain_by_selected=True
)
# print(f"number of edgeloops: {len(edge_loops)}")
for edgeloop in edge_loops:
expand_uv_edgeloop(edgeloop, uv_layer)
for edgeloop in edge_loops:
select_uv_edgeloop(edgeloop, uv_layer)
elif self.mode == 'SHRINK':
edge_loops = find_uv_edgeloops(
selected_uv_loops, uv_layer, constrain_by_selected=True
)
# print(f"number of edgeloops: {len(edge_loops)}")
for edgeloop in edge_loops:
shrink_uv_edgeloop(edgeloop, uv_layer)
bmesh.update_edit_mesh(obj.data)
return {"FINISHED"}
def invoke(self, context, event):
self.execute(context)
return {"FINISHED"}
class UV_OT_uvkit_select_uv_edgering(bpy.types.Operator):
bl_idname = "view2d.uvkit_select_uv_edgering"
bl_label = "uvkit select uv edge ring"
bl_options = {"REGISTER", "UNDO"}
bl_description = "Select UV edgering"
mode: bpy.props.EnumProperty(name="Mode", items=expand_modes)
@classmethod
def poll(cls, context):
return is_uv_edit_mode()
def execute(self, context):
# print("#" * 66)
for obj in context.selected_objects:
if obj.mode == "EDIT" and obj.type == "MESH":
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
selected_uv_loops = get_selected_uv_edge_loops(bm, uv_layer)
if self.mode == "CONTINUOS":
edge_rings = find_uv_edgerings(
selected_uv_loops, uv_layer, constrain_by_selected=False
)
for edge_ring in edge_rings:
select_uv_edgering(edge_ring, uv_layer)
elif self.mode == "EXPAND":
edge_rings = find_uv_edgerings(
selected_uv_loops, uv_layer, constrain_by_selected=True
)
for edge_ring in edge_rings:
expand_uv_edgering(edge_ring, uv_layer)
for edge_ring in edge_rings:
select_uv_edgering(edge_ring, uv_layer)
elif self.mode == "SHRINK":
edge_rings = find_uv_edgerings(
selected_uv_loops, uv_layer, constrain_by_selected=True
)
for edgering in edge_rings:
shrink_uv_edgering(edgering, uv_layer)
bmesh.update_edit_mesh(obj.data)
return {"FINISHED"}
def invoke(self, context, event):
self.execute(context)
return {"FINISHED"}
class UV_OT_uvkit_align_uv_edgeloops(bpy.types.Operator):
bl_idname = "view2d.uvkit_align_uv_edgeloops"
bl_label = "uvkit align edge loops"
bl_options = {"REGISTER", "UNDO"}
bl_description = """Aligns uv edgeloops
SHIFT - align to max value.
ALT - align to min value.
CTRL - use global values"""
apply_per_edgeloop: bpy.props.BoolProperty(name="Apply per loop", default=True)
direction: bpy.props.EnumProperty(name="Direction", items=align_directions)
mode: bpy.props.EnumProperty(name="Mode", items=align_modes)
@classmethod
def poll(cls, context):
return is_uv_edit_mode()
def execute(self, context):
# print( "#" * 66)
class Part:
def __init__(self):
self.mesh = None
self.bm = None
self.uv_layer = None
self.edge_loops = []
self.bounding_boxes = []
self.directions = []
parts = []
for obj in context.selected_objects:
if obj.mode != "EDIT" or obj.type != "MESH":
continue
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
selected_uv_loops = get_selected_uv_edge_loops(bm, uv_layer)
edge_loops = find_uv_edgeloops(
selected_uv_loops, uv_layer, constrain_by_selected=True
)
# add "other" vert from end
for edge_loop in edge_loops:
edge_loop.append(edge_loop[-1].link_loop_next)
part = Part()
parts.append(part)
part.mesh = obj.data
part.bm = bm
part.uv_layer = uv_layer
part.edge_loops = edge_loops
for edge_loop in edge_loops:
bbox = BBoxUV(edge_loop, uv_layer)
direction = self.direction
# AUTO direction - need find out alignment
if self.direction == "AUTO":
direction = "X"
if bbox.diagonal.y < bbox.diagonal.x:
direction = "Y"
part.bounding_boxes.append(bbox)
part.directions.append(direction)
global_bbox = BBoxUV()
if not self.apply_per_edgeloop:
for part in parts:
for bbox in part.bounding_boxes:
global_bbox.merge(bbox)
for part in parts:
bm = part.bm
uv_layer = part.uv_layer
for i, edge_loop in enumerate(part.edge_loops):
connected_uv_verts = []
for loop in edge_loop:
for connected in loop.vert.link_loops:
if not connected.face.select:
continue
a = loop[uv_layer].uv
b = connected[uv_layer].uv
if is_same_uv_location(a, b):
connected_uv_verts.append(connected)
if self.apply_per_edgeloop:
current_bbox = part.bounding_boxes[i]
else:
current_bbox = global_bbox
for loop in connected_uv_verts:
if part.directions[i] == "X":
if self.mode == "AVERAGE":
loop[uv_layer].uv.x = current_bbox.average.x
elif self.mode == "MAX":
loop[uv_layer].uv.x = current_bbox.max.x
elif self.mode == "MIN":
loop[uv_layer].uv.x = current_bbox.min.x
elif part.directions[i] == "Y":
if self.mode == "AVERAGE":
loop[uv_layer].uv.y = current_bbox.average.y
elif self.mode == "MAX":
loop[uv_layer].uv.y = current_bbox.max.y
elif self.mode == "MIN":
loop[uv_layer].uv.y = current_bbox.min.y
bmesh.update_edit_mesh(part.mesh)
return {"FINISHED"}
def invoke(self, context, event):
self.mode = align_modes[0][0]
if event.shift:
self.mode = align_modes[2][0]
elif event.alt:
self.mode = align_modes[1][0]
if event.ctrl:
self.apply_per_edgeloop = False
else:
self.apply_per_edgeloop = True
self.execute(context)
return {"FINISHED"}
class UV_OT_uvkit_spread_loop(bpy.types.Operator):
bl_idname = "view2d.uvkit_spread_loop"
bl_label = "uvkit spread loop"
bl_options = {"REGISTER", "UNDO"}
bl_description = "Straightens uv edgeloops"
mode: bpy.props.EnumProperty(name="Mode", items=straighten_modes)
@classmethod
def poll(cls, context):
return is_uv_edit_mode()
def execute(self, context):
# print("#"*66)
for obj in context.selected_objects:
if obj.mode == "EDIT" and obj.type == "MESH":
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
selected_uv_loops = get_selected_uv_edge_loops(bm, uv_layer)
edge_loops = find_uv_edgeloops(
selected_uv_loops, uv_layer, constrain_by_selected=True
)
# add "other" vert from end
for edge_loop in edge_loops:
edge_loop.append(edge_loop[-1].link_loop_next)
for edge_loop in edge_loops:
bbox = BBoxUV(edge_loop, uv_layer)
uv_distance = bbox.diagonal.length
distances = []
for i in range(1, len(edge_loop)):
a = edge_loop[i - 1]
b = edge_loop[i]
c = b.vert.co - a.vert.co
distances.append(c.length)
total_distance = sum(distances)
first_loop_uv = edge_loop[0][uv_layer].uv
last_loop_uv = edge_loop[-1][uv_layer].uv
direction = (last_loop_uv - first_loop_uv).normalized()
uv_vectors = []
for loop in edge_loop:
uv_vectors.append(loop[uv_layer].uv - first_loop_uv)
for i in range(1, len(edge_loop) - 1):
a = edge_loop[i - 1]
b = edge_loop[i]
a_uv = a[uv_layer]
b_uv = b[uv_layer]
target_pos = b_uv.uv.to_2d()
if self.mode == "GEOMETRY":
distance = (distances[i - 1] * uv_distance) / total_distance
target_pos = a_uv.uv + direction * distance
elif self.mode == "EVEN":
distance = uv_distance / (len(edge_loop) - 1)
target_pos = a_uv.uv + direction * distance
elif self.mode == "PROJECT":
vector = uv_vectors[i]
projection = vector.dot(direction)
target_pos = (
edge_loop[0][uv_layer].uv + direction * projection
)
# need to store the pos, as b_uv could be moved around in following code
b_pos = b_uv.uv.to_2d()
for connected in b.vert.link_loops:
if not connected.face.select:
continue
c_uv = connected[uv_layer]
if is_same_uv_location(c_uv.uv, b_pos):
c_uv.uv = target_pos
bmesh.update_edit_mesh(obj.data)
return {"FINISHED"}
def invoke(self, context, event):
self.execute(context)
return {"FINISHED"}
class UV_OT_uvkit_constrained_unwrap(bpy.types.Operator):
bl_idname = "view2d.uvkit_constrained_unwrap"
bl_label = "uvkit constrained unwrap"
bl_options = {"REGISTER", "UNDO"}
bl_description = """Keeps the selected uv's in place/pinned and unwraps the unselected rest of the uv island.
SHIFT - ignore seams.
CTRL - ignore pins.
ALT - ignore seams and pins"""
mode: bpy.props.EnumProperty(name="Mode", items=unwrap_modes)
ignore_seams: bpy.props.BoolProperty(name="Ignore edge seams", default=False)
ignore_pins: bpy.props.BoolProperty(name="Ignore pinned uv", default=False)
@classmethod
def poll(cls, context):
return is_uv_edit_mode()
def execute(self, context):
for obj in context.selected_objects:
if obj.mode == "EDIT" and obj.type == "MESH":
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
selected_uv_loops = get_selected_uv_vert_loops(bm, uv_layer)
uv_islands = find_uv_islands_for_selected_uv_loops(bm, uv_layer)
pinned_uvs = set()
seam_edges = set()
for island in uv_islands:
for loop in island:
for connected in loop.face.loops:
if not connected.face.select:
continue
connected[uv_layer].select = True
connected[uv_layer].select_edge = False
if self.ignore_pins:
if connected[uv_layer].pin_uv:
pinned_uvs.add(connected)
connected[uv_layer].pin_uv = False
if self.ignore_seams:
if connected.edge.seam:
seam_edges.add(connected.edge)
connected.edge.seam = False
for selected_uv_loop in selected_uv_loops:
selected_uv_loop[uv_layer].select = False
if selected_uv_loop[uv_layer].pin_uv:
pinned_uvs.add(selected_uv_loop)
selected_uv_loop[uv_layer].pin_uv = True
for connected in selected_uv_loop.vert.link_loops:
if connected.face.select:
connected[uv_layer].select = False
if connected[uv_layer].pin_uv:
pinned_uvs.add(connected)
connected[uv_layer].pin_uv = True
if self.mode == "ANGLE_BASED":
bpy.ops.uv.unwrap(method="ANGLE_BASED")
elif self.mode == "CONFORMAL":
bpy.ops.uv.unwrap(method="CONFORMAL")
for island in uv_islands:
for loop in island:
for connected in loop.face.loops:
connected[uv_layer].select = False
connected[uv_layer].select_edge = False
for edge in seam_edges:
edge.seam = True
for selected_uv_loop in selected_uv_loops:
selected_uv_loop[uv_layer].select = True
selected_uv_loop[uv_layer].pin_uv = False
for pinned_uv in pinned_uvs:
pinned_uv[uv_layer].pin_uv = False
for selected_uv_loop in selected_uv_loops:
selected_uv_loop[uv_layer].select_edge = selected_uv_loop.link_loop_next[uv_layer].select
bmesh.update_edit_mesh(obj.data)
return {"FINISHED"}
def invoke(self, context, event):
if event.shift:
self.ignore_seams = True
elif event.ctrl:
self.ignore_pins = True
elif event.alt:
self.ignore_seams = True
self.ignore_pins = True
else:
self.ignore_seams = False
self.ignore_pins = False
self.execute(context)
return {"FINISHED"}
from math import radians
from bpy_extras.object_utils import object_data_add
class UV_OT_uvkit_trim_unwrap(bpy.types.Operator):
bl_idname = "view2d.uvkit_trim_unwrap"
bl_label = "uvkit trim unwrap"
bl_options = {"REGISTER", "UNDO"}
bl_description = """
"""
@classmethod
def poll(cls, context):
return is_uv_edit_mode()
def execute(self, context):
from .bpypolyskel.bpyeuclid import Edge2
from .bpypolyskel.bpypolyskel import skeletonize
for object_original in context.selected_objects:
if object_original.mode != "EDIT" and obj.type != "MESH":
continue
bpy.context.view_layer.objects.active = object_original
# make working copy
bpy.ops.object.editmode_toggle()
bpy.ops.object.duplicate()
#bpy.ops.object.editmode_toggle()
deform_source = bpy.context.view_layer.objects.active
deform_source.name = "DeformSource"
deform_source.modifiers.clear()
deform_mesh = deform_source.data
bm = bmesh.new()
bm.from_mesh(deform_mesh)
#region Preprocess
# delete unselected faces
unselected_faces = []
for face in bm.faces:
if not face.select:
unselected_faces.append(face)
bmesh.ops.delete(bm, geom=unselected_faces, context='FACES')
# unwrap and get rid of inner edges
# should result in one big polygon
seams = []
inner_edges = []
for edge in bm.edges:
if edge.seam:
seams.append(edge)
if not edge.is_boundary and not edge.seam:
inner_edges.append(edge)
bmesh.ops.split_edges(bm, edges=seams)
bm.to_mesh(deform_mesh)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.01)
bpy.ops.object.mode_set(mode='OBJECT')
pre_processed_bm = bm.copy()
deform_target_mesh = bpy.data.meshes.new(name="DeformTargetMesh")
deform_target_obj = object_data_add(context, deform_target_mesh, name='DeformTarget')
uv_layer = pre_processed_bm.loops.layers.uv.verify()
for face in pre_processed_bm.faces:
for loop in face.loops:
loop.vert.co = loop[uv_layer].uv.to_3d()
pre_processed_bm.to_mesh(deform_target_mesh)
#endregion Preprocess
#region Skeleton
bmesh.ops.dissolve_edges(bm, edges=inner_edges)
bm.faces.ensure_lookup_table()
bm.verts.ensure_lookup_table()
# bmesh will keep extra edges and thus faces if it has to maintain a hole
uv_shell_has_hole = len(bm.faces) > 1
if uv_shell_has_hole:
print("hole detected")
continue
uv_layer = bm.loops.layers.uv.verify()
for face in bm.faces:
for loop in face.loops:
loop.vert.co = loop[uv_layer].uv.to_3d()
bmesh.ops.dissolve_limit(bm, angle_limit=radians(10.0), edges=bm.edges)
min_edge_length = 10000000000
for edge in bm.edges:
min_edge_length = min(min_edge_length, (edge.verts[0].co - edge.verts[1].co).length)
contour_verts = []
edges = []
for loop in bm.faces[0].loops:
edges.append(Edge2(loop.vert.co, loop.link_loop_next.vert.co))
contour_verts.append(loop.vert.co)
edge_contours = [edges]
skeleton = skeletonize(edge_contours)
bmesh.ops.delete(bm, geom=bm.faces, context='FACES')
skeleton_positions = []
verts = []
for subtree in skeleton:
vert1 = bm.verts.new(subtree.source.to_3d())
verts.append(vert1)
skeleton_positions.append(subtree.source)
for link in subtree.sinks:
vert2 = bm.verts.new(link.to_3d())
e = bm.edges.new((vert1, vert2))
# for index_A, subtree in enumerate(skeleton):
# for link in subtree.sinks:
# for index_B, position in enumerate(skeleton_positions):
# if (position - link).length < 0.001:
# vert1 = verts[index_A]
# vert2 = verts[index_B]
# e = bm.edges.new((vert1, vert2))
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=min_edge_length*0.99)
bm.to_mesh(deform_mesh)
#endregion Skeleton
#mod = deform_target_obj.modifiers.new("Surface Deform", type='SURFACE_DEFORM')
#mod.target = deform_source
return {"FINISHED"}
def invoke(self, context, event):
self.execute(context)
return {"FINISHED"}
class UV_OT_uvkit_show_image(bpy.types.Operator):
# heavily copied from Reinier Goijvaerts
bl_idname = "view2d.uvkit_show_image"
bl_label = "Show Image"
bl_options = {"INTERNAL"}
image_name: bpy.props.StringProperty(name='image_name', default='')
def execute(self, context):
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
for image in bpy.data.images:
if image.name == self.image_name:
area.spaces.active.image = image
return {'FINISHED'}
class UV_OT_uvkit_align(bpy.types.Operator):
bl_idname = "view2d.uvkit_align"
bl_label = "Align"
bl_options = {"REGISTER", "UNDO"}
bl_description = """Aligns the selection or the Cursor.
SHIFT - move island instead of align selection
CTRL - use 0-1 space as boundary.
ALT - set the Cursor"""
move_island: bpy.props.BoolProperty(name="Move Island", default=False)
direction: bpy.props.StringProperty(name='Direction', default='center')
use_unit_square: bpy.props.BoolProperty(name="Use 0-1 space", default=False)
set_cursor: bpy.props.BoolProperty(name="Set Cursor", default=False)
def execute(self, context: Context) -> Set[str]:
class Part:
def __init__(self):
self.mesh = None
self.bm = None
self.uv_layer = None
self.selected_uvs = []
self.uv_islands = []
self.uv_islands_bounds = []
global_bbox = BBoxUV()
parts = []
has_uv_selection = False
# selection_mode = context.scene.tool_settings.uv_select_mode
# face_or_island_selection_mode = selection_mode == 'FACE' or selection_mode == 'ISLAND'
for obj in context.selected_objects:
if obj.mode != "EDIT" or obj.type != "MESH":
continue
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
selected_uv_loops = get_selected_uv_vert_loops(bm, uv_layer)
if not has_uv_selection and len(selected_uv_loops) > 0:
has_uv_selection = True
uv_islands: List[bmesh.types.BMLoop] = []
uv_islands_bounds: List[BBoxUV] = []
if self.move_island:
uv_islands = find_uv_islands_for_selected_uv_loops(bm, uv_layer)
print(f"islands: {len(uv_islands)}")
for island in uv_islands:
uv_islands_bounds.append(BBoxUV(island, uv_layer))
part = Part()
part.mesh = obj.data
part.bm = bm
part.uv_layer = uv_layer
part.selected_uvs = selected_uv_loops
part.uv_islands = uv_islands
part.uv_islands_bounds = uv_islands_bounds
parts.append(part)
if self.move_island:
for bbox in part.uv_islands_bounds:
global_bbox.merge(bbox)
else:
global_bbox.merge(BBoxUV(selected_uv_loops, uv_layer))
if not has_uv_selection:
return {"FINISHED"}
if self.use_unit_square:
global_bbox.set_to_unit_square()
if self.set_cursor:
space_data = context.space_data
space_data.cursor_location = global_bbox.get_location(self.direction)
return {"FINISHED"}
for part in parts:
bm = part.bm
uv_layer = part.uv_layer
if self.move_island:
for i, island in enumerate(part.uv_islands):
island_bbox: BBoxUV = part.uv_islands_bounds[i]
if self.direction == 'left':
delta = global_bbox.left - island_bbox.left
delta.y = 0
elif self.direction == 'topleft':
delta = global_bbox.topleft - island_bbox.topleft
elif self.direction == 'top':
delta = global_bbox.top - island_bbox.top
delta.x = 0
elif self.direction == 'topright':
delta = global_bbox.topright - island_bbox.topright
elif self.direction == 'right':
delta = global_bbox.right - island_bbox.right
delta.y = 0
elif self.direction == 'bottomright':
delta = global_bbox.bottomright - island_bbox.bottomright
elif self.direction == 'bottom':
delta = global_bbox.bottom - island_bbox.bottom
delta.x = 0
elif self.direction == 'bottomleft':
delta = global_bbox.bottomleft - island_bbox.bottomleft
elif self.direction == 'center':
delta = global_bbox.center - island_bbox.center
elif self.direction == 'horizontal':
delta = global_bbox.center - island_bbox.center
delta.y = 0
elif self.direction == 'vertical':
delta = global_bbox.center - island_bbox.center
delta.x = 0
for loop in island:
loop[uv_layer].uv += delta
else:
for loop in part.selected_uvs:
if self.direction == 'left':
loop[uv_layer].uv.x = global_bbox.get_location(self.direction).x
elif self.direction == 'topleft':
loop[uv_layer].uv = global_bbox.get_location(self.direction)
elif self.direction == 'top':
loop[uv_layer].uv.y = global_bbox.get_location(self.direction).y
elif self.direction == 'topright':
loop[uv_layer].uv = global_bbox.get_location(self.direction)
elif self.direction == 'right':
loop[uv_layer].uv.x = global_bbox.get_location(self.direction).x
elif self.direction == 'bottomright':
loop[uv_layer].uv = global_bbox.get_location(self.direction)
elif self.direction == 'bottom':
loop[uv_layer].uv.y = global_bbox.get_location(self.direction).y
elif self.direction == 'bottomleft':
loop[uv_layer].uv = global_bbox.get_location(self.direction)
elif self.direction == 'center':
loop[uv_layer].uv = global_bbox.get_location(self.direction)
elif self.direction == 'horizontal':
loop[uv_layer].uv.y = global_bbox.get_location(self.direction).y
elif self.direction == 'vertical':
loop[uv_layer].uv.x = global_bbox.get_location(self.direction).x
bmesh.update_edit_mesh(part.mesh)
return {"FINISHED"}
def invoke(self, context: Context, event: Event) -> Set[str]:
self.set_cursor = False
self.move_island = False
self.use_unit_square = False
if event.alt:
self.set_cursor = True
else:
if event.shift:
self.move_island = True
if event.ctrl:
self.use_unit_square = True
return self.execute(context)
class UV_OT_uvkit_rotate_shell(bpy.types.Operator):
bl_idname = "view2d.uvkit_rotate_shell"
bl_label = "Rotate 90 degrees"
bl_options = {'REGISTER', 'UNDO'}
bl_description = """Rotate the selected UVs 90 degrees left or right
SHIFT - rotate Island.
CTRL - rotate around individual origins.
ALT - rotate around 2D Cursor"""
angle: bpy.props.FloatProperty(name="Angle", subtype='ANGLE')
use_individual_origin: bpy.props.BoolProperty(name="Use Boundingbox Center", default=False)
use_cursor: bpy.props.BoolProperty(name="Use Cursor", default=False)
use_island: bpy.props.BoolProperty(name="Use Island", default=False)
@classmethod
def poll(cls, context):
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
if not bpy.context.active_object:
return False
if bpy.context.active_object.type != 'MESH':
return False
if bpy.context.active_object.mode != 'EDIT':
return False
if not bpy.context.object.data.uv_layers:
return False
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
return True
def execute(self, context):
class Part():
def __init__(self):
self.mesh = None
self.bm = None
self.uv_layer = None
self.selected = []
# remember initial pivot point setting
intial_pivot_point = context.space_data.pivot_point
context.space_data.pivot_point = "CENTER"
if self.use_cursor:
context.space_data.pivot_point = 'CURSOR'
elif self.use_individual_origin:
context.space_data.pivot_point = 'INDIVIDUAL_ORIGINS'
# store selected uv loops
parts = []
if self.use_island:
for obj in context.selected_objects:
if obj.mode != "EDIT" or obj.type != "MESH":
continue
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
part = Part()
part.mesh = obj.data
part.bm = bm
part.uv_layer = uv_layer
part.selected = get_selected_uv_vert_loops(bm, uv_layer)
parts.append(part)
bpy.ops.uv.select_linked()
bpy.ops.transform.rotate(value=self.angle, orient_axis='Z', constraint_axis=(False, False, False), use_proportional_edit=False)
context.space_data.pivot_point = intial_pivot_point
if self.use_island:
bpy.ops.uv.select_all(action='DESELECT')
# regenerate the selection state
for part in parts:
bm = part.bm
uv_layer = part.uv_layer
for loop in part.selected:
loop[uv_layer].select = True
bmesh.update_edit_mesh(part.mesh)
# flush selection... bit ugly..
uv_selection_mode = context.tool_settings.uv_select_mode
bpy.ops.uv.select_mode(type='VERTEX')
bpy.ops.uv.select_mode(type=uv_selection_mode)
return {'FINISHED'}
def invoke(self, context: Context, event: Event):
self.use_individual_origin = False
self.use_cursor = False
self.use_island = False
if event.ctrl:
self.use_individual_origin = True
elif event.alt:
self.use_cursor = True
if event.shift:
self.use_island = True
return self.execute(context)
# -------------------------------------------------------------------
# Register & Unregister
# -------------------------------------------------------------------
classes = [
UV_OT_uvkit_spread_loop,
UV_OT_uvkit_select_uv_edgering,
UV_OT_uvkit_select_uv_edgeloop,
UV_OT_uvkit_align_uv_edgeloops,
UV_OT_uvkit_constrained_unwrap,
UV_OT_uvkit_trim_unwrap,
UV_OT_uvkit_show_image,
UV_OT_uvkit_align,
UV_OT_uvkit_rotate_shell,
]
def register():
import importlib
from . import bbox
importlib.reload(bbox)
from . import uv
importlib.reload(uv)
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in reversed(classes):
bpy.utils.unregister_class(c)