import bpy import os import sys import types import importlib sys.path.insert(1, 'INSERT PATH TO uvcalc_lightmap.py and uvcalc_lightmap_kd.py') def reload_package(package): assert (hasattr(package, "__package__")) fn = package.__file__ fn_dir = os.path.dirname(fn) + os.sep module_visit = {fn} del fn def reload_recursive_ex(module): importlib.reload(module) for module_child in vars(module).values(): if isinstance(module_child, types.ModuleType): fn_child = getattr(module_child, "__file__", None) if (fn_child is not None) and fn_child.startswith(fn_dir): if fn_child not in module_visit: # print("reloading:", fn_child, "from", module) module_visit.add(fn_child) reload_recursive_ex(module_child) return reload_recursive_ex(package) import uvcalc_lightmap_kd import uvcalc_lightmap reload_package(uvcalc_lightmap_kd) def calculate_area(uv): if len(uv) != 3: raise ValueError("Input must contain exactly 3 tuples for triangle vertices.") x1, y1 = uv[0] x2, y2 = uv[1] x3, y3 = uv[2] area = 0.5 * abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))) return area def do(): meshes = list({ me for obj in bpy.context.selected_objects if obj.type == 'MESH' if (me := obj.data).polygons and me.library is None }) mesh = meshes[0] bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.select_all(action='SELECT') bpy.ops.object.mode_set(mode='OBJECT') uvcalc_lightmap.lightmap_uvpack(meshes) # default version uvmap = mesh.uv_layers.active uv1 = [] for tri in mesh.loop_triangles: for i in range(3): loop_index = tri.loops[i] uv1.append(tuple(uvmap.data[loop_index].uv)) uvcalc_lightmap_kd.lightmap_uvpack(meshes) # new version uv2 = [] for tri in mesh.loop_triangles: for i in range(3): loop_index = tri.loops[i] uv2.append(tuple(uvmap.data[loop_index].uv)) equal = True for i, uv in enumerate(uv1): if abs(uv[0] - uv2[i][0]) > 0.00001 or abs(uv[0] - uv2[i][0]) > 0.00001: equal = False break area_diff = 0.0 if not equal: for i in range(int(len(uv1) / 3)): area_diff = area_diff + abs(calculate_area(uv1[i * 3:i * 3 + 3]) - calculate_area(uv2[i * 3:i * 3 + 3])) if equal: print("Test success. Unwrappings are the same.") else: print(f"Test failed. Unwraps differ. Difference: {area_diff * 100.0}%") bpy.ops.object.mode_set(mode='EDIT') bpy.context.scene.tool_settings.use_uv_select_sync = True bpy.ops.uv.select_overlap() bpy.ops.object.mode_set(mode='OBJECT') for f in mesh.polygons: if f.select: print("Error: Overlap in generated uv!") return do()