1
0
mirror of https://github.com/drewcassidy/TexTools-Blender synced 2024-06-11 00:26:53 +00:00
Blender-TexTools/op_select_islands_identical.py

130 lines
3.5 KiB
Python
Raw Normal View History

2019-06-08 23:42:50 +00:00
import bpy
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_uv
class op(bpy.types.Operator):
2019-12-18 20:53:16 +00:00
bl_idname = "uv.textools_select_islands_identical"
bl_label = "Select identical"
bl_description = "Select identical UV islands with similar topology"
bl_options = {'REGISTER', 'UNDO'}
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
@classmethod
def poll(cls, context):
if not bpy.context.active_object:
return False
if bpy.context.active_object.type != 'MESH':
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
#Only in Edit mode
if bpy.context.active_object.mode != 'EDIT':
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
#Only in UV editor mode
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
##Requires UV map
if not bpy.context.object.data.uv_layers:
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
#Not in Synced mode
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
return True
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
def execute(self, context):
swap(self, context)
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def swap(self, context):
2019-12-18 20:53:16 +00:00
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Get selected island
islands = utilities_uv.getSelectionIslands()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
if len(islands) != 1:
self.report({'ERROR_INVALID_INPUT'}, "Please select only 1 UV Island")
return
island_stats_source = Island_stats(islands[0])
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.context.scene.tool_settings.uv_select_mode = 'FACE'
bpy.ops.uv.select_all(action='SELECT')
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
islands_all = utilities_uv.getSelectionIslands()
islands_equal = []
for island in islands_all:
island_stats = Island_stats(island)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
if island_stats_source.isEqual(island_stats):
islands_equal.append(island_stats.faces)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
print("Islands: "+str(len(islands_equal))+"x")
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.ops.uv.select_all(action='DESELECT')
for island in islands_equal:
for face in island:
for loop in face.loops:
if not loop[uv_layers].select:
loop[uv_layers].select = True
2019-06-08 23:42:50 +00:00
class Island_stats:
2019-12-18 20:53:16 +00:00
countFaces = 0
countVerts = 0
faces = []
area = 0
countLinkedEdges = 0
countLinkedFaces = 0
def __init__(self, faces):
bm = bmesh.from_edit_mesh(bpy.context.active_object.data);
uv_layers = bm.loops.layers.uv.verify();
# Collect topology stats
self.faces = faces
verts = []
for face in faces:
self.countFaces+=1
self.area+=face.calc_area()
for loop in face.loops:
if loop.vert not in verts:
verts.append(loop.vert)
self.countVerts+=1
self.countLinkedEdges+= len(loop.vert.link_edges)
self.countLinkedFaces+= len(loop.vert.link_faces)
def isEqual(self, other):
if self.countVerts != other.countVerts:
return False
if self.countFaces != other.countFaces:
return False
if self.countLinkedEdges != other.countLinkedEdges:
return False
if self.countLinkedFaces != other.countLinkedFaces:
return False
# area needs to be 90%+ identical
if min(self.area, other.area)/max(self.area, other.area) < 0.7:
return False
return True
2019-06-08 23:42:50 +00:00
bpy.utils.register_class(op)