# -*- coding: utf-8 -*- """vrmio.py - GLB / glTF 容器读写 + 纯 Python 图片编解码。 只依赖标准库。提供: GLBBuilder —— 构造 glTF JSON + 单一 BIN chunk read_glb —— 拆开 .vrm/.glb 得到 (json dict, bin bytes) write_glb —— 打包成 .vrm/.glb to_png / decode_image / save_image —— 贴图处理 """ import io import json import os import struct import zlib GLB_MAGIC = 0x46546C67 # "glTF" CHUNK_JSON = 0x4E4F534A # "JSON" CHUNK_BIN = 0x004E4942 # "BIN\0" FLOAT = 5126 UNSIGNED_BYTE = 5121 SHORT = 5122 UNSIGNED_SHORT = 5123 UNSIGNED_INT = 5125 _COMP_FMT = {FLOAT: "= 0 and world[p] else loc return world def _depth(i, parent): d = 0 while parent[i] >= 0: d += 1 i = parent[i] return d def world_translation(m): return (m[12], m[13], m[14]) # ----------------------------------------------------------------- images -- def encode_png(width, height, rows_rgba): """rows_rgba: 每行为 bytes,长度 width*4。""" raw = bytearray() for r in rows_rgba: raw.append(0) raw += r def chunk(typ, data): return (struct.pack(">I", len(data)) + typ + data + struct.pack(">I", zlib.crc32(typ + data) & 0xFFFFFFFF)) out = bytearray(b"\x89PNG\r\n\x1a\n") out += chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0)) out += chunk(b"IDAT", zlib.compress(bytes(raw), 6)) out += chunk(b"IEND", b"") return bytes(out) def _png_from_rgba(w, h, pix): """pix: bytes,长度 w*h*4,按行自上而下。""" rows = [pix[y * w * 4:(y + 1) * w * 4] for y in range(h)] return encode_png(w, h, rows) def _decode_bmp(data): if data[:2] != b"BM": return None off = struct.unpack_from(" 0 h = abs(h) stride = ((w * bpp + 31) // 32) * 4 pix = bytearray(w * h * 4) src = off for y in range(h): sy = (h - 1 - y) if flip else y row = data[src + sy * stride: src + sy * stride + stride] for x in range(w): if bpp == 24: b, g, r = row[x * 3], row[x * 3 + 1], row[x * 3 + 2] a = 255 else: b, g, r, a = row[x * 4], row[x * 4 + 1], row[x * 4 + 2], row[x * 4 + 3] o = (y * w + x) * 4 pix[o] = r pix[o + 1] = g pix[o + 2] = b pix[o + 3] = a return _png_from_rgba(w, h, bytes(pix)) def _decode_tga(data): idlen = data[0] cmap_type = data[1] img_type = data[2] w = struct.unpack_from("= w * h: break b, g, r = px[0], px[1], px[2] a = px[3] if bpp == 4 else 255 o = idx * 4 pix[o] = r pix[o + 1] = g pix[o + 2] = b pix[o + 3] = a idx += 1 if not top: # 底到顶存储,翻过来 rows = [bytes(pix[y * w * 4:(y + 1) * w * 4]) for y in range(h)] rows.reverse() pix = bytearray(b"".join(rows)) return _png_from_rgba(w, h, bytes(pix)) # ------------------------------------------------------------------- DDS ---- # XPS / XNALara 的贴图大量是 DDS(实测轩辕剑柒的样本是 2048×2048 的 DXT1 与 # 512×512 的 DXT5)。MMD 本体虽然认 .dds(见 VPVP wiki 的「拡張子」表,所以 # 转换时原样拷过去就行),但 GUI 预览的图片解码走的是纯标准库路线,没装 # Pillow 时只认 PNG/TGA/BMP,DDS 会解不出来、预览变灰模。这里补一个 # DXT1/DXT3/DXT5 与未压缩 RGB(A) 的解码,解成 PNG 后预览就能正常显示。 # 一个索引字节 → 4 个 2 位索引(低位在前),DXT 全系都用这个位序 _DXT_Q = tuple(((b & 3), ((b >> 2) & 3), ((b >> 4) & 3), ((b >> 6) & 3)) for b in range(256)) def _rgb565(v): r = (v >> 11) & 0x1F g = (v >> 5) & 0x3F b = v & 0x1F return ((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2)) def _dxt_palette(c0, c1, allow_transparent): """(c0, c1) → 4 个 RGBA 字节串。 allow_transparent(只有 DXT1 传 True)时,c0 <= c1 表示用「3 色 + 全透明」 档,第 4 个是透明黑 —— 这是 DXT1 唯一的 alpha 来源。 """ r0, g0, b0 = _rgb565(c0) r1, g1, b1 = _rgb565(c1) p0 = bytes((r0, g0, b0, 255)) p1 = bytes((r1, g1, b1, 255)) if allow_transparent and c0 <= c1: return (p0, p1, bytes(((r0 + r1) // 2, (g0 + g1) // 2, (b0 + b1) // 2, 255)), b"\x00\x00\x00\x00") return (p0, p1, bytes(((2 * r0 + r1) // 3, (2 * g0 + g1) // 3, (2 * b0 + b1) // 3, 255)), bytes(((r0 + 2 * r1) // 3, (g0 + 2 * g1) // 3, (b0 + 2 * b1) // 3, 255))) def _dxt5_alpha_ramp(a0, a1): """DXT5 的 8 个 alpha 值:a0 > a1 是 6 插值档,否则 4 插值 + 0/255。""" if a0 > a1: return (a0, a1) + tuple( ((7 - i) * a0 + i * a1) // 7 for i in range(1, 7)) return (a0, a1) + tuple( ((5 - i) * a0 + i * a1) // 5 for i in range(1, 5)) + (0, 255) def _decode_dds(data): """DDS → PNG 字节;不支持的子格式返回 None。""" if data[:4] != b"DDS " or len(data) < 128: return None hdr = struct.unpack_from("<7I", data, 4) height, width = hdr[2], hdr[3] if not width or not height or width > 16384 or height > 16384: return None fourcc = data[84:88] rgb_bits = struct.unpack_from("= height: break q = Q[(bits >> (8 * r)) & 0xFF] row = (pal[q[0]] + pal[q[1]] + pal[q[2]] + pal[q[3]]) o = (y0 + r) * stride + x0 * 4 if x0 + 4 <= width: pix[o:o + 16] = row else: pix[o:o + (width - x0) * 4] = \ row[:(width - x0) * 4] elif fourcc == b"DXT3": for by in range(bh): y0 = by * 4 for bx in range(bw): al = data[off:off + 8] c0 = data[off + 8] | (data[off + 9] << 8) c1 = data[off + 10] | (data[off + 11] << 8) bits = struct.unpack_from("= height: break o = (y0 + r) * stride + x0 * 4 for k in range(4): if x0 + k >= width: break i = r * 4 + k n = (al[i >> 1] >> (4 * (i & 1))) & 0xF c = pal[(bits >> (2 * i)) & 3] pix[o + k * 4] = c[0] pix[o + k * 4 + 1] = c[1] pix[o + k * 4 + 2] = c[2] pix[o + k * 4 + 3] = n * 17 else: # DXT5 for by in range(bh): y0 = by * 4 for bx in range(bw): ramp = _dxt5_alpha_ramp(data[off], data[off + 1]) abits = int.from_bytes(data[off + 2:off + 8], "little") c0 = data[off + 8] | (data[off + 9] << 8) c1 = data[off + 10] | (data[off + 11] << 8) bits = struct.unpack_from("= height: break o = (y0 + r) * stride + x0 * 4 for k in range(4): if x0 + k >= width: break i = r * 4 + k c = pal[(bits >> (2 * i)) & 3] pix[o + k * 4] = c[0] pix[o + k * 4 + 1] = c[1] pix[o + k * 4 + 2] = c[2] pix[o + k * 4 + 3] = ramp[(abits >> (3 * i)) & 7] return _png_from_rgba(width, height, bytes(pix)) if fourcc == b"\x00\x00\x00\x00" and rgb_bits in (24, 32): rmask, gmask, bmask, amask = struct.unpack_from("<4I", data, 92) bpp = rgb_bits // 8 if len(data) < 128 + width * height * bpp: return None # 常见布局走快路:24 位 BGR / 32 位 BGRA if (rgb_bits == 24 and rmask == 0xFF0000 and gmask == 0xFF00 and bmask == 0xFF): src = data[128:128 + width * height * 3] for i in range(width * height): o = i * 4 pix[o] = src[i * 3 + 2] pix[o + 1] = src[i * 3 + 1] pix[o + 2] = src[i * 3] pix[o + 3] = 255 return _png_from_rgba(width, height, bytes(pix)) if (rgb_bits == 32 and rmask == 0xFF0000 and gmask == 0xFF00 and bmask == 0xFF and amask == 0xFF000000): src = data[128:128 + width * height * 4] for i in range(width * height): o = i * 4 pix[o] = src[i * 4 + 2] pix[o + 1] = src[i * 4 + 1] pix[o + 2] = src[i * 4] pix[o + 3] = src[i * 4 + 3] return _png_from_rgba(width, height, bytes(pix)) # 其它掩码:按位段通用抽取 def _shift(mask): if not mask: return 0, 0 s = 0 while not (mask >> s) & 1: s += 1 bits = bin(mask >> s).count("1") return s, bits rs, rb = _shift(rmask) gs, gb = _shift(gmask) bs, bb = _shift(bmask) as_, ab = _shift(amask) def _comp(v, s, n, default=255): if not n: return default x = (v >> s) & ((1 << n) - 1) return x * 255 // ((1 << n) - 1) src = data[128:128 + width * height * bpp] for i in range(width * height): v = int.from_bytes(src[i * bpp:(i + 1) * bpp], "little") o = i * 4 pix[o] = _comp(v, rs, rb) pix[o + 1] = _comp(v, gs, gb) pix[o + 2] = _comp(v, bs, bb) pix[o + 3] = _comp(v, as_, ab, 255) return _png_from_rgba(width, height, bytes(pix)) return None def _bmp_pil(Image, data): """BMP → Pillow 图像。24 / 32 位的像素和 _decode_bmp 一样(32 位的第 4 个字节当 alpha,Pillow 自己 打开时会把它当成填充字节丢掉);纯 Python 版不认的(8 位调色板 / 灰度等)交给 Pillow 自己解。""" off = struct.unpack_from(" 0 else 1) def _decode_pil(data, kind): """BMP / TGA / DDS → PNG 字节,用 Pillow(C 解码,比下面的纯 Python 版快几十倍)。 没装 Pillow 或它解不了时返回 None,交给纯 Python 版。没有 alpha 的图存成 RGB PNG。""" try: from PIL import Image except ImportError: return None try: if kind == "BMP": im = _bmp_pil(Image, data) if im is None: return None else: im = Image.open(io.BytesIO(data), formats=[kind]) im.load() if im.mode not in ("RGB", "RGBA"): alpha = im.mode in ("LA", "PA", "La", "RGBa") or "transparency" in im.info im = im.convert("RGBA" if alpha else "RGB") buf = io.BytesIO() im.save(buf, "PNG") return buf.getvalue() except Exception: # noqa: BLE001 罕见子格式:纯 Python 版再试 return None def decode_image(data): """任意常见贴图字节 → PNG 字节;无法处理时返回 None。BMP / TGA / DDS 装了 Pillow 时用 Pillow 解。""" if not data: return None if data[:8] == b"\x89PNG\r\n\x1a\n": return data if data[:3] == b"\xff\xd8\xff": return data # JPEG 直接透传 for kind, ok, pure in ( ("BMP", data[:2] == b"BM", _decode_bmp), ("DDS", data[:4] == b"DDS ", _decode_dds), ("TGA", len(data) > 18 and data[2] in (2, 3, 10, 11) and data[1] in (0, 1), _decode_tga)): if ok: out = _decode_pil(data, kind) if out: return out try: return pure(data) except Exception: return None return None def decode_rgba(data, max_pixels=4 << 20): """贴图字节 → (宽, 高, 行列表),每行是 (r, g, b, a) 元组的列表;失败返回 None。 纯 Python,给 toon 渐变这类小图取色用(BMP / TGA / DDS 先转成 PNG)。 只支持 8 位、非隔行的 PNG;超过 max_pixels 的大图不解(太慢)。 """ if not data: return None if data[:8] != b"\x89PNG\r\n\x1a\n": data = decode_image(data) if not data or data[:8] != b"\x89PNG\r\n\x1a\n": return None p = 8 idat = [] w = h = bd = ct = il = 0 plte = trns = None while p + 8 <= len(data): ln, ty = struct.unpack(">I4s", data[p:p + 8]) body = data[p + 8:p + 8 + ln] p += 12 + ln if ty == b"IHDR": w, h, bd, ct, _, _, il = struct.unpack(">IIBBBBB", body[:13]) elif ty == b"IDAT": idat.append(body) elif ty == b"PLTE": plte = body elif ty == b"tRNS": trns = body elif ty == b"IEND": break if bd != 8 or il or ct not in (0, 2, 3, 4, 6) or w * h > max_pixels: return None if ct == 3 and not plte: return None bpp = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[ct] stride = w * bpp try: raw = zlib.decompress(b"".join(idat)) except zlib.error: return None if len(raw) < h * (stride + 1): return None rows = [] prev = bytearray(stride) for y in range(h): base = y * (stride + 1) f = raw[base] out = bytearray(raw[base + 1:base + 1 + stride]) if f == 1: for x in range(bpp, stride): out[x] = (out[x] + out[x - bpp]) & 255 elif f == 2: for x in range(stride): out[x] = (out[x] + prev[x]) & 255 elif f == 3: for x in range(stride): left = out[x - bpp] if x >= bpp else 0 out[x] = (out[x] + ((left + prev[x]) >> 1)) & 255 elif f == 4: for x in range(stride): a = out[x - bpp] if x >= bpp else 0 b = prev[x] c = prev[x - bpp] if x >= bpp else 0 pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - 2 * c) out[x] = (out[x] + (a if pa <= pb and pa <= pc else (b if pb <= pc else c))) & 255 prev = out if ct == 6: rows.append([tuple(out[i:i + 4]) for i in range(0, stride, 4)]) elif ct == 2: rows.append([tuple(out[i:i + 3]) + (255,) for i in range(0, stride, 3)]) elif ct == 0: rows.append([(v, v, v, 255) for v in out]) elif ct == 4: rows.append([(out[i], out[i], out[i], out[i + 1]) for i in range(0, stride, 2)]) else: rows.append([tuple(plte[3 * v:3 * v + 3]) + ((trns[v] if trns and v < len(trns) else 255),) for v in out]) return w, h, rows def png_alpha_stats_image(im): """已经用 Pillow 打开的 PNG → 和 png_alpha_stats 相同的结果(解码时出错会抛异常)。 FBX 转换判断透明时已经解过一遍贴图,用它算好交给后面的 PMX → VRM,不用再解一遍。""" if im.mode in ("RGBA", "LA", "PA"): a = im.getchannel("A") elif im.mode == "P" and "transparency" in im.info: a = im.convert("RGBA").getchannel("A") else: return None # 没有 alpha(tRNS 色键和纯 Python 版一样忽略) h = a.histogram() return (sum(h), sum(h[:250]), sum(h[11:245])) def _png_alpha_stats_pil(data): """png_alpha_stats 的 Pillow 版(C 解码,比纯 Python 版快十倍以上,统计整张图)。 没有 alpha 通道的图看文件头就知道,不解码。没装 Pillow 或解不开时返回 NotImplemented,交给纯 Python 版。""" try: from PIL import Image except ImportError: return NotImplemented try: return png_alpha_stats_image(Image.open(io.BytesIO(data))) except Exception: # noqa: BLE001 坏图 / 罕见格式:纯 Python 版有自己的保守处理 return NotImplemented def png_alpha_stats(data, max_rows=512): """统计 PNG 的透明度分布,用来决定材质的 alphaMode。 返回 (采样像素数, 透明像素数 a<250, 半透明像素数 10I4s", data[p:p + 8]) body = data[p + 8:p + 8 + ln] p += 12 + ln if ty == b"IHDR": w, h, bd, ct, _, _, il = struct.unpack(">IIBBBBB", body[:13]) elif ty == b"IDAT": idat.append(body) elif ty == b"tRNS": trns = body elif ty == b"IEND": break if ct == 3: if not trns: return None lut = bytes(trns) + b"\xff" * (256 - len(trns)) elif ct not in (4, 6): return None # tRNS 色键这种少见情况忽略 if il or bd not in (8, 16) and ct != 3: return (1, 1, 1) # 隔行/罕见位深:保守地当半透明 try: raw = zlib.decompress(b"".join(idat)) except zlib.error: return (1, 1, 1) if ct == 3: if bd != 8: return (1, 1, 1) bpp, aoff = 1, 0 else: ch = 4 if ct == 6 else 2 bpp = ch * (bd // 8) aoff = bpp - (bd // 8) # 16 位取 alpha 的高字节 stride = w * bpp if len(raw) < h * (stride + 1): return (1, 1, 1) step = max(1, h // max_rows) filt = [raw[y * (stride + 1)] for y in range(h)] def unfilter(y, prev): """只还原第 y 行 alpha(调色板图还原索引),prev 为上一行结果或 None。""" base = y * (stride + 1) + 1 src = raw[base + aoff:base + stride:bpp] f = filt[y] if f == 0: return bytearray(src) out = bytearray(len(src)) if f == 1: a = 0 for x, v in enumerate(src): a = (v + a) & 255 out[x] = a return out up = prev if prev is not None else bytes(len(src)) if f == 2: for x, v in enumerate(src): out[x] = (v + up[x]) & 255 return out a = c = 0 for x, v in enumerate(src): b = up[x] if f == 3: a = (v + ((a + b) >> 1)) & 255 else: pa = b - c pb = a - c pc = abs(pa + pb) pa = abs(pa) pb = abs(pb) pr = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c) a = (v + pr) & 255 out[x] = a c = b return out n = trans = mid = 0 last_y, last_row = -1, None for t in range(0, h, step): if last_y >= 0 and t - last_y <= step: y, prev = last_y + 1, last_row else: y = t while y > 0 and filt[y] > 1 and y > last_y + 1: y -= 1 prev = last_row if y == last_y + 1 else None row = prev while y <= t: row = unfilter(y, prev) prev = row y += 1 last_y, last_row = t, row if ct == 3: row = row.translate(lut) n += len(row) for v in row: if v < 250: trans += 1 if 10 < v < 245: mid += 1 return (n, trans, mid) IMAGE_EXTS = (".png", ".jpg", ".jpeg", ".bmp", ".tga", ".dds", ".spa", ".sph", ".gif", ".webp", ".tif", ".tiff") class TextureFinder: """模型里记的贴图路径(相对模型所在文件夹)→ 磁盘上的文件。 依次试:记的相对路径;模型文件夹里的同名文件(作者留了自己电脑上的绝对路径之类);模型文件夹的 子文件夹里的同名文件(不分大小写,比如记的是 body.png、实际在 tex/body.png);同名不同扩展名 (记的 .png、实际是 .tga / .dds)。子文件夹按需建一次索引,最多看 max_files 个文件(不只是图片)、max_depth 层, 拖进来的是「下载」这种大文件夹时也不会慢。find 可以在线程里调用。 """ def __init__(self, base_dir, max_files=20000, max_depth=6): import threading self.base_dir = base_dir self.max_files, self.max_depth = max_files, max_depth self._index = None self._lock = threading.Lock() def _build(self): by_name, by_stem, n = {}, {}, 0 root = os.path.abspath(self.base_dir) depth0 = root.rstrip("\\/").count(os.sep) for d, dirs, files in os.walk(root): if d.count(os.sep) - depth0 >= self.max_depth: dirs[:] = [] dirs.sort() n += len(files) # 所有文件都算数:模型放在「下载」这种大文件夹里时不会一直找下去 for f in sorted(files): low = f.lower() if low.endswith(IMAGE_EXTS): p = os.path.join(d, f) by_name.setdefault(low, []).append(p) by_stem.setdefault(os.path.splitext(low)[0], []).append(p) if n >= self.max_files: break return by_name, by_stem def find(self, rel): """→ (路径, how):how 是 "exact"(就在记的位置)、"moved"(在别处按文件名找到的);找不到为 (None, None)。""" rel = (rel or "").replace("\\", "/").strip() if not rel: return None, None c = os.path.join(self.base_dir, *[x for x in rel.split("/") if x]) if not os.path.isabs(rel) else rel if os.path.isfile(c): return c, "exact" name = rel.rsplit("/", 1)[-1] c = os.path.join(self.base_dir, name) if os.path.isfile(c): return c, "moved" with self._lock: if self._index is None: self._index = self._build() by_name, by_stem = self._index cands = by_name.get(name.lower()) or by_stem.get(os.path.splitext(name.lower())[0]) or [] if not cands: return None, None # 几个同名的:选所在子文件夹和记的路径对得最多的,再选层级浅的 want = [x.lower() for x in rel.split("/")[:-1]] def score(p): parts = os.path.relpath(os.path.dirname(p), self.base_dir).lower().replace("\\", "/").split("/") return (-sum(1 for x in want if x in parts), len(parts), p) return min(cands, key=score), "moved" def load_texture(path): """读取磁盘贴图,返回 (bytes, mime);失败返回 (None, None)。""" try: with open(path, "rb") as f: data = f.read() except OSError: return None, None if data[:8] == b"\x89PNG\r\n\x1a\n": return data, "image/png" if data[:3] == b"\xff\xd8\xff": return data, "image/jpeg" out = decode_image(data) if out is None: return None, None return out, ("image/png" if out[:8] == b"\x89PNG\r\n\x1a\n" else "image/jpeg") def save_image(data, path): if data[:8] == b"\x89PNG\r\n\x1a\n": p = os.path.splitext(path)[0] + ".png" elif data[:3] == b"\xff\xd8\xff": p = os.path.splitext(path)[0] + ".jpg" elif data[:4] == b"RIFF" and data[8:12] == b"WEBP": p = os.path.splitext(path)[0] + ".webp" elif data[:4] == b"\xabKTX": p = os.path.splitext(path)[0] + ".ktx2" else: p = path with open(p, "wb") as f: f.write(data) return p