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

144 lines
4.0 KiB
Python
Raw Normal View History

2019-06-08 23:42:50 +00:00
import bpy
import bmesh
import operator
import math
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_uv
2019-12-24 19:59:46 +00:00
2019-06-08 23:42:50 +00:00
class op(bpy.types.Operator):
2019-12-18 20:53:16 +00:00
bl_idname = "uv.textools_island_align_edge"
bl_label = "Align Island by Edge"
bl_description = "Align the island by selected edge"
bl_options = {'REGISTER', 'UNDO'}
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
@classmethod
def poll(cls, context):
2019-12-24 19:59:46 +00:00
# Only in UV editor mode
2019-12-18 20:53:16 +00:00
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
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-24 19:59:46 +00:00
# Only in Edit mode
2019-12-18 20:53:16 +00:00
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
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Requires UV map
2019-12-18 20:53:16 +00:00
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
# Requires UV Edge select mode
if bpy.context.scene.tool_settings.uv_select_mode != 'EDGE':
2019-12-24 19:59:46 +00:00
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):
2019-12-24 19:59:46 +00:00
# Store selection
2019-12-18 20:53:16 +00:00
utilities_uv.selection_store()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
main(context)
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Restore selection
2019-12-18 20:53:16 +00:00
utilities_uv.selection_restore()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def main(context):
2019-12-18 20:53:16 +00:00
print("Executing operator_island_align_edge")
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()
2019-12-24 19:59:46 +00:00
faces_selected = []
2019-12-18 20:53:16 +00:00
for face in bm.faces:
if face.select:
for loop in face.loops:
if loop[uv_layers].select:
faces_selected.append(face)
break
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
print("faces_selected: "+str(len(faces_selected)))
# Collect 2 uv verts for each island
face_uvs = {}
for face in faces_selected:
uvs = []
for loop in face.loops:
if loop[uv_layers].select:
uvs.append(loop[uv_layers])
if len(uvs) >= 2:
break
if len(uvs) >= 2:
face_uvs[face] = uvs
faces_islands = {}
faces_unparsed = faces_selected.copy()
for face in face_uvs:
if face in faces_unparsed:
bpy.ops.uv.select_all(action='DESELECT')
2019-12-24 19:59:46 +00:00
face_uvs[face][0].select = True
bpy.ops.uv.select_linked() # Extend selection
# Collect faces
faces_island = [face]
2019-12-18 20:53:16 +00:00
for f in faces_unparsed:
if f != face and f.select and f.loops[0][uv_layers].select:
print("append "+str(f.index))
faces_island.append(f)
for f in faces_island:
faces_unparsed.remove(f)
2019-12-24 19:59:46 +00:00
# Assign Faces to island
2019-12-18 20:53:16 +00:00
faces_islands[face] = faces_island
print("Sets: {}x".format(len(faces_islands)))
# Align each island to its edges
for face in faces_islands:
2019-12-24 19:59:46 +00:00
align_island(face_uvs[face][0].uv, face_uvs[face]
[1].uv, faces_islands[face])
2019-06-08 23:42:50 +00:00
def align_island(uv_vert0, uv_vert1, faces):
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
print("Align {}x faces".format(len(faces)))
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Select faces
bpy.ops.uv.select_all(action='DESELECT')
for face in faces:
for loop in face.loops:
loop[uv_layers].select = True
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
diff = uv_vert1 - uv_vert0
2019-12-24 19:59:46 +00:00
angle = math.atan2(diff.y, diff.x) % (math.pi/2)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.ops.uv.select_linked()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
bpy.ops.uv.cursor_set(location=uv_vert0 + diff/2)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
if angle >= (math.pi/4):
angle = angle - (math.pi/2)
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
bpy.ops.transform.rotate(value=angle, orient_axis='Z', constraint_axis=(
False, False, False), orient_type='GLOBAL', mirror=False, use_proportional_edit=False)
2019-06-08 23:42:50 +00:00
bpy.utils.register_class(op)