summaryrefslogtreecommitdiffstats
path: root/BakeVertexData.py
blob: 87249674a80e592868c85e346243921293e0fd16 (plain)
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
bl_info = {
    "name": "Bake Vertex to Target Vector",
    "blender": (3, 0, 0),
    "category": "Mesh",
    "version": (2, 3, 0),
    "author": "yum_food",
    "description": "Bake vertex vectors with automatic center and scale calculation in Edit Mode, with submesh deduplication"
}

import bpy
import mathutils
import bmesh
import math
from bpy.props import BoolProperty, FloatProperty
from bpy.types import Panel, Operator


class MESH_OT_bake_vertex_vectors(Operator):
    bl_idname = "mesh.bake_vertex_vectors"
    bl_label = "Bake Vertex Vectors"
    bl_description = "Bake selected vertices with automatic center and scale calculation"
    bl_options = {'REGISTER', 'UNDO'}
    
    contiguous_mode: BoolProperty(
        name="Contiguous Groups",
        description="Process each contiguous group of vertices separately with its own center and scale",
        default=False
    )
    
    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj is not None and
                obj.type == 'MESH' and
                context.mode == 'EDIT_MESH')
    
    def get_vertex_islands(self, mesh, selected_indices):
        """Find contiguous groups of vertices using edge connectivity"""
        adjacency = {idx: set() for idx in selected_indices}
        
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            if v0 in selected_indices and v1 in selected_indices:
                adjacency[v0].add(v1)
                adjacency[v1].add(v0)
        
        islands = []
        visited = set()
        
        for start_idx in selected_indices:
            if start_idx in visited:
                continue
                
            island = set()
            queue = [start_idx]
            
            while queue:
                current = queue.pop(0)
                if current in visited:
                    continue
                    
                visited.add(current)
                island.add(current)
                
                for neighbor in adjacency[current]:
                    if neighbor not in visited:
                        queue.append(neighbor)
            
            islands.append(island)
        
        return islands
    
    def get_island_info(self, mesh, island_indices):
        """Calculate center and scale for a single island"""
        verts = []
        for idx in island_indices:
            verts.append(mesh.vertices[idx].co)
        
        if not verts:
            return None, None, None
        
        center = mathutils.Vector((0, 0, 0))
        for co in verts:
            center += co
        center /= len(verts)
        
        max_dist = 0.0
        for co in verts:
            vec = co - center
            max_component = max(abs(vec.x), abs(vec.y), abs(vec.z))
            max_dist = max(max_dist, max_component)
        
        if max_dist > 0:
            scale = 1.0 / max_dist
        else:
            scale = 1.0
        
        return center, max_dist, scale
    
    def execute(self, context):
        obj = context.active_object
        mesh = obj.data
        
        try:
            bpy.ops.object.mode_set(mode='OBJECT')
        except Exception as e:
            self.report({'ERROR'}, f"Failed to switch to object mode: {str(e)}")
            return {'CANCELLED'}
        
        selected_indices = {v.index for v in mesh.vertices if v.select}
        
        if not selected_indices:
            self.report({'WARNING'}, "No vertices selected")
            try:
                bpy.ops.object.mode_set(mode='EDIT')
            except:
                pass
            return {'CANCELLED'}
        
        # Check if mesh has faces
        if not mesh.polygons:
            self.report({'ERROR'}, "Mesh has no faces. Vertex colors require faces to store data.")
            try:
                bpy.ops.object.mode_set(mode='EDIT')
            except:
                pass
            return {'CANCELLED'}
        
        if not mesh.vertex_colors:
            mesh.vertex_colors.new(name="BakedVectors")
        
        color_layer = mesh.vertex_colors.active
        if not color_layer:
            self.report({'ERROR'}, "Failed to create vertex color layer")
            try:
                bpy.ops.object.mode_set(mode='EDIT')
            except:
                pass
            return {'CANCELLED'}
        
        source_matrix = obj.matrix_world
        
        if self.contiguous_mode:
            # Build vertex to polygon mapping for efficiency
            vertex_to_polys = {}
            for poly_idx, poly in enumerate(mesh.polygons):
                for vertex_idx in poly.vertices:
                    if vertex_idx in selected_indices:
                        if vertex_idx not in vertex_to_polys:
                            vertex_to_polys[vertex_idx] = []
                        vertex_to_polys[vertex_idx].append(poly_idx)
            
            islands = self.get_vertex_islands(mesh, selected_indices)
            total_updated = 0
            
            for island_indices in islands:
                center, max_dist, scale = self.get_island_info(mesh, island_indices)
                if center is None:
                    continue
                
                center_world = source_matrix @ center
                
                # Collect all polygons that contain vertices from this island
                relevant_polys = set()
                for vertex_idx in island_indices:
                    if vertex_idx in vertex_to_polys:
                        relevant_polys.update(vertex_to_polys[vertex_idx])
                
                # Only process relevant polygons
                for poly_idx in relevant_polys:
                    poly = mesh.polygons[poly_idx]
                    for loop_idx in poly.loop_indices:
                        vertex_idx = mesh.loops[loop_idx].vertex_index
                        
                        if vertex_idx in island_indices:
                            vertex = mesh.vertices[vertex_idx]
                            
                            vertex_world = source_matrix @ vertex.co
                            vector_world = center_world - vertex_world
                            vector_object = source_matrix.inverted_safe().to_3x3() @ vector_world
                            vector_scaled = vector_object * scale
                            
                            color = mathutils.Vector((
                                (vector_scaled.x + 1.0) * 0.5,
                                (vector_scaled.y + 1.0) * 0.5,
                                (vector_scaled.z + 1.0) * 0.5,
                                scale
                            ))
                            
                            color_layer.data[loop_idx].color = color
                            total_updated += 1
            
            mesh.update()
            
            try:
                bpy.ops.object.mode_set(mode='EDIT')
            except Exception as e:
                self.report({'WARNING'}, f"Could not return to edit mode: {str(e)}")
            
            self.report({'INFO'}, f"Baked {len(islands)} contiguous groups ({len(selected_indices)} vertices)")
            return {'FINISHED'}
            
        else:
            verts = []
            for idx in selected_indices:
                verts.append(mesh.vertices[idx].co)
            
            center = mathutils.Vector((0, 0, 0))
            for co in verts:
                center += co
            center /= len(verts)
            
            max_dist = 0.0
            for co in verts:
                vec = co - center
                max_component = max(abs(vec.x), abs(vec.y), abs(vec.z))
                max_dist = max(max_dist, max_component)
            
            if max_dist > 0:
                scale = 1.0 / max_dist
            else:
                scale = 1.0
            
            center_world = source_matrix @ center
            
            updated_count = 0
            for poly in mesh.polygons:
                for loop_idx in poly.loop_indices:
                    vertex_idx = mesh.loops[loop_idx].vertex_index
                    
                    if vertex_idx in selected_indices:
                        vertex = mesh.vertices[vertex_idx]
                        
                        vertex_world = source_matrix @ vertex.co
                        vector_world = center_world - vertex_world
                        vector_object = source_matrix.inverted_safe().to_3x3() @ vector_world
                        vector_scaled = vector_object * scale
                        
                        color = mathutils.Vector((
                            (vector_scaled.x + 1.0) * 0.5,
                            (vector_scaled.y + 1.0) * 0.5,
                            (vector_scaled.z + 1.0) * 0.5,
                            scale
                        ))
                        
                        color_layer.data[loop_idx].color = color
                        updated_count += 1
            
            mesh.update()
            
            try:
                bpy.ops.object.mode_set(mode='EDIT')
            except Exception as e:
                self.report({'WARNING'}, f"Could not return to edit mode: {str(e)}")
            
            self.report({'INFO'}, f"Baked {len(selected_indices)} vertices with scale {scale:.3f}")
            return {'FINISHED'}
    
    def draw(self, context):
        layout = self.layout
        layout.prop(self, "contiguous_mode")


