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
import imp
imp . reload ( utilities_uv )
class op ( bpy . types . Operator ) :
2019-12-18 20:53:16 +00:00
bl_idname = " uv.textools_select_islands_flipped "
bl_label = " Select Flipped "
bl_description = " Select all flipped UV islands "
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
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
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
return True
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
def execute ( self , context ) :
select_flipped ( context )
return { ' FINISHED ' }
2019-06-08 23:42:50 +00:00
def select_flipped ( 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
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 = utilities_uv . getSelectionIslands ( )
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 . context . scene . tool_settings . use_uv_select_sync = False
bpy . ops . uv . select_all ( action = ' DESELECT ' )
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
for island in islands :
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
is_flipped = False
for face in island :
if is_flipped :
break
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Using 'Sum of Edges' to detect counter clockwise https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order
sum = 0
count = len ( face . loops )
for i in range ( count ) :
uv_A = face . loops [ i ] [ uv_layers ] . uv
uv_B = face . loops [ ( i + 1 ) % count ] [ uv_layers ] . uv
sum + = ( uv_B . x - uv_A . x ) * ( uv_B . y + uv_A . y )
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
if sum > 0 :
# Flipped
is_flipped = True
break
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Select Island if flipped
if is_flipped :
for face in island :
for loop in face . loops :
loop [ uv_layers ] . select = True
2019-06-08 23:42:50 +00:00
class Island_bounds :
2019-12-18 20:53:16 +00:00
faces = [ ]
center = Vector ( [ 0 , 0 ] )
min = Vector ( [ 0 , 0 ] )
max = Vector ( [ 0 , 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
#Select Island
bpy . ops . uv . select_all ( action = ' DESELECT ' )
utilities_uv . set_selected_faces ( faces )
bounds = utilities_uv . getSelectionBBox ( )
self . center = bounds [ ' center ' ]
self . min = bounds [ ' min ' ]
self . max = bounds [ ' max ' ]
def isEqual ( A , B ) :
# Bounding Box AABB intersection?
min_x = max ( A . min . x , B . min . x )
min_y = max ( A . min . y , B . min . y )
max_x = min ( A . max . x , B . max . x )
max_y = min ( A . max . y , B . max . y )
if not ( max_x < min_x or max_y < min_y ) :
return True
return False
2019-06-08 23:42:50 +00:00
bpy . utils . register_class ( op )