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

123 lines
3.8 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_align"
bl_label = "Align"
bl_description = "Align vertices, edges or shells"
bl_options = {'REGISTER', 'UNDO'}
2019-12-24 19:59:46 +00:00
direction: bpy.props.StringProperty(name="Direction", default="top")
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-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-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):
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
align(context, self.direction)
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def align(context, direction):
2019-12-24 19:59:46 +00:00
# Store selection
2019-12-18 20:53:16 +00:00
utilities_uv.selection_store()
if bpy.context.tool_settings.transform_pivot_point != 'CURSOR':
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
2019-12-24 19:59:46 +00:00
# B-Mesh
2019-12-18 20:53:16 +00:00
obj = bpy.context.active_object
2019-12-24 19:59:46 +00:00
bm = bmesh.from_edit_mesh(obj.data)
uv_layers = bm.loops.layers.uv.verify()
2019-12-18 20:53:16 +00:00
if len(obj.data.uv_layers) == 0:
print("There is no UV channel or UV data set")
return
# Collect BBox sizes
boundsAll = utilities_uv.getSelectionBBox()
mode = bpy.context.scene.tool_settings.uv_select_mode
if mode == 'FACE' or mode == 'ISLAND':
print("____ Align Islands")
2019-12-24 19:59:46 +00:00
# Collect UV islands
2019-12-18 20:53:16 +00:00
islands = utilities_uv.getSelectionIslands()
for island in islands:
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
bpy.ops.uv.select_all(action='DESELECT')
utilities_uv.set_selected_faces(island)
bounds = utilities_uv.getSelectionBBox()
# print("Island "+str(len(island))+"x faces, delta: "+str(delta.y))
if direction == "bottom":
2019-12-24 19:59:46 +00:00
delta = boundsAll['min'] - bounds['min']
2019-12-18 20:53:16 +00:00
bpy.ops.transform.translate(value=(0, delta.y, 0))
elif direction == "top":
delta = boundsAll['max'] - bounds['max']
bpy.ops.transform.translate(value=(0, delta.y, 0))
elif direction == "left":
2019-12-24 19:59:46 +00:00
delta = boundsAll['min'] - bounds['min']
2019-12-18 20:53:16 +00:00
bpy.ops.transform.translate(value=(delta.x, 0, 0))
elif direction == "right":
delta = boundsAll['max'] - bounds['max']
bpy.ops.transform.translate(value=(delta.x, 0, 0))
else:
print("Unkown direction: "+str(direction))
elif mode == 'EDGE' or mode == 'VERTEX':
print("____ Align Verts")
for f in bm.faces:
if f.select:
for l in f.loops:
luv = l[uv_layers]
if luv.select:
# print("Idx: "+str(luv.uv))
if direction == "top":
luv.uv[1] = boundsAll['max'].y
elif direction == "bottom":
luv.uv[1] = boundsAll['min'].y
elif direction == "left":
luv.uv[0] = boundsAll['min'].x
elif direction == "right":
luv.uv[0] = boundsAll['max'].x
bmesh.update_edit_mesh(obj.data)
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-24 19:59:46 +00:00
bpy.utils.register_class(op)