Files
AIC-Project/Tools/ModelTranslator/uv_preview.py
T
2026-07-21 11:37:19 +08:00

32 lines
1.4 KiB
Python

"""UV 线框观察图渲染:把 UV 多边形(归一化 0-1 坐标)画成 PNG 供人工查阅。
在系统 Python(有 Pillow)中运行——Blender 自带 Python 无 PIL,故 UV 几何由
bl_decimate 导出为 JSON,本模块负责绘制。"""
from PIL import Image, ImageDraw
def _to_px(u, v, size, pad):
"""UV(0-1, 原点左下) -> 像素(原点左上),含边距。"""
span = size - 2 * pad
x = pad + u * span
y = pad + (1.0 - v) * span # V 轴翻转:图像 y 向下
return x, y
def render_uv_png(polygons, out_png, size=1024, pad=8,
line=(30, 30, 30, 255), fill=(80, 140, 220, 64)):
"""polygons: [[[u,v], ...], ...] 每个多边形一组 UV 顶点。
白底 + 半透明填充 + 深色线框;写 PNG 到 out_png,返回 out_png。"""
base = Image.new("RGBA", (size, size), (255, 255, 255, 255))
overlay = Image.new("RGBA", (size, size), (0, 0, 0, 0))
draw = ImageDraw.Draw(overlay, "RGBA")
for poly in polygons:
pts = [_to_px(u, v, size, pad) for (u, v) in poly]
if len(pts) >= 3:
draw.polygon(pts, fill=fill)
for poly in polygons:
pts = [_to_px(u, v, size, pad) for (u, v) in poly]
if len(pts) >= 2:
draw.line(pts + pts[:1], fill=line, width=1)
Image.alpha_composite(base, overlay).convert("RGB").save(out_png, "PNG")
return out_png