2023-08-16 00:20:26 +10:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Blender Authors
|
2023-06-15 13:09:04 +10:00
|
|
|
#
|
2022-08-25 17:59:39 +12:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
"bmesh_linked_uv_islands",
|
|
|
|
)
|
|
|
|
|
2022-08-25 16:41:22 +10:00
|
|
|
|
2022-08-25 17:59:39 +12:00
|
|
|
def match_uv(face, vert, uv, uv_layer):
|
|
|
|
for loop in face.loops:
|
|
|
|
if loop.vert == vert:
|
|
|
|
return uv == loop[uv_layer].uv
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def bmesh_linked_uv_islands(bm, uv_layer):
|
|
|
|
"""
|
2022-08-25 16:59:12 +10:00
|
|
|
Returns lists of faces connected by UV islands.
|
2022-08-25 17:59:39 +12:00
|
|
|
|
2022-08-25 16:41:22 +10:00
|
|
|
For meshes use :class:`bpy.types.Mesh.mesh_linked_uv_islands` instead.
|
2022-08-25 17:59:39 +12:00
|
|
|
|
|
|
|
:arg bm: the bmesh used to group with.
|
2022-08-25 16:41:22 +10:00
|
|
|
:type bmesh: :class:`BMesh`
|
2022-08-25 17:59:39 +12:00
|
|
|
:arg uv_layer: the UV layer to source UVs from.
|
2022-08-25 16:41:22 +10:00
|
|
|
:type bmesh: :class:`BMLayerItem`
|
2022-08-25 17:59:39 +12:00
|
|
|
:return: list of lists containing polygon indices
|
2024-11-03 15:42:19 +11:00
|
|
|
:rtype: list[list[int]]
|
2022-08-25 17:59:39 +12:00
|
|
|
"""
|
|
|
|
|
|
|
|
result = []
|
2022-08-25 16:41:22 +10:00
|
|
|
used = set()
|
2022-08-25 17:59:39 +12:00
|
|
|
for seed_face in bm.faces:
|
2022-08-25 16:59:12 +10:00
|
|
|
if seed_face in used:
|
2022-08-25 16:41:22 +10:00
|
|
|
continue # Face has already been processed.
|
2022-08-25 16:59:12 +10:00
|
|
|
used.add(seed_face)
|
|
|
|
island = [seed_face]
|
2022-08-25 16:41:22 +10:00
|
|
|
stack = [seed_face] # Faces still to consider on this island.
|
2022-08-25 17:59:39 +12:00
|
|
|
while stack:
|
|
|
|
current_face = stack.pop()
|
|
|
|
for loop in current_face.loops:
|
|
|
|
v = loop.vert
|
|
|
|
uv = loop[uv_layer].uv
|
|
|
|
for f in v.link_faces:
|
2022-08-25 16:59:12 +10:00
|
|
|
if f is current_face or f in used:
|
2022-08-25 17:59:39 +12:00
|
|
|
continue
|
|
|
|
if not match_uv(f, v, uv, uv_layer):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# `f` is part of island, add to island and stack
|
2022-08-25 16:59:12 +10:00
|
|
|
used.add(f)
|
|
|
|
island.append(f)
|
2022-08-25 17:59:39 +12:00
|
|
|
stack.append(f)
|
|
|
|
result.append(island)
|
|
|
|
|
|
|
|
return result
|