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

177 lines
5.7 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 . 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_island_align_sort"
bl_label = "Align & Sort"
bl_description = "Rotates UV islands to minimal bounds and sorts them horizontal or vertical"
bl_options = {'REGISTER', 'UNDO'}
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
is_vertical: bpy.props.BoolProperty(
description="Vertical or Horizontal orientation", default=True)
padding: bpy.props.FloatProperty(
description="Padding between UV islands", default=0.05)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
@classmethod
def poll(cls, context):
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-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-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:
2019-12-24 19:59:46 +00:00
# self.report({'WARNING'}, "Object must have more than one UV map")
return False
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Not in Synced mode
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-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):
main(context, self.is_vertical, self.padding)
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def main(context, isVertical, padding):
2019-12-18 20:53:16 +00:00
print("Executing IslandsAlignSort main {}".format(padding))
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
if bpy.context.tool_settings.transform_pivot_point != 'CURSOR':
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Only in Face or Island mode
2019-12-18 20:53:16 +00:00
if bpy.context.scene.tool_settings.uv_select_mode is not 'FACE' or 'ISLAND':
bpy.context.scene.tool_settings.uv_select_mode = 'FACE'
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +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
boundsAll = utilities_uv.getSelectionBBox()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
islands = utilities_uv.getSelectionIslands()
2019-12-24 19:59:46 +00:00
allSizes = {} # https://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
2019-12-18 20:53:16 +00:00
allBounds = {}
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
print("Islands: "+str(len(islands))+"x")
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.context.window_manager.progress_begin(0, len(islands))
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Rotate to minimal bounds
2019-12-18 20:53:16 +00:00
for i in range(0, len(islands)):
alignIslandMinimalBounds(uv_layers, islands[i])
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Collect BBox sizes
bounds = utilities_uv.getSelectionBBox()
2019-12-24 19:59:46 +00:00
allSizes[i] = max(bounds['width'], bounds['height']) + \
i*0.000001 # Make each size unique
allBounds[i] = bounds
2019-12-18 20:53:16 +00:00
print("Rotate compact: "+str(allSizes[i]))
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.context.window_manager.progress_update(i)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.context.window_manager.progress_end()
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Position by sorted size in row
# Sort by values, store tuples
sortedSizes = sorted(allSizes.items(), key=operator.itemgetter(1))
2019-12-18 20:53:16 +00:00
sortedSizes.reverse()
offset = 0.0
for sortedSize in sortedSizes:
index = sortedSize[0]
island = islands[index]
bounds = allBounds[index]
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Select Island
2019-12-18 20:53:16 +00:00
bpy.ops.uv.select_all(action='DESELECT')
utilities_uv.set_selected_faces(island)
2019-12-24 19:59:46 +00:00
# Offset Island
2019-12-18 20:53:16 +00:00
if(isVertical):
2019-12-24 19:59:46 +00:00
delta = Vector(
(boundsAll['min'].x - bounds['min'].x, boundsAll['max'].y - bounds['max'].y))
2019-12-18 20:53:16 +00:00
bpy.ops.transform.translate(value=(delta.x, delta.y-offset, 0))
offset += bounds['height']+padding
else:
print("Horizontal")
2019-12-24 19:59:46 +00:00
delta = Vector(
(boundsAll['min'].x - bounds['min'].x, boundsAll['max'].y - bounds['max'].y))
2019-12-18 20:53:16 +00:00
bpy.ops.transform.translate(value=(delta.x+offset, delta.y, 0))
offset += bounds['width']+padding
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
def alignIslandMinimalBounds(uv_layers, faces):
2019-12-18 20:53:16 +00:00
# Select Island
bpy.ops.uv.select_all(action='DESELECT')
utilities_uv.set_selected_faces(faces)
steps = 8
2019-12-24 19:59:46 +00:00
angle = 45 # Starting Angle, half each step
2019-12-18 20:53:16 +00:00
bboxPrevious = utilities_uv.getSelectionBBox()
for i in range(0, steps):
# Rotate right
2019-12-24 19:59:46 +00:00
bpy.ops.transform.rotate(
value=(angle * math.pi / 180), orient_axis='Z')
2019-12-18 20:53:16 +00:00
bbox = utilities_uv.getSelectionBBox()
if i == 0:
sizeA = bboxPrevious['width'] * bboxPrevious['height']
sizeB = bbox['width'] * bbox['height']
if abs(bbox['width'] - bbox['height']) <= 0.0001 and sizeA < sizeB:
# print("Already squared")
2019-12-24 19:59:46 +00:00
bpy.ops.transform.rotate(
value=(-angle * math.pi / 180), orient_axis='Z')
break
2019-12-18 20:53:16 +00:00
if bbox['minLength'] < bboxPrevious['minLength']:
2019-12-24 19:59:46 +00:00
bboxPrevious = bbox # Success
2019-12-18 20:53:16 +00:00
else:
# Rotate Left
2019-12-24 19:59:46 +00:00
bpy.ops.transform.rotate(
value=(-angle*2 * math.pi / 180), orient_axis='Z')
2019-12-18 20:53:16 +00:00
bbox = utilities_uv.getSelectionBBox()
if bbox['minLength'] < bboxPrevious['minLength']:
2019-12-24 19:59:46 +00:00
bboxPrevious = bbox # Success
2019-12-18 20:53:16 +00:00
else:
# Restore angle of this iteration
2019-12-24 19:59:46 +00:00
bpy.ops.transform.rotate(
value=(angle * math.pi / 180), orient_axis='Z')
2019-12-18 20:53:16 +00:00
angle = angle / 2
if bboxPrevious['width'] < bboxPrevious['height']:
bpy.ops.transform.rotate(value=(90 * math.pi / 180), orient_axis='Z')
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
bpy.utils.register_class(op)