class MESH_OT_select_all_linked_submeshes(Operator):
    bl_idname = "mesh.select_all_linked_submeshes"
    bl_label = "Select All Linked Submeshes"
    bl_description = "Select all vertices in any submesh that has at least one vertex selected"
    bl_options = {'REGISTER', 'UNDO'}
    
    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj is not None and
                obj.type == 'MESH' and
                context.mode == 'EDIT_MESH')
    
    def execute(self, context):
        obj = context.active_object
        mesh = obj.data
        
        # Switch to object mode for reliable selection updates
        bpy.ops.object.mode_set(mode='OBJECT')
        
        # Get currently selected vertices
        initially_selected = set()
        for v in mesh.vertices:
            if v.select:
                initially_selected.add(v.index)
        
        if not initially_selected:
            self.report({'WARNING'}, "No vertices selected")
            bpy.ops.object.mode_set(mode='EDIT')
            return {'CANCELLED'}
        
        # Build adjacency from edges
        adjacency = {i: set() for i in range(len(mesh.vertices))}
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            adjacency[v0].add(v1)
            adjacency[v1].add(v0)
        
        # Find all islands
        all_indices = set(range(len(mesh.vertices)))
        islands = []
        visited = set()
        
        for start_idx in all_indices:
            if start_idx in visited:
                continue
                
            island = set()
            queue = [start_idx]
            
            while queue:
                current = queue.pop(0)
                if current in visited:
                    continue
                    
                visited.add(current)
                island.add(current)
                
                for neighbor in adjacency[current]:
                    if neighbor not in visited:
                        queue.append(neighbor)
            
            islands.append(island)
        
        # Select vertices in islands that have any selected vertex
        expanded_count = 0
        affected_islands = 0
        
        for island in islands:
            if island & initially_selected:  # If island has any selected vertices
                new_selections = island - initially_selected
                expanded_count += len(new_selections)
                if new_selections:
                    affected_islands += 1
                # Select all vertices in this island
                for idx in island:
                    mesh.vertices[idx].select = True
        
        # Select edges where both vertices are selected
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            if mesh.vertices[v0].select and mesh.vertices[v1].select:
                edge.select = True
        
        # Select faces where all vertices are selected
        for face in mesh.polygons:
            all_verts_selected = all(mesh.vertices[v].select for v in face.vertices)
            if all_verts_selected:
                face.select = True
        
        # Return to edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        self.report({'INFO'}, f"Expanded selection in {affected_islands} submeshes ({expanded_count} new vertices)")
        return {'FINISHED'}


