ModelTranslator: 新增 uv_preview.render_uv_png——PIL 绘 UV 线框 PNG

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
ud18010
2026-07-21 11:37:19 +08:00
co-authored by Claude Opus 4.8
parent 8fe0a3f7ae
commit a19e342bf4
2 changed files with 79 additions and 0 deletions
@@ -0,0 +1,48 @@
import os
import sys
import tempfile
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import uv_preview as up
from PIL import Image
class TestToPx(unittest.TestCase):
def test_origin_bottom_left_maps_to_bottom_left_pixel(self):
# UV(0,0) 原点在左下 -> 图像左下角(x=pad, y=size-pad
x, y = up._to_px(0.0, 0.0, size=100, pad=10)
self.assertAlmostEqual(x, 10.0)
self.assertAlmostEqual(y, 90.0)
def test_top_right(self):
x, y = up._to_px(1.0, 1.0, size=100, pad=10)
self.assertAlmostEqual(x, 90.0)
self.assertAlmostEqual(y, 10.0)
class TestRenderUvPng(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.mkdtemp()
def _out(self, name="uv.png"):
return os.path.join(self.tmp, name)
def test_writes_png_of_requested_size(self):
polys = [[[0.1, 0.1], [0.4, 0.1], [0.25, 0.4]],
[[0.6, 0.6], [0.9, 0.6], [0.9, 0.9], [0.6, 0.9]]]
out = up.render_uv_png(polys, self._out(), size=128)
self.assertTrue(os.path.isfile(out))
with Image.open(out) as im:
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "PNG")
def test_empty_polygons_still_writes_blank(self):
out = up.render_uv_png([], self._out("blank.png"), size=64)
self.assertTrue(os.path.isfile(out))
with Image.open(out) as im:
self.assertEqual(im.size, (64, 64))
def test_degenerate_polygon_skipped_no_crash(self):
out = up.render_uv_png([[[0.5, 0.5]]], self._out("deg.png"), size=64)
self.assertTrue(os.path.isfile(out))