forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_align.py
182 lines (143 loc) · 6.18 KB
/
op_align.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import bpy
import bmesh
from mathutils import Vector
from . import utilities_uv
class op(bpy.types.Operator):
bl_idname = "uv.textools_align"
bl_label = "Align"
bl_description = "Align vertices, edges or shells"
bl_options = {'REGISTER', 'UNDO'}
direction : bpy.props.StringProperty(name="Direction", default="top")
@classmethod
def poll(cls, context):
if bpy.context.area.ui_type != 'UV':
return False
if not bpy.context.active_object:
return False
if bpy.context.active_object.mode != 'EDIT':
return False
if not bpy.context.object.data.uv_layers:
return False
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
return True
def execute(self, context):
align_mode = bpy.context.scene.texToolsSettings.align_mode
if align_mode == 'SELECTION':
all_ob_bounds = utilities_uv.multi_object_loop(utilities_uv.getSelectionBBox, need_results=True)
if not any(all_ob_bounds):
return {'CANCELLED'}
boundsAll = utilities_uv.get_BBOX_multi(all_ob_bounds)
utilities_uv.multi_object_loop(align, context, align_mode, self.direction, boundsAll=boundsAll)
else:
udim_tile, column, row = utilities_uv.get_UDIM_tile_coords(bpy.context.active_object)
utilities_uv.multi_object_loop(align, context, align_mode, self.direction, column=column, row=row)
return {'FINISHED'}
def align(context, align_mode, direction, boundsAll={}, column=0, row=0):
prepivot = bpy.context.space_data.pivot_point
bpy.context.space_data.pivot_point = 'CURSOR'
obj = bpy.context.active_object
bm = bmesh.from_edit_mesh(obj.data)
uv_layers = bm.loops.layers.uv.verify()
if align_mode == 'SELECTION':
center_all = boundsAll['center']
elif align_mode == 'CURSOR':
cursor = Vector(bpy.context.space_data.cursor_location.copy())
center_all = boundsAll['min'] = boundsAll['max'] = cursor
else: #CANVAS
if direction == "bottom" or direction == "left" or direction == "bottomleft":
center_all = boundsAll['min'] = boundsAll['max'] = Vector((column, row))
elif direction == "top" or direction == "topleft":
center_all = boundsAll['min'] = boundsAll['max'] = Vector((column, row + 1))
elif direction == "right" or direction == "topright":
center_all = boundsAll['min'] = boundsAll['max'] = Vector((column + 1, row + 1))
elif direction == "bottomright":
center_all = boundsAll['min'] = boundsAll['max'] = Vector((column + 1, row))
elif direction == "horizontal" or direction == "vertical" or direction == "center":
center_all = boundsAll['min'] = boundsAll['max'] = Vector((column + 0.5, row + 0.5))
selection_mode = bpy.context.scene.tool_settings.uv_select_mode
if selection_mode == 'FACE' or selection_mode == 'ISLAND':
islands = utilities_uv.splittedSelectionByIsland(bm, uv_layers, restore_selected=True)
for island in islands:
bounds = utilities_uv.get_BBOX(island, bm, uv_layers)
center = bounds['center']
if direction == "bottom":
delta = boundsAll['min'] - bounds['min']
utilities_uv.move_island(island, 0, delta.y)
elif direction == "top":
delta = boundsAll['max'] - bounds['max']
utilities_uv.move_island(island, 0, delta.y)
elif direction == "left":
delta = boundsAll['min'] - bounds['min']
utilities_uv.move_island(island, delta.x, 0)
elif direction == "right":
delta = boundsAll['max'] - bounds['max']
utilities_uv.move_island(island, delta.x, 0)
elif direction == "center":
delta = Vector((center_all - center))
utilities_uv.move_island(island, delta.x, delta.y)
elif direction == "horizontal":
delta = Vector((center_all - center))
utilities_uv.move_island(island, 0, delta.y)
elif direction == "vertical":
delta = Vector((center_all - center))
utilities_uv.move_island(island, delta.x, 0)
elif direction == "bottomleft":
delta = boundsAll['min'] - bounds['min']
utilities_uv.move_island(island, delta.x, delta.y)
elif direction == "topright":
delta = boundsAll['max'] - bounds['max']
utilities_uv.move_island(island, delta.x, delta.y)
elif direction == "topleft":
delta_x = boundsAll['min'] - bounds['min']
delta_y = boundsAll['max'] - bounds['max']
utilities_uv.move_island(island, delta_x.x, delta_y.y)
elif direction == "bottomright":
delta_x = boundsAll['max'] - bounds['max']
delta_y = boundsAll['min'] - bounds['min']
utilities_uv.move_island(island, delta_x.x, delta_y.y)
else:
print("Unknown direction: "+str(direction))
# Workaround for selection not flushing properly from loops to EDGE Selection Mode, apparently since UV edge selection support was added to the UV space
# Not fully working though
bpy.ops.uv.select_mode(type='VERTEX')
bpy.context.scene.tool_settings.uv_select_mode = selection_mode
else: # Vertices or Edges UV selection mode
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
elif direction == "center":
luv.uv[0] = center_all.x
luv.uv[1] = center_all.y
elif direction == "horizontal":
luv.uv[1] = center_all.y
elif direction == "vertical":
luv.uv[0] = center_all.x
elif direction == "bottomleft":
luv.uv[0] = boundsAll['min'].x
luv.uv[1] = boundsAll['min'].y
elif direction == "topright":
luv.uv[0] = boundsAll['max'].x
luv.uv[1] = boundsAll['max'].y
elif direction == "topleft":
luv.uv[0] = boundsAll['min'].x
luv.uv[1] = boundsAll['max'].y
elif direction == "bottomright":
luv.uv[0] = boundsAll['max'].x
luv.uv[1] = boundsAll['min'].y
else:
print("Unknown direction: "+str(direction))
bmesh.update_edit_mesh(obj.data)
bpy.context.space_data.pivot_point = prepivot
bpy.utils.register_class(op)