class MESH_OT_select_linked_across_boundaries(Operator):
    bl_idname = "mesh.select_linked_across_boundaries"
    bl_label = "Select Linked (Cross Boundaries)"
    bl_description = "Select linked vertices, crossing submesh boundaries where vertices share locations"
    bl_options = {'REGISTER', 'UNDO'}
    
    epsilon: FloatProperty(
        name="Location Tolerance",
        description="Maximum distance for vertices to be considered at the same location",
        default=0.0001,
        min=0.0,
        max=1.0,
        precision=6,
        subtype='DISTANCE'
    )
    
    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj is not None and
                obj.type == 'MESH' and
                context.mode == 'EDIT_MESH')
    
    def build_position_map(self, mesh, epsilon):
        """Build a map of vertices that share the same position within epsilon"""
        # Use integer hashing for much faster performance
        if epsilon > 0:
            scale = min(1.0 / epsilon, 1e7)  # Cap scale to prevent overflow
        else:
            scale = 1e7  # Large scale for exact matching
        
        position_map = {}
        
        # Group vertices by their quantized positions
        for v in mesh.vertices:
            # Quantize to integer grid
            key = (
                int(v.co.x * scale),
                int(v.co.y * scale),
                int(v.co.z * scale)
            )
            
            if key not in position_map:
                position_map[key] = []
            position_map[key].append(v.index)
        
        # Create adjacency only for vertices we'll actually use
        # This avoids creating empty sets for all vertices
        position_adjacency = {}
        
        for vertices_at_pos in position_map.values():
            if len(vertices_at_pos) > 1:
                # For small groups, connect all to all
                if len(vertices_at_pos) <= 10:
                    for i in range(len(vertices_at_pos)):
                        v1 = vertices_at_pos[i]
                        if v1 not in position_adjacency:
                            position_adjacency[v1] = set()
                        for j in range(i + 1, len(vertices_at_pos)):
                            v2 = vertices_at_pos[j]
                            if v2 not in position_adjacency:
                                position_adjacency[v2] = set()
                            position_adjacency[v1].add(v2)
                            position_adjacency[v2].add(v1)
                else:
                    # For large groups, create a hub vertex to avoid O(n²) connections
                    hub = vertices_at_pos[0]
                    if hub not in position_adjacency:
                        position_adjacency[hub] = set()
                    for i in range(1, len(vertices_at_pos)):
                        v = vertices_at_pos[i]
                        if v not in position_adjacency:
                            position_adjacency[v] = set()
                        position_adjacency[hub].add(v)
                        position_adjacency[v].add(hub)
        
        return position_adjacency
    
    def execute(self, context):
        obj = context.active_object
        mesh = obj.data
        
        # Switch to object mode
        bpy.ops.object.mode_set(mode='OBJECT')
        
        # Get initially selected vertices
        initially_selected = {v.index for v in mesh.vertices if v.select}
        
        if not initially_selected:
            self.report({'WARNING'}, "No vertices selected")
            bpy.ops.object.mode_set(mode='EDIT')
            return {'CANCELLED'}
        
        # Build edge adjacency only for vertices we might visit
        edge_adjacency = {}
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            if v0 not in edge_adjacency:
                edge_adjacency[v0] = set()
            if v1 not in edge_adjacency:
                edge_adjacency[v1] = set()
            edge_adjacency[v0].add(v1)
            edge_adjacency[v1].add(v0)
        
        # Build position adjacency
        position_adjacency = self.build_position_map(mesh, self.epsilon)
        
        # Function to get combined neighbors efficiently
        def get_neighbors(vertex_idx):
            neighbors = set()
            if vertex_idx in edge_adjacency:
                neighbors.update(edge_adjacency[vertex_idx])
            if vertex_idx in position_adjacency:
                neighbors.update(position_adjacency[vertex_idx])
            return neighbors
        
        # Flood fill from selected vertices using deque for better performance
        from collections import deque
        visited = set()
        queue = deque(initially_selected)
        
        while queue:
            current = queue.popleft()
            if current in visited:
                continue
                
            visited.add(current)
            mesh.vertices[current].select = True
            
            # Add all connected vertices to queue
            for neighbor in get_neighbors(current):
                if neighbor not in visited:
                    queue.append(neighbor)
        
        # Select edges where both vertices are selected
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            if mesh.vertices[v0].select and mesh.vertices[v1].select:
                edge.select = True
        
        # Select faces where all vertices are selected
        for face in mesh.polygons:
            all_verts_selected = all(mesh.vertices[v].select for v in face.vertices)
            if all_verts_selected:
                face.select = True
        
        # Return to edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        expanded_count = len(visited) - len(initially_selected)
        self.report({'INFO'}, f"Selected {len(visited)} vertices ({expanded_count} new)")
        return {'FINISHED'}
    
    def draw(self, context):
        layout = self.layout
        layout.prop(self, "epsilon")
        layout.label(text="Connects vertices at same location", icon='INFO')


