Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
2.3 kB
3
Indexable
Never
import bpy
import bmesh

def get_uv_islands(bm, uv_layer):
    """ Получаем список UV островов. """
    seen = set()
    islands = []
    faces = [f for f in bm.faces if f.select]

    for face in faces:
        if face in seen:
            continue
        island = []
        stack = [face]
        while stack:
            f = stack.pop()
            if f not in seen:
                seen.add(f)
                island.append(f)
                for edge in f.edges:
                    if edge.seam:
                        continue
                    for f_adj in edge.link_faces:
                        if f_adj.select and f_adj not in seen:
                            stack.append(f_adj)
        islands.append(island)
    return islands

def normalize_uv_island(island, uv_layer):
    """ Нормализация UV острова. """
    min_u, min_v, max_u, max_v = 1.0, 1.0, 0.0, 0.0
    for face in island:
        for loop in face.loops:
            uv = loop[uv_layer].uv
            min_u = min(min_u, uv.x)
            min_v = min(min_v, uv.y)
            max_u = max(max_u, uv.x)
            max_v = max(max_v, uv.y)

    scale_u, scale_v = max_u - min_u, max_v - min_v

    for face in island:
        for loop in face.loops:
            uv = loop[uv_layer].uv
            uv.x = (uv.x - min_u) / scale_u
            uv.y = (uv.y - min_v) / scale_v

def normalize_uv_for_object(obj):
    """ Нормализация UV для одного объекта. """
    bpy.context.view_layer.objects.active = obj
    bpy.ops.object.mode_set(mode='EDIT')
    bm = bmesh.from_edit_mesh(obj.data)
    uv_layer = bm.loops.layers.uv.verify()
    islands = get_uv_islands(bm, uv_layer)

    for island in islands:
        normalize_uv_island(island, uv_layer)

    bmesh.update_edit_mesh(obj.data)
    bpy.ops.object.mode_set(mode='OBJECT')

def main():
    selected_objects = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']

    if not selected_objects:
        print("Не найдены выделенные меш-объекты")
        return

    for obj in selected_objects:
        normalize_uv_for_object(obj)

if __name__ == "__main__":
    main()
Leave a Comment