-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathop_island_centralize.py
56 lines (44 loc) · 1.82 KB
/
op_island_centralize.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
import bpy
import bmesh
from . import utilities_uv
from .utilities_bbox import BBox
from mathutils import Vector
class op(bpy.types.Operator):
bl_idname = "uv.textools_island_centralize"
bl_label = "Centralize"
bl_description = "Move the selected faces the closest possible to the 0-1 UV area without changes in the textured object"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if not bpy.context.active_object:
return False
if bpy.context.active_object.mode != 'EDIT':
return False
return True
def execute(self, context):
_, column, row = utilities_uv.get_UDIM_tile_coords(bpy.context.active_object)
return self.centralize(column, row)
@staticmethod
def centralize(column, row):
selected_objs = utilities_uv.selected_unique_objects_in_mode_with_uv()
update_obj = []
for obj in selected_objs:
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
islands = utilities_uv.get_selected_islands(bm, uv_layer)
if not islands:
continue
changed = False
for island in islands:
center = BBox.calc_bbox_uv(island, uv_layer).center
delta = Vector((round(-center.x + 0.5) + column, round(-center.y + 0.5) + row))
if delta != Vector((0, 0)):
changed = True
utilities_uv.translate_island(island, uv_layer, delta)
if changed:
update_obj.append(obj)
if not update_obj:
return {'CANCELLED'}
for obj in update_obj:
bmesh.update_edit_mesh(obj.data)
return {'FINISHED'}