class MESH_OT_deduplicate_submeshes(Operator):
    bl_idname = "mesh.deduplicate_submeshes"
    bl_label = "Deduplicate Submeshes"
    bl_description = "Remove duplicate submeshes from selection that have vertices at the same locations"
    bl_options = {'REGISTER', 'UNDO'}
    
    tolerance: FloatProperty(
        name="Position Tolerance",
        description="Maximum distance for vertices to be considered at the same position",
        default=0.0001,
        min=0.0,
        max=1.0,
        precision=6
    )
    
    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj is not None and
                obj.type == 'MESH' and
                context.mode == 'EDIT_MESH')
    
    def get_selected_vertex_islands(self, mesh):
        """Find all contiguous groups of selected vertices in the mesh"""
        # Get selected vertices
        selected_indices = {v.index for v in mesh.vertices if v.select}
        
        if not selected_indices:
            return []
        
        # Build adjacency only for selected vertices
        adjacency = {idx: set() for idx in selected_indices}
        
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            if v0 in selected_indices and v1 in selected_indices:
                adjacency[v0].add(v1)
                adjacency[v1].add(v0)
        
        islands = []
        visited = set()
        
        for start_idx in selected_indices:
            if start_idx in visited:
                continue
                
            island = set()
            queue = [start_idx]
            
            while queue:
                current = queue.pop(0)
                if current in visited:
                    continue
                    
                visited.add(current)
                island.add(current)
                
                for neighbor in adjacency[current]:
                    if neighbor not in visited:
                        queue.append(neighbor)
            
            islands.append(island)
        
        return islands
    
    def get_island_hash(self, mesh, island_indices):
        """Create a hash for an island based on vertex positions"""
        # Round positions to handle tolerance
        decimal_places = 6 if self.tolerance == 0 else max(0, int(-math.log10(self.tolerance)))
        
        positions = []
        for idx in island_indices:
            co = mesh.vertices[idx].co
            # Round each component
            rounded = (
                round(co.x, decimal_places),
                round(co.y, decimal_places),
                round(co.z, decimal_places)
            )
            positions.append(rounded)
        
        # Sort positions to ensure consistent ordering
        positions.sort()
        
        # Convert to tuple for hashing
        return tuple(positions)
    
    def execute(self, context):
        obj = context.active_object
        mesh = obj.data
        
        # Switch to object mode
        bpy.ops.object.mode_set(mode='OBJECT')
        
        # Find all selected islands
        islands = self.get_selected_vertex_islands(mesh)
        
        if not islands:
            self.report({'WARNING'}, "No vertices selected")
            bpy.ops.object.mode_set(mode='EDIT')
            return {'CANCELLED'}
        
        if len(islands) <= 1:
            self.report({'INFO'}, "No duplicate submeshes found in selection (only 1 submesh)")
            bpy.ops.object.mode_set(mode='EDIT')
            return {'FINISHED'}
        
        # Group islands by their hash
        island_groups = {}
        for island in islands:
            island_hash = self.get_island_hash(mesh, island)
            if island_hash not in island_groups:
                island_groups[island_hash] = []
            island_groups[island_hash].append(island)
        
        # Find duplicates
        duplicates_to_remove = []
        duplicate_count = 0
        
        for hash_key, group in island_groups.items():
            if len(group) > 1:
                # Keep the first island, mark others for removal
                for island in group[1:]:
                    duplicates_to_remove.append(island)
                    duplicate_count += 1
        
        if not duplicates_to_remove:
            self.report({'INFO'}, "No duplicate submeshes found in selection")
            bpy.ops.object.mode_set(mode='EDIT')
            return {'FINISHED'}
        
        # Enter edit mode and select vertices to delete
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_all(action='DESELECT')
        bpy.ops.object.mode_set(mode='OBJECT')
        
        # Select all vertices in duplicate islands
        vertices_to_delete = set()
        for island in duplicates_to_remove:
            vertices_to_delete.update(island)
        
        for idx in vertices_to_delete:
            mesh.vertices[idx].select = True
        
        # Delete selected vertices
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.delete(type='VERT')
        
        self.report({'INFO'}, f"Removed {duplicate_count} duplicate submeshes from selection ({len(vertices_to_delete)} vertices)")
        return {'FINISHED'}
    
    def draw(self, context):
        layout = self.layout
        layout.prop(self, "tolerance")
        layout.label(text="Set to 0 for exact matching", icon='INFO')


class MESH_OT_merge_by_distance_in_submeshes(Operator):
    bl_idname = "mesh.merge_by_distance_in_submeshes"
    bl_label = "Merge by Distance (Per Submesh)"
    bl_description = "Merge vertices by distance within each submesh separately"
    bl_options = {'REGISTER', 'UNDO'}
    
    merge_distance: FloatProperty(
        name="Merge Distance",
        description="Maximum distance for merging vertices",
        default=0.001,
        min=0.0,
        max=1.0,
        precision=6,
        subtype='DISTANCE'
    )
    
    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj is not None and
                obj.type == 'MESH' and
                context.mode == 'EDIT_MESH')
    
    def get_selected_vertex_islands(self, mesh):
        """Find all contiguous groups of selected vertices - works like other operators"""
        # Get selected vertices
        selected_indices = {v.index for v in mesh.vertices if v.select}
        
        if not selected_indices:
            return []
        
        # Build adjacency only for selected vertices
        adjacency = {idx: set() for idx in selected_indices}
        
        # Only check edges that might connect selected vertices
        for edge in mesh.edges:
            v0, v1 = edge.vertices
            if v0 in selected_indices and v1 in selected_indices:
                adjacency[v0].add(v1)
                adjacency[v1].add(v0)
        
        islands = []
        visited = set()
        
        from collections import deque
        
        for start_idx in selected_indices:
            if start_idx in visited:
                continue
                
            island = set()
            queue = deque([start_idx])
            
            while queue:
                current = queue.popleft()
                if current in visited:
                    continue
                    
                visited.add(current)
                island.add(current)
                
                for neighbor in adjacency[current]:
                    if neighbor not in visited:
                        queue.append(neighbor)
            
            islands.append(island)
        
        return islands
    
    def execute(self, context):
        obj = context.active_object
        mesh = obj.data
        
        # Use the most efficient approach: single merge with island constraints
        bm = bmesh.from_edit_mesh(mesh)
        
        # Get selected vertices
        selected_verts = [v for v in bm.verts if v.select]
        if not selected_verts:
            self.report({'WARNING'}, "No vertices selected")
            return {'CANCELLED'}
        
        # Build islands using optimized algorithm
        selected_set = set(selected_verts)
        vert_to_island = {}
        island_id = 0
        
        # Find islands with stack-based traversal
        for v in selected_verts:
            if v in vert_to_island:
                continue
                
            # Mark all vertices in this island
            stack = [v]
            while stack:
                current = stack.pop()
                if current in vert_to_island:
                    continue
                    
                vert_to_island[current] = island_id
                
                # Add connected vertices
                for edge in current.link_edges:
                    other = edge.other_vert(current)
                    if other in selected_set and other not in vert_to_island:
                        stack.append(other)
            
            island_id += 1
        
        # Build merge mapping manually to avoid repeated operations
        merge_targets = {}
        total_merged = 0
        islands_with_merges = 0
        
        # For each island, find vertices to merge
        from collections import defaultdict
        island_verts = defaultdict(list)
        for v, iid in vert_to_island.items():
            island_verts[iid].append(v)
        
        # Process each island
        for island_id, verts in island_verts.items():
            if len(verts) < 2:
                continue
            
            # Build spatial hash for this island only
            merge_dist_sq = self.merge_distance * self.merge_distance
            merged_in_island = 0
            
            # Simple O(n²) for small islands is often faster than spatial hashing
            # Most submeshes have 10-50 verts, so this is actually efficient
            processed = set()
            for i, v1 in enumerate(verts):
                if v1 in merge_targets or v1 in processed:
                    continue
                    
                processed.add(v1)
                
                # Find vertices within merge distance
                for v2 in verts[i+1:]:
                    if v2 in merge_targets or v2 in processed:
                        continue
                        
                    # Check distance
                    diff = v1.co - v2.co
                    if diff.length_squared <= merge_dist_sq:
                        merge_targets[v2] = v1
                        processed.add(v2)
                        merged_in_island += 1
            
            if merged_in_island > 0:
                total_merged += merged_in_island
                islands_with_merges += 1
        
        # Now perform all merges in one go using BMesh weld
        if merge_targets:
            # Convert merge mapping to format expected by weld_verts
            targetmap = {v: merge_targets[v] for v in merge_targets if v.is_valid}
            
            if targetmap:
                bmesh.ops.weld_verts(bm, targetmap=targetmap)
        
        # Update the mesh
        bmesh.update_edit_mesh(mesh)
        
        total_islands = len(island_verts)
        multi_vert_islands = sum(1 for verts in island_verts.values() if len(verts) >= 2)
        single_vert_islands = total_islands - multi_vert_islands
        
        if total_merged > 0:
            self.report({'INFO'}, f"Merged {total_merged} vertices in {islands_with_merges} of {multi_vert_islands} submeshes (skipped {single_vert_islands} single-vertex)")
        else:
            self.report({'INFO'}, f"No vertices close enough to merge in {multi_vert_islands} submeshes")
        
        return {'FINISHED'}
    
    def draw(self, context):
        layout = self.layout
        layout.prop(self, "merge_distance")
        layout.label(text="Merges within submeshes only", icon='INFO')


class MESH_PT_bake_vertex_panel(Panel):
    bl_label = "Bake Vertex Vectors"
    bl_idname = "MESH_PT_bake_vertex_vectors"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Tool"

    def draw(self, context):
        layout = self.layout
        obj = context.active_object

        col = layout.column()
        
        if obj and obj.type == 'MESH':
            if context.mode == 'EDIT_MESH':
                col.operator("mesh.select_all_linked_submeshes", icon='SELECT_EXTEND')
                col.operator("mesh.select_linked_across_boundaries", icon='LINKED')
                col.operator("mesh.deduplicate_submeshes", icon='DUPLICATE')
                col.operator("mesh.merge_by_distance_in_submeshes", icon='AUTOMERGE_ON')
                col.separator()
                col.operator("mesh.bake_vertex_vectors", icon='EXPORT')
                
                box = col.box()
                box.label(text="Info:", icon='INFO')
                box.label(text="Select All Linked: Expand selection to full submeshes")
                box.label(text="Select Linked Cross: Select linked across boundaries")
                box.label(text="Deduplicate: Remove duplicate selected submeshes")
                box.label(text="Merge: Merge vertices within submeshes")
                box.label(text="Bake: Auto-scale selected vertices")
                box.label(text="Toggle Contiguous Groups for separate islands")
                box.label(text="Scale factor stored in alpha channel")
                
                mesh = obj.data
                if mesh.vertex_colors and len(mesh.vertex_colors) > 0:
                    col.separator()
                    col.label(text=f"Active: {mesh.vertex_colors.active.name}", icon='GROUP_VCOL')
            else:
                col.label(text="Enter Edit Mode to bake vertices", icon='INFO')
        else:
            col.label(text="Select a mesh object", icon='ERROR')


classes = [
    MESH_OT_bake_vertex_vectors,
    MESH_OT_select_all_linked_submeshes,
    MESH_OT_select_linked_across_boundaries,
    MESH_OT_deduplicate_submeshes,
    MESH_OT_merge_by_distance_in_submeshes,
    MESH_PT_bake_vertex_panel
]


def menu_func(self, context):
    self.layout.separator()
    self.layout.operator("mesh.select_all_linked_submeshes", icon='SELECT_EXTEND')
    self.layout.operator("mesh.select_linked_across_boundaries", icon='LINKED')
    self.layout.operator("mesh.deduplicate_submeshes", icon='DUPLICATE')
    self.layout.operator("mesh.merge_by_distance_in_submeshes", icon='AUTOMERGE_ON')
    self.layout.operator("mesh.bake_vertex_vectors", icon='EXPORT')


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.VIEW3D_MT_edit_mesh.append(menu_func)


def unregister():
    bpy.types.VIEW3D_MT_edit_mesh.remove(menu_func)
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()