初始化 VRMify 增强版(VRMify Enhanced)
Tests / test (push) Has been cancelled

导入上游源码,为所有面向用户的品牌显示处添加「增强版 / Enhanced」标识;包含 unitypackage→VRM 的纯色 Toon 材质、兽耳 SpringBone、Unity 描边、UV 接缝白边补偿修复,以及默认不强制 T-pose。146 个单元测试通过。
This commit is contained in:
2026-11-15 23:21:59 +08:00
commit 60cce66993
114 changed files with 56114 additions and 0 deletions
+129
View File
@@ -0,0 +1,129 @@
# 工具
> VRMify 增强版 · Enhanced fork 的开发与批量处理脚本。
开发和批量处理用的脚本,不随程序打包。转换器本身(`convert/`、`formats/`)只用 Python 标准库;
这里的检查工具另需 `jsonschema`(`pip install -r requirements-dev.txt`)和 Node.js。
## 批量转换 `batch_convert.py`
```bash
python tools/batch_convert.py model -o vrm
python tools/batch_convert.py model -o vrm --only 穗穗 爱弥斯 --variants spring
```
* 模型目录下每个子文件夹算一个角色,递归找所有 `.pmx`;文件夹里的 `.zip` 中的 PMX 也会解出来一起转
(在临时目录里解压,不改动模型目录)。
* 默认输出两份:`<输出>/有SpringBone/<角色>/` 和 `<输出>/无SpringBone/<角色>/`。
* 「<名字>_by_<游戏>_<哈希>」形式的文件夹名会整理成「<游戏>—<名字>」。
* `--report 结果.json` 保存每个模型的日志。
## 静态检查 `qa/check_vrm.py`
```bash
python tools/qa/check_vrm.py vrm # 递归检查目录下所有 .vrm
python tools/qa/check_vrm.py a.vrm -v # 通过的也打印摘要
```
检查 humanoid 结构、网格、表情绑定、SpringBone 链,以及 **VRM 官方 JSON Schema**
(第一次运行时从 vrm-c/vrm-specification 下载固定版本,缓存在 `qa/.schema-cache/`)。
UniVRM 对不合规数据很少容错——比如材质颜色绑定少一个分量、BlendShape 重名,都会让 Unity 里的
导入直接中断(表现为卡在加载中),这些都会被它报出来。有错误时退出码为 1。
## 动态测试 `qa/dynamics.py`
```bash
cd tools/qa/harness && npm install # 第一次
python tools/qa/dynamics.py vrm/有SpringBone -j 4
python tools/qa/dynamics.py a.vrm --fps 30 --jitter 0.3 --motions idle,walk_real,run,leg_raise
```
用 three.js + three-vrm 在 Node 里无界面地跑 SpringBone(和 UniVRM 同一套规则),测:
| 指标 | 含义 | 参考范围(VRoid / 官方示例模型) |
|---|---|---|
| 静止偏移 | 站着不动 2 秒后弹簧末端离绑定姿势的距离 | < 5 cm |
| hf | 末端 8 Hz 以上的颤动能量 / 链长 ×1000(平均 / 最大) | 头发 2~16,裙摆 3~58 |
| swing | 末端最大摆幅 / 链长 | — |
| 穿腿 | 裙摆关节落在模型自己腿部碰撞体里的比例(含静止时的重叠,和 idle 对比看) | — |
| 张开 | 相邻裙摆链末端比静止时多分开的距离(平均 / 90% / 最大,cm);腿把裙子从中间劈开时变大 | 原 PMX 的 MMD 物理:走路 4~15 |
动作:`idle`(放下手臂、轻微晃动)、`walk_real` / `run`(带根运动的走路 / 跑步)、`leg_raise`
(抬腿 70°)等,定义在 `qa/harness/motions.mjs`。超出参考范围的数值标「!」,`--strict` 时返回 1。
## 回归对比 `regress.py`
改转换代码前后,用本机的真实模型确认输出有没有变、变在哪里:
```bash
python tools/regress.py record model/角色A model/角色B/某.pmx # 记下基准(默认 VRM 1.0,--spec 0x 记 0.x)
python tools/regress.py check # 用当前代码重转、校验、对比基准
python tools/regress.py check --code ../旧版本的代码目录 # 用另一份代码转(比如 git worktree)
```
* 基准在 `tests/.golden/`(不进仓库:模型许可不允许再分发,衍生数据也只留在本机),记录 glTF JSON 全文和二进制块的 sha256。
* `check` 对每个样本:转换 → `check_vrm` 结构和 Schema 校验 → 列出 JSON 里不同的路径(最多 20 条)、二进制块是否一致。
有错误或差异时退出码为 1。变化是否符合预期要自己判断,确认后重新 `record`。
## 骨骼自检扫描 `scan_models.py`
```bash
python tools/scan_models.py model ~/Downloads -o 结果.txt
```
对目录里的每个 PMX(压缩包在临时目录里解开)只跑骨骼处理和转换时的自检,不写 VRM,几秒一个:
付与骨合并了几根、按比例拆权重的骨有几根,以及自检问题——网格在 MMD 里跟着关节转、在 VRM 里却不跟,
或者 humanoid 骨位置明显不对(在身体另一侧、离父骨或网格太远)。这些问题转换时也会写进日志。
改骨骼相关的代码前后各跑一次对比,发版前跑一次;有自检问题时退出码为 1。
## 表情截图 `qa/snapshots.py`
```bash
python tools/qa/snapshots.py a.vrm -o a.png # 全身、脸、表情预设、口型、眨眼、视线四个方向
python tools/qa/snapshots.py 旧/a.vrm 新/a.vrm -o 对比.png # 每个输入一行,上下对照
python tools/qa/snapshots.py model/角色/角色.pmx -o a.png # 不是 VRM 的先用当前代码转一份(临时目录)
python tools/qa/snapshots.py a.vrm --custom --cols 12 -o a.png # 再加上自定义表情,每段 12 列
```
用无界面的 Edge / Chrome(`--browser` 指定路径)加载界面预览用的 `ui/viewer/viewer-core.js` 来拍,
和程序里看到的一致;WebGL 走软件渲染,不依赖显卡,一个模型几秒。模型没有的表情格子标「无」。
改表情相关的代码前后各拍一张对照,比只看 `regress.py` 的 JSON 差异直观。
## 单元测试 `tests/`
```bash
python -m unittest discover -s tests -v
```
不需要真实模型:`tests/synth.py` 合成一个小的 MMD 人形 PMX(标准骨名、头发物理、几个表情、两张贴图),
测转换(1.0 / 0.x、元信息、结果确定性、贴图瘦身)、转换后调整(`vrm_edit` 的套用 / 还原 / 幂等)和 zip 输入。
装了 jsonschema 时顺带做官方 Schema 校验;没装 Pillow 时跳过贴图瘦身的测试。
## 程序图标 `make_icon.py`
```bash
python tools/make_icon.py # → ui/icon.ico(exe 图标)+ ui/web/icon.png(页面图标),需要 Pillow
```
圆角蓝底 + 白色菱形,和界面左上角的「◆」一致;改颜色、形状就改这个脚本再重新生成。
## 打包发布 `build_release.py`
```bash
python tools/build_release.py # → dist/VRMify-v<版本>-win64.zip
python tools/build_release.py --expect-version v0.1.0 # tag 和 ui/version.py 不一致时报错(Actions 里这么调用)
```
按 `VRMify.spec` 打包,把 `LICENSE`、README 放进程序文件夹,再压成绿色版 zip。GitHub Actions 的
`release.yml` 用的也是它,流程见 README 的「开发」一节。
## 冒烟测试 `smoke_exe.py`
```bash
python tools/smoke_exe.py dist/VRMify-v0.1.2-win64.zip # 发布 zip、解开的程序文件夹或 exe 都行
```
单元测试测源码,这里测**打包以后**的 exe:用临时数据目录启动 `--no-open`,通过它自己的 HTTP 接口
加入合成的 PMX / zip / FBX / unitypackage 并转换,检查版本号、页面和预览库、每个输出过 `check_vrm`、
PMX 原生预览和调整面板接口。PyInstaller 漏打模块或页面资源时会在这里失败(退出码 1,并打印 exe 的输出)。
`release.yml` 打包后自动跑,不通过就不建 Release。
+146
View File
@@ -0,0 +1,146 @@
# -*- coding: utf-8 -*-
"""批量把一个目录下的 PMX 转成 VRM。
python tools/batch_convert.py [模型目录] [-o 输出目录] [--only 关键字 ...]
[--variants both|spring|nospring] [--spec 1.0|0.x]
[-j 进程数] [--report 结果.json]
* 模型目录下每个子文件夹算一个角色,递归找其中所有 .pmx;子文件夹里的 .zip
(作者常把道具、差分模型打包在里面)也会解出其中的 .pmx 一起转。解压在临时
目录里进行(连同角色文件夹的贴图一起复制过去,相对路径才找得到),不改动
模型目录本身。
* 输出:<输出目录>/有SpringBone/<角色>/<名>.vrm 和 <输出目录>/无SpringBone/<角色>/<名>.vrm
(--variants 只要其中一种时不分这一层)。
* 角色文件夹名形如「<名字>_by_<游戏>_<哈希>」(模之屋的下载格式)时整理成
「<游戏>—<名字>」,并去掉「(修模绑骨后)」这类说明和结尾的版本号;其它名字原样使用。
* 最后打印每个模型的结果;用占位骨补齐 humanoid 的(道具、非人形)会标出来。
"""
import argparse
import glob
import json
import os
import shutil
import sys
import tempfile
import time
import zipfile
from concurrent.futures import ProcessPoolExecutor
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path[:0] = [os.path.join(ROOT, "convert"), os.path.join(ROOT, "formats")]
import pipeline # noqa: E402
char_name = pipeline.nice_name # 角色文件夹名 → 输出用的角色名
def collect(model_dir, extract_dir, only=None):
"""→ [(角色文件夹名, pmx 路径, 来源)];only = 文件夹名须包含其中之一的关键字。"""
jobs = []
for d in sorted(glob.glob(os.path.join(model_dir, "*"))):
if not os.path.isdir(d):
continue
folder = os.path.basename(d)
if only and not any(k in folder for k in only):
continue
have = set()
for p in sorted(glob.glob(os.path.join(d, "**", "*.pmx"), recursive=True)):
jobs.append((folder, p, "folder"))
have.add(os.path.basename(p))
zips = sorted(glob.glob(os.path.join(d, "*.zip")))
if not zips:
continue
copy = os.path.join(extract_dir, folder)
for z in zips:
with zipfile.ZipFile(z) as zf:
for info, names in pipeline.zip_members(zf):
base = os.path.basename(names[0])
if not base.lower().endswith(".pmx") or base in have:
continue
if not os.path.isdir(copy):
shutil.copytree(d, copy, ignore=shutil.ignore_patterns(
"*.zip", "*.vrm", "*.pmx"))
dst = os.path.join(copy, base)
with zf.open(info) as src, open(dst, "wb") as f:
shutil.copyfileobj(src, f)
jobs.append((folder, dst, "zip:" + os.path.basename(z)))
have.add(base)
return jobs
def run(job):
folder, pmx, src, out_dir, variants, spec = job
sys.path[:0] = [os.path.join(ROOT, "convert"), os.path.join(ROOT, "formats")]
import pmx2vrm
stem = os.path.splitext(os.path.basename(pmx))[0]
res = {"folder": folder, "char": char_name(folder), "pmx": pmx, "src": src, "stem": stem}
for label, spring in variants:
sub = [out_dir] + ([label] if len(variants) > 1 else []) + [res["char"], stem + ".vrm"]
out = os.path.join(*sub)
logs = []
t = time.time()
try:
st = pmx2vrm.convert(pmx, out, spec=spec, spring=spring, meta={"title": stem},
log=lambda m, tag=None: logs.append((tag, m)))
res[label] = {"ok": True, "out": out, "mb": st["bytes"] / 1048576.0,
"sec": time.time() - t, "logs": logs}
except Exception as e: # noqa: BLE001 一个模型失败不影响其它
import traceback
res[label] = {"ok": False, "err": "%s: %s" % (type(e).__name__, e),
"tb": traceback.format_exc()[-2000:], "logs": logs}
return res
def main(argv=None):
ap = argparse.ArgumentParser(description="批量把目录下的 PMX 转成 VRM")
ap.add_argument("models", nargs="?", default=os.path.join(ROOT, "model"),
help="模型目录(每个子文件夹一个角色),默认 ./model")
ap.add_argument("-o", "--out", default=os.path.join(ROOT, "vrm"), help="输出目录,默认 ./vrm")
ap.add_argument("--only", nargs="+", metavar="关键字",
help="只转文件夹名包含这些关键字的角色,如 --only 穗穗 爱弥斯")
ap.add_argument("--variants", choices=("both", "spring", "nospring"), default="both",
help="同时输出有 / 无 SpringBone 两份(默认),或只要其中一种")
ap.add_argument("--spec", choices=("1.0", "0.x"), default="1.0", help="VRM 版本,默认 1.0")
ap.add_argument("-j", "--jobs", type=int, default=max(1, min(6, (os.cpu_count() or 2) - 1)),
help="并行进程数")
ap.add_argument("--report", help="把每个模型的结果和日志写成 JSON(给 tools/qa 用)")
a = ap.parse_args(argv)
variants = {"both": [("有SpringBone", True), ("无SpringBone", False)],
"spring": [("有SpringBone", True)],
"nospring": [("无SpringBone", False)]}[a.variants]
if a.spec != "1.0" and a.variants != "nospring":
print("提示:VRM 0.x 不输出 SpringBone,「有SpringBone」那份会和「无」一样")
extract = tempfile.mkdtemp(prefix="vrmify_zip_")
try:
jobs = collect(a.models, extract, a.only)
names = [(char_name(f), os.path.basename(p)) for f, p, _ in jobs]
dup = {n for n in names if names.count(n) > 1}
if dup:
sys.exit("输出文件重名:%s" % sorted(dup))
print("共 %d 个 PMX → %s" % (len(jobs), a.out))
args = [(f, p, s, a.out, variants, a.spec) for f, p, s in jobs]
with ProcessPoolExecutor(max_workers=a.jobs) as ex:
results = list(ex.map(run, args))
finally:
shutil.rmtree(extract, ignore_errors=True)
n_fail = 0
for r in results:
oks = [r[label]["ok"] for label, _ in variants]
n_fail += oks.count(False)
first = r[variants[0][0]]
ph = sum(1 for _, m in first.get("logs", []) if "占位骨骼" in m)
print("%s %-16s %-28s %s%s" % (
"OK " if all(oks) else "FAIL", r["char"][:16], r["stem"][:28],
("占位骨 %d 根" % ph) if ph else "",
"" if all(oks) else " " + "; ".join(r[l].get("err", "") for l, _ in variants)))
if a.report:
with open(a.report, "w", encoding="utf-8") as f:
json.dump({"variants": [l for l, _ in variants], "results": results}, f,
ensure_ascii=False, indent=1)
print("完成:%d 个模型,失败 %d 次" % (len(results), n_fail))
return 1 if n_fail else 0
if __name__ == "__main__":
sys.exit(main())
+70
View File
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
"""VRMify 增强版:打包发布用的 zip(绿色版:解压后双击 VRMify.exe)。本地和 GitHub Actions 用的是同一个脚本。
python tools/build_release.py → dist/VRMify-v0.1.0-win64.zip
python tools/build_release.py --expect-version v0.1.0 tag 和 ui/version.py 不一致时报错退出
步骤:PyInstaller 按 VRMify.spec 打包 → 把 LICENSE、README 放进程序文件夹 → 压成 zip。
需要 requirements-dev.txt 里的包(pyinstaller、pywebview、Pillow)。
"""
import argparse
import os
import platform
import shutil
import subprocess
import sys
import zipfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "ui"))
from version import __version__ # noqa: E402
APP = "VRMify"
# 随程序一起发的文档:GPL-3.0 要求附上许可证;README 里有用法和源码出处
DOCS = ["LICENSE", "README_SC.md", "README.md"]
def main(argv=None):
ap = argparse.ArgumentParser(description="打包发布用的 zip")
ap.add_argument("--expect-version", default="", help="tag(如 v0.1.0),必须和 ui/version.py 一致")
ap.add_argument("--dist", default=os.path.join(ROOT, "dist"))
ap.add_argument("--work", default=os.path.join(ROOT, "build"))
a = ap.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
want = a.expect_version.lstrip("vV")
if want and want != __version__:
print("版本号不一致:tag 是 %s,ui/version.py 里是 %s(先改 version.py 再打 tag)"
% (a.expect_version, __version__))
return 1
subprocess.run([sys.executable, "-m", "PyInstaller", os.path.join(ROOT, "VRMify.spec"),
"--noconfirm", "--clean", "--distpath", a.dist, "--workpath", a.work],
cwd=ROOT, check=True)
app_dir = os.path.join(a.dist, APP)
for fn in DOCS:
shutil.copy2(os.path.join(ROOT, fn), os.path.join(app_dir, fn))
arch = "win64" if sys.platform.startswith("win") and platform.machine().endswith("64") else \
"%s-%s" % (sys.platform, platform.machine().lower())
out = os.path.join(a.dist, "%s-v%s-%s.zip" % (APP, __version__, arch))
if os.path.exists(out):
os.remove(out)
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED, compresslevel=9) as zf:
for d, _, fs in os.walk(app_dir):
for fn in fs:
p = os.path.join(d, fn)
zf.write(p, os.path.join(APP, os.path.relpath(p, app_dir)))
print("已生成 %s(%.1f MB)" % (out, os.path.getsize(out) / 1048576.0))
gh_out = os.environ.get("GITHUB_OUTPUT")
if gh_out: # 给 Actions 后面的步骤用
with open(gh_out, "a", encoding="utf-8") as f:
f.write("zip=%s\nversion=%s\n" % (out, __version__))
return 0
if __name__ == "__main__":
sys.exit(main())
+54
View File
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""生成程序图标:圆角方块(界面的强调蓝)+ 白色菱形,和界面左上角的「◆」一致。
python tools/make_icon.py → ui/icon.ico(exe 图标,多尺寸)+ ui/web/icon.png(页面 favicon)
需要 Pillow。按 4 倍分辨率画再缩小,边缘是平滑的。
"""
import os
from PIL import Image, ImageDraw
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TOP, BOTTOM = (0x4F, 0x86, 0xF5), (0x2F, 0x5C, 0xD6) # 界面的 --accent #3a6ee8 上下各偏一点
SIZES = [16, 20, 24, 32, 40, 48, 64, 128, 256]
def draw(size):
k = 4
n = size * k
img = Image.new("RGBA", (n, n), (0, 0, 0, 0))
# 竖直渐变的底
grad = Image.new("RGBA", (1, n))
for y in range(n):
t = y / max(1, n - 1)
grad.putpixel((0, y), tuple(round(TOP[i] + (BOTTOM[i] - TOP[i]) * t) for i in range(3)) + (255,))
grad = grad.resize((n, n))
mask = Image.new("L", (n, n), 0)
pad = round(n * 0.04)
ImageDraw.Draw(mask).rounded_rectangle((pad, pad, n - 1 - pad, n - 1 - pad), radius=round(n * 0.22), fill=255)
img.paste(grad, (0, 0), mask)
# 白色菱形(小尺寸时画大一点,免得糊成一个点)
d = ImageDraw.Draw(img)
c = n / 2
r = n * (0.30 if size >= 32 else 0.34)
d.polygon([(c, c - r), (c + r, c), (c, c + r), (c - r, c)], fill=(255, 255, 255, 255))
if size >= 48: # 大图标:菱形中间挖一个小菱形,多一点层次
r2 = r * 0.38
d.polygon([(c, c - r2), (c + r2, c), (c, c + r2), (c - r2, c)],
fill=tuple(round((TOP[i] + BOTTOM[i]) / 2) for i in range(3)) + (255,))
return img.resize((size, size), Image.LANCZOS)
def main():
imgs = [draw(s) for s in SIZES]
ico = os.path.join(ROOT, "ui", "icon.ico")
imgs[-1].save(ico, sizes=[(s, s) for s in SIZES], append_images=imgs[:-1])
png = os.path.join(ROOT, "ui", "web", "icon.png")
draw(64).save(png, optimize=True)
print(ico)
print(png)
if __name__ == "__main__":
main()
+358
View File
@@ -0,0 +1,358 @@
# -*- coding: utf-8 -*-
"""VRM 文件静态检查:结构、SpringBone、官方 JSON Schema、导入器会踩的坑。
python tools/qa/check_vrm.py 文件或目录 [...] [--no-schema] [-v]
逐个文件检查(目录会递归找 .vrm),有问题的打印出来,最后汇总;有错误时退出码为 1。
* humanoid:骨位名合法、必需骨位齐全、每根骨的 VRM 父骨位确实是它的祖先、
用占位骨(vrm_*)补的骨位(道具 / 非人形模型会有,记为提示)。
* 网格:JOINTS_0 不越界;同一网格里 morph target 不重名(Unity 遇到重名的
BlendShape 会抛异常,UniVRM 导入直接中断)。
* 表情:materialColorBinds 的材质号、morphTargetBinds 的 node / index 在范围内。
* SpringBone(VRM 1.0):相邻关节是祖先关系、关节不落在 humanoid 骨上、参数有限、
碰撞组号合法、同一节点不重复当关节。
* SpringBone(VRM 0.x):硬度键名是 stiffiness、参数有限、碰撞组号合法、组根子树不含
humanoid 骨、同一节点不落在两个组的子树里(0.x 组根下面整棵子树都会摆动)。
* Schema:用 vrm-c/vrm-specification 的官方 JSON Schema 校验 VRMC_vrm、
VRMC_springBone、VRMC_materials_mtoon,以及 VRM 0.x 的 VRM 扩展。需要 jsonschema 包;第一次运行时从 GitHub
下载固定版本的 schema,缓存在 tools/qa/.schema-cache/。UniVRM 对不合规的数据很少
容错(例如 targetValue 少一个分量就 IndexOutOfRange),所以这一项很有用。
"""
import argparse
import json
import math
import os
import sys
import urllib.request
from collections import Counter
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path[:0] = [os.path.join(ROOT, "formats"), os.path.join(ROOT, "convert")]
import vrmio # noqa: E402
from vrmconv.humanoid import _vrm_anchor # noqa: E402
# ---------------------------------------------------------------- humanoid --
_B10 = set("hips spine chest upperChest neck head leftEye rightEye jaw leftUpperLeg leftLowerLeg "
"leftFoot leftToes rightUpperLeg rightLowerLeg rightFoot rightToes leftShoulder "
"leftUpperArm leftLowerArm leftHand rightShoulder rightUpperArm rightLowerArm "
"rightHand".split())
_B0 = set(_B10)
for _s in ("left", "right"):
_B10 |= {_s + "ThumbMetacarpal", _s + "ThumbProximal", _s + "ThumbDistal"}
_B0 |= {_s + "ThumbProximal", _s + "ThumbIntermediate", _s + "ThumbDistal"}
for _f in ("Index", "Middle", "Ring", "Little"):
for _j in ("Proximal", "Intermediate", "Distal"):
_B10.add(_s + _f + _j)
_B0.add(_s + _f + _j)
_REQ = ("hips spine head leftUpperArm leftLowerArm leftHand rightUpperArm rightLowerArm "
"rightHand leftUpperLeg leftLowerLeg leftFoot rightUpperLeg rightLowerLeg "
"rightFoot").split()
# ------------------------------------------------------------------ schema --
SPEC_COMMIT = "94e82dd346fa6cf0337c4421728640e5252dd38e" # vrm-c/vrm-specification
SPEC_URL = "https://raw.githubusercontent.com/vrm-c/vrm-specification/%s/specification/%s/schema/%s"
SPEC_FILES = {
"VRMC_vrm-1.0": [
"VRMC_vrm.schema.json", "VRMC_vrm.meta.schema.json", "VRMC_vrm.humanoid.schema.json",
"VRMC_vrm.humanoid.humanBones.schema.json",
"VRMC_vrm.humanoid.humanBones.humanBone.schema.json",
"VRMC_vrm.firstPerson.schema.json", "VRMC_vrm.firstPerson.meshAnnotation.schema.json",
"VRMC_vrm.lookAt.schema.json", "VRMC_vrm.lookAt.rangeMap.schema.json",
"VRMC_vrm.expressions.schema.json", "VRMC_vrm.expressions.expression.schema.json",
"VRMC_vrm.expressions.expression.materialColorBind.schema.json",
"VRMC_vrm.expressions.expression.morphTargetBind.schema.json",
"VRMC_vrm.expressions.expression.textureTransformBind.schema.json"],
"VRMC_springBone-1.0": [
"VRMC_springBone.schema.json", "VRMC_springBone.collider.schema.json",
"VRMC_springBone.colliderGroup.schema.json", "VRMC_springBone.joint.schema.json",
"VRMC_springBone.shape.schema.json", "VRMC_springBone.spring.schema.json"],
"VRMC_materials_mtoon-1.0": [
"VRMC_materials_mtoon.schema.json", "mtoon.shadingShiftTexture.schema.json"],
"0.0": [
"vrm.schema.json", "vrm.meta.schema.json", "vrm.humanoid.schema.json", "vrm.humanoid.bone.schema.json",
"vrm.firstperson.schema.json", "vrm.firstperson.degreemap.schema.json",
"vrm.firstperson.meshannotation.schema.json", "vrm.blendshape.schema.json",
"vrm.blendshape.group.schema.json", "vrm.blendshape.bind.schema.json",
"vrm.blendshape.materialbind.schema.json", "vrm.secondaryanimation.schema.json",
"vrm.secondaryanimation.spring.schema.json", "vrm.secondaryanimation.collidergroup.schema.json",
"vrm.material.schema.json"],
}
# glTF 本体的几个 schema 只被引用、不需要逐项校验,用最小定义代替
_GLTF_STUBS = {
"glTFProperty.schema.json": {"type": "object"},
"glTFid.schema.json": {"type": "integer", "minimum": 0},
"textureInfo.schema.json": {"type": "object", "required": ["index"],
"properties": {"index": {"type": "integer", "minimum": 0},
"texCoord": {"type": "integer", "minimum": 0}}},
}
_BASE = "https://vrm-spec.invalid/"
def load_validators():
"""→ {扩展名: validator};缺 jsonschema 或下载失败时返回 (None, 原因)。"""
try:
import jsonschema
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT4
except ImportError:
return None, "未安装 jsonschema(pip install jsonschema),跳过 Schema 校验"
cache = os.path.join(HERE, ".schema-cache", SPEC_COMMIT[:12])
docs = dict(_GLTF_STUBS)
for d, files in SPEC_FILES.items():
for fn in files:
path = os.path.join(cache, fn)
if not os.path.isfile(path):
os.makedirs(cache, exist_ok=True)
try:
with urllib.request.urlopen(SPEC_URL % (SPEC_COMMIT, d, fn), timeout=30) as r:
data = r.read()
except OSError as e:
return None, "下载 schema 失败(%s),跳过 Schema 校验" % e
with open(path, "wb") as f:
f.write(data)
with open(path, encoding="utf-8") as f:
docs[fn] = json.load(f)
resources = []
for fn, doc in docs.items():
# draft-04 用 id(不是 $id)定基准 URI,相对的 $ref 才解析得到
doc = dict({k: v for k, v in doc.items() if k not in ("$schema", "$id")}, id=_BASE + fn)
resources.append((_BASE + fn, Resource.from_contents(doc, default_specification=DRAFT4)))
registry = Registry().with_resources(resources)
out = {}
for ext, fn in (("VRMC_vrm", "VRMC_vrm"), ("VRMC_springBone", "VRMC_springBone"),
("VRMC_materials_mtoon", "VRMC_materials_mtoon"), ("VRM", "vrm")):
schema = registry.contents(_BASE + fn + ".schema.json")
out[ext] = jsonschema.Draft4Validator(schema, registry=registry)
return out, None
def schema_errors(g, validators):
errs = []
def run(ext, obj, where):
for e in validators[ext].iter_errors(obj):
path = "/".join(str(p) for p in e.absolute_path)
errs.append("schema %s/%s: %s" % (where, path, e.message[:120]))
ext = g.get("extensions", {})
if "VRMC_vrm" in ext:
run("VRMC_vrm", ext["VRMC_vrm"], "VRMC_vrm")
if "VRMC_springBone" in ext:
run("VRMC_springBone", ext["VRMC_springBone"], "VRMC_springBone")
if "VRM" in ext:
run("VRM", ext["VRM"], "VRM")
for i, m in enumerate(g.get("materials", [])):
mt = m.get("extensions", {}).get("VRMC_materials_mtoon")
if mt is not None:
run("VRMC_materials_mtoon", mt, "materials/%d/mtoon" % i)
return errs
# ------------------------------------------------------------------ checks --
def check(path, validators=None):
"""→ (错误列表, 提示列表, 摘要 dict)"""
errs, notes = [], []
g, b = vrmio.read_glb(path)
ext = g.get("extensions", {})
nodes = g["nodes"]
par = {c: i for i, n in enumerate(nodes) for c in n.get("children", [])}
is1 = "VRMC_vrm" in ext
if not is1 and "VRM" not in ext:
return ["不是 VRM(没有 VRMC_vrm / VRM 扩展)"], [], {}
# humanoid
if is1:
hb = {k: v["node"] for k, v in ext["VRMC_vrm"]["humanoid"]["humanBones"].items()}
valid, req = _B10, _REQ
else:
hb = {x["bone"]: x["node"] for x in ext["VRM"]["humanoid"]["humanBones"]}
valid, req = _B0, _REQ + ["chest", "neck"]
for k in sorted(set(hb) - valid):
errs.append("humanoid:非法骨位 %s" % k)
for k in req:
if k not in hb:
errs.append("humanoid:缺少必需骨位 %s" % k)
for h, n in hb.items():
a = _vrm_anchor(h, hb)
if a is None:
continue
p = par.get(n)
while p is not None and p != hb[a]:
p = par.get(p)
if p is None:
errs.append("humanoid:%s 不在 %s 之下" % (h, a))
placeholders = sorted(h for h, n in hb.items() if nodes[n].get("name", "").startswith("vrm_"))
if placeholders:
notes.append("humanoid:%d 根占位骨(%s)" % (len(placeholders), ", ".join(placeholders[:6])
+ (" …" if len(placeholders) > 6 else "")))
# mesh
njoints = len(g["skins"][0]["joints"]) if g.get("skins") else 0
for mi, m in enumerate(g.get("meshes", [])):
for pr in m["primitives"]:
if "JOINTS_0" in pr["attributes"]:
if max(vrmio.read_accessor(g, b, pr["attributes"]["JOINTS_0"])) >= njoints:
errs.append("mesh %d:JOINTS_0 超出 skin.joints" % mi)
break
names = (m.get("extras") or {}).get("targetNames") or []
for k, v in Counter(names).items():
if v > 1:
errs.append("mesh %d:morph target 重名「%s」×%d(Unity 导入会中断)" % (mi, k, v))
# expressions
nm = len(g.get("materials", []))
if is1:
groups = ext["VRMC_vrm"].get("expressions", {})
for grp in ("preset", "custom"):
for name, e in (groups.get(grp) or {}).items():
for bnd in e.get("materialColorBinds", []):
if not 0 <= bnd["material"] < nm:
errs.append("表情 %s:材质号 %d 越界" % (name, bnd["material"]))
for bnd in e.get("morphTargetBinds", []):
node = nodes[bnd["node"]] if 0 <= bnd["node"] < len(nodes) else {}
if "mesh" not in node:
errs.append("表情 %s:node %d 没有网格" % (name, bnd["node"]))
continue
nt = len(g["meshes"][node["mesh"]]["primitives"][0].get("targets", []))
if not 0 <= bnd["index"] < nt:
errs.append("表情 %s:morph 索引 %d 越界(共 %d)" % (name, bnd["index"], nt))
# SpringBone
summary = {"spec": "1.0" if is1 else "0.x", "humanBones": len(hb),
"placeholders": len(placeholders)}
sb = ext.get("VRMC_springBone")
if sb:
hum = set(hb.values())
seen = Counter()
ncg = len(sb.get("colliderGroups", []))
for sp in sb["springs"]:
js = sp["joints"]
for a, c in zip(js, js[1:]):
q = par.get(c["node"])
while q is not None and q != a["node"]:
q = par.get(q)
if q is None:
errs.append("SpringBone %s:%s 不是 %s 的后代" % (
sp.get("name"), nodes[c["node"]]["name"], nodes[a["node"]]["name"]))
for j in js:
seen[j["node"]] += 1
if j["node"] in hum:
errs.append("SpringBone %s:关节落在 humanoid 骨 %s 上"
% (sp.get("name"), nodes[j["node"]]["name"]))
for k in ("hitRadius", "stiffness", "gravityPower", "dragForce"):
if k in j and not math.isfinite(j[k]):
errs.append("SpringBone %s:%s 不是有限值" % (sp.get("name"), k))
for cg in sp.get("colliderGroups", []):
if not 0 <= cg < ncg:
errs.append("SpringBone %s:碰撞组号 %d 越界" % (sp.get("name"), cg))
for n, c in seen.items():
if c > 1:
errs.append("SpringBone:节点 %s 在 %d 条链里当关节" % (nodes[n]["name"], c))
summary.update(springs=len(sb["springs"]),
joints=sum(len(sp["joints"]) for sp in sb["springs"]),
colliders=len(sb.get("colliders", [])))
sa = None if is1 else ext["VRM"].get("secondaryAnimation")
if sa and sa.get("boneGroups"):
errs += _check_secondary(sa, nodes, par, set(hb.values()))
summary.update(springs=len(sa["boneGroups"]),
colliders=sum(len(c.get("colliders", [])) for c in sa.get("colliderGroups", [])))
if validators:
errs += schema_errors(g, validators)
return errs, notes, summary
def _check_secondary(sa, nodes, par, hum):
"""VRM 0.x 的 secondaryAnimation:组根子树整棵都会摆动,同一节点在两个组里会被模拟两次。"""
errs = []
kids = {i: n.get("children", []) for i, n in enumerate(nodes)}
ncg = len(sa.get("colliderGroups", []))
owner = {}
for gi, grp in enumerate(sa["boneGroups"]):
name = grp.get("comment") or "#%d" % gi
if "stiffness" in grp and "stiffiness" not in grp:
errs.append("secondaryAnimation %s:硬度要写成 stiffiness(规范里的拼写),stiffness 不会被读取" % name)
for k in ("stiffiness", "gravityPower", "dragForce", "hitRadius"):
if k in grp and not math.isfinite(grp[k]):
errs.append("secondaryAnimation %s:%s 不是有限值" % (name, k))
for cg in grp.get("colliderGroups", []):
if not 0 <= cg < ncg:
errs.append("secondaryAnimation %s:碰撞组号 %d 越界" % (name, cg))
for r in grp.get("bones", []):
if not 0 <= r < len(nodes):
errs.append("secondaryAnimation %s:根节点 %d 越界" % (name, r))
continue
stack = [r]
while stack:
n = stack.pop()
if n in hum:
errs.append("secondaryAnimation %s:humanoid 骨 %s 在摆动子树里" % (name, nodes[n]["name"]))
if n in owner:
errs.append("secondaryAnimation:节点 %s 同时在组 %s 和 %s 的子树里(会被模拟两次)"
% (nodes[n]["name"], owner[n], name))
continue
owner[n] = name
stack.extend(kids[n])
for ci, cg in enumerate(sa.get("colliderGroups", [])):
if not 0 <= cg.get("node", -1) < len(nodes):
errs.append("secondaryAnimation:碰撞组 %d 的节点越界" % ci)
return errs
def iter_files(paths):
for p in paths:
if os.path.isdir(p):
for d, _, fs in os.walk(p):
for f in sorted(fs):
if f.lower().endswith(".vrm"):
yield os.path.join(d, f)
else:
yield p
def main(argv=None):
ap = argparse.ArgumentParser(description="VRM 文件静态检查")
ap.add_argument("paths", nargs="+", help=".vrm 文件或目录")
ap.add_argument("--no-schema", action="store_true", help="不做官方 JSON Schema 校验")
ap.add_argument("-v", "--verbose", action="store_true", help="通过的文件也打印摘要和提示")
a = ap.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
validators = None
if not a.no_schema:
validators, why = load_validators()
if why:
print("提示:" + why)
n = bad = 0
for path in iter_files(a.paths):
n += 1
try:
errs, notes, summ = check(path, validators)
except Exception as e: # noqa: BLE001 坏文件也要继续往下查
errs, notes, summ = ["读取失败:%s: %s" % (type(e).__name__, e)], [], {}
if errs:
bad += 1
print("✗ %s" % path)
shown = Counter()
for e in errs: # 同类错误只列前几条
key = e.split(":")[0] if ":" in e else e[:40]
shown[key] += 1
if shown[key] <= 5:
print(" " + e)
for k, c in shown.items():
if c > 5:
print(" …… %s 还有 %d 条" % (k, c - 5))
elif a.verbose:
print("✓ %s %s" % (path, " ".join("%s=%s" % kv for kv in summ.items())))
for t in notes:
print(" 提示:" + t)
print("检查 %d 个文件,%d 个有错误" % (n, bad))
return 1 if bad else 0
if __name__ == "__main__":
sys.exit(main())
+147
View File
@@ -0,0 +1,147 @@
# -*- coding: utf-8 -*-
"""SpringBone 动态测试:静止偏移 + 待机 / 带位移走路 / 跑步时的末端颤动。
python tools/qa/dynamics.py 文件或目录 [...] [-j 4] [--motions idle,walk_real,run]
[--fps 60] [--jitter 0] [--json 结果.json] [--strict]
用 three.js + three-vrm 在 Node 里无界面地跑物理(和 UniVRM 同一套 SpringBone 规则),
每个文件一个进程并行。只测带 VRMC_springBone 的人形模型(占位骨补出来的道具跳过)。
第一次用要先装依赖: cd tools/qa/harness && npm install
指标(越小越稳;参考范围来自 VRoid / VRM 官方示例模型在同样动作下的实测):
静止偏移 站着不动 2 秒后弹簧末端离绑定姿势的最大距离。正常 < 5 cm
hf 末端 8 Hz 以上的颤动能量 / 链长 ×1000(平均/最大)。原生模型头发 2~16、裙摆 3~58
swing 末端最大摆幅 / 链长
穿腿 裙摆关节落在模型自己的腿部碰撞体里的比例(含静止时就重叠的,要和 idle 对比看)
超出范围的数值后面标「!」;--strict 时有标记就返回 1。
"""
import argparse
import json
import os
import shutil
import struct
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor
HERE = os.path.dirname(os.path.abspath(__file__))
HARNESS = os.path.join(HERE, "harness")
LIMITS = {"rest": 0.05, "hf": {"hair": 16.0, "skirt": 60.0, "torso": 60.0, "other": 60.0}}
def glb_json(path):
with open(path, "rb") as f:
head = f.read(20)
n = struct.unpack_from("<I", head, 12)[0]
return json.loads(f.read(n))
def testable(path):
"""有 SpringBone、humanoid 不是占位骨拼出来的 → True。"""
try:
g = glb_json(path)
except (OSError, ValueError, struct.error):
return False
if "VRMC_springBone" not in g.get("extensionsUsed", []):
return False
hb = g.get("extensions", {}).get("VRMC_vrm", {}).get("humanoid", {}).get("humanBones", {})
names = [g["nodes"][v["node"]].get("name", "") for v in hb.values()]
return not any(n.startswith("vrm_") for n in names)
def iter_files(paths):
for p in paths:
if os.path.isdir(p):
for d, _, fs in os.walk(p):
for f in sorted(fs):
if f.lower().endswith(".vrm"):
yield os.path.join(d, f)
else:
yield p
def run_one(path, env):
r = subprocess.run(["node", os.path.join(HARNESS, "measure.mjs"), os.path.abspath(path)],
cwd=HARNESS, env=env, capture_output=True, encoding="utf-8")
lines = [l for l in r.stdout.splitlines() if l.startswith("{")]
if r.returncode or not lines:
return {"file": path, "error": (r.stderr or r.stdout).strip()[-800:]}
return json.loads(lines[-1])
def fmt(res):
"""一个文件的结果 → (文本行, 是否有超范围)"""
flag = False
name = os.path.basename(res["file"])
if "error" in res:
return ["✗ %s\n %s" % (name, res["error"].replace("\n", "\n "))], True
rest = res["rest"]
bad = rest["max"] > LIMITS["rest"]
flag |= bad
out = ["%s %s" % ("!" if bad else "·", name),
" 静止偏移 最大 %.1f cm%s(%s)· 超过 1 cm 的 %d / %d" % (
rest["max"] * 100, " !" if bad else "", rest["maxName"], rest["over1cm"], rest["joints"])]
for m, r in res["motions"].items():
parts = []
for cat in ("hair", "skirt", "torso", "other"):
c = r["cats"].get(cat)
if not c:
continue
over = c["hfMax"] > LIMITS["hf"][cat]
flag |= over
parts.append("%s hf %.1f/%.1f%s swing %.2f" % (
{"hair": "头发", "skirt": "裙摆", "torso": "躯干", "other": "其它"}[cat],
c["hf"], c["hfMax"], "!" if over else "", c["swing"]))
if r.get("skirtInside") is not None:
parts.append("穿腿 %.1f%%" % (r["skirtInside"] * 100))
if r.get("skirtGap"):
g = r["skirtGap"]
parts.append("张开 %.1f/%.1f/%.1f cm" % (g["mean"] * 100, g["p90"] * 100, g["max"] * 100))
out.append(" %-9s %s" % (m, " | ".join(parts)))
if r.get("worst") and any(w["hf"] > LIMITS["hf"][w["cat"]] for w in r["worst"]):
for w in r["worst"]:
out.append(" %s:%s hf %.1f swing %.2f(%d 节,%.2f m)" % (
w["cat"], w["chain"], w["hf"], w["swing"], w["joints"], w["length"]))
return out, flag
def main(argv=None):
ap = argparse.ArgumentParser(description="SpringBone 动态测试(three-vrm,无界面)")
ap.add_argument("paths", nargs="+", help=".vrm 文件或目录")
ap.add_argument("-j", "--jobs", type=int, default=4, help="并行数(每个文件一个 Node 进程)")
ap.add_argument("--motions", default="idle,walk_real,run",
help="动作:idle, walk_real, run, leg_raise, yaw_gentle, yaw_strong, walk, static")
ap.add_argument("--fps", type=float, default=60, help="物理帧率(默认 60)")
ap.add_argument("--jitter", type=float, default=0, help="帧时间抖动比例,如 0.3")
ap.add_argument("--json", help="把原始结果写成 JSON")
ap.add_argument("--strict", action="store_true", help="有超范围的数值时返回 1")
a = ap.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
if not shutil.which("node"):
sys.exit("需要 Node.js(https://nodejs.org)")
if not os.path.isdir(os.path.join(HARNESS, "node_modules", "@pixiv", "three-vrm")):
sys.exit("先安装依赖:cd %s && npm install" % HARNESS)
files = list(iter_files(a.paths))
todo = [f for f in files if testable(f)]
if len(todo) < len(files):
print("跳过 %d 个没有 SpringBone 或不是人形的文件" % (len(files) - len(todo)))
env = dict(os.environ, MOTIONS=a.motions, FPS=str(a.fps), JITTER=str(a.jitter), DETAIL="3")
with ThreadPoolExecutor(max_workers=max(1, a.jobs)) as ex:
results = list(ex.map(lambda f: run_one(f, env), todo))
n_flag = 0
for res in results:
lines, flag = fmt(res)
n_flag += flag
print("\n".join(lines))
if a.json:
with open(a.json, "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=1)
print("测试 %d 个文件,%d 个有超出参考范围的数值(标「!」)" % (len(results), n_flag))
return 1 if (a.strict and n_flag) else 0
if __name__ == "__main__":
sys.exit(main())
+225
View File
@@ -0,0 +1,225 @@
// SpringBone behaviour of one VRM, headless (three.js + three-vrm, no rendering).
// node measure.mjs model.vrm → one JSON object on stdout
// env: MOTIONS=idle,walk_real,run FPS=60 JITTER=0 (frame-time jitter ratio) DETAIL=3
//
// rest: after 2 s standing still, how far each spring tip has moved from the bind pose
// (a correct setup stays put; large values = joints fight colliders / wrong rest dir)
// motions: per chain category (hair = under head/neck, skirt = under hips/legs,
// torso = under spine/chest/shoulders, other = arms etc.)
// hf tip energy above 8 Hz / chain length ×1000 — genuine shake (gait content < 7 Hz);
// mean and max over chains. Native VRoid/sample models: hair ≈ 2–16, skirt ≈ 3–58.
// swing peak tip deformation / chain length (anchor-local)
// skirtInside share of skirt joint samples inside the model's own leg colliders
// (counts rest overlap too — compare against idle)
import * as THREE from 'three';
import fs from 'fs';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { VRMLoaderPlugin, VRMUtils } from '@pixiv/three-vrm';
import { MOTIONS, FRAMES } from './motions.mjs';
// Textures and MToon are irrelevant here and slow to decode in Node: strip them.
function stripGlb(buf) {
const jl = buf.readUInt32LE(12);
const json = JSON.parse(buf.slice(20, 20 + jl).toString('utf8'));
delete json.images; delete json.textures; delete json.samplers;
for (const m of json.materials || []) {
if (m.pbrMetallicRoughness) delete m.pbrMetallicRoughness.baseColorTexture;
delete m.extensions; delete m.normalTexture; delete m.emissiveTexture; delete m.occlusionTexture;
}
json.extensionsUsed = (json.extensionsUsed || []).filter((e) => e !== 'VRMC_materials_mtoon' && e !== 'KHR_texture_transform');
if (json.extensions && json.extensions.VRM) json.extensions.VRM.materialProperties = [];
let js = Buffer.from(JSON.stringify(json));
js = Buffer.concat([js, Buffer.alloc((4 - js.length % 4) % 4, 0x20)]);
const bin = buf.slice(20 + jl);
const h = Buffer.alloc(20);
h.writeUInt32LE(0x46546c67, 0); h.writeUInt32LE(2, 4); h.writeUInt32LE(20 + js.length + bin.length, 8);
h.writeUInt32LE(js.length, 12); h.writeUInt32LE(0x4e4f534a, 16);
const o = Buffer.concat([h, js, bin]);
return o.buffer.slice(o.byteOffset, o.byteOffset + o.byteLength);
}
async function load(path) {
const loader = new GLTFLoader();
loader.register((p) => new VRMLoaderPlugin(p));
const warn = console.warn; console.warn = () => {}; // material-bind warnings: materials were stripped
try {
const vrm = await new Promise((r, j) => loader.parse(stripGlb(fs.readFileSync(path)), '', (g) => r(g.userData.vrm), j));
if (vrm.meta && vrm.meta.metaVersion === '0') VRMUtils.rotateVRM0(vrm);
new THREE.Scene().add(vrm.scene);
vrm.update(0); vrm.scene.updateMatrixWorld(true);
return vrm;
} finally { console.warn = warn; }
}
const wp = (o) => new THREE.Vector3().setFromMatrixPosition(o.matrixWorld);
const LEG = ['leftUpperLeg', 'rightUpperLeg', 'leftLowerLeg', 'rightLowerLeg'];
const LEG_CHILD = { leftUpperLeg: 'leftLowerLeg', rightUpperLeg: 'rightLowerLeg', leftLowerLeg: 'leftFoot', rightLowerLeg: 'rightFoot' };
async function restDrift(path) {
const vrm = await load(path);
const joints = vrm.springBoneManager ? [...vrm.springBoneManager.joints] : [];
if (!joints.length) return null;
const pos = () => joints.map((j) => wp(j.child || j.bone));
const p0 = pos();
for (let i = 0; i < 120; i++) vrm.update(1 / 60);
vrm.scene.updateMatrixWorld(true);
const p1 = pos();
const d = joints.map((j, i) => [p1[i].distanceTo(p0[i]), j.bone.name]).sort((a, b) => b[0] - a[0]);
return { max: d[0][0], maxName: d[0][1], over1cm: d.filter((x) => x[0] > 0.01).length, joints: d.length,
top: d.slice(0, 5).map(([v, n]) => [n, +v.toFixed(4)]) };
}
function chainsOf(vrm) {
const h = vrm.humanoid, sbm = vrm.springBoneManager;
const roleOf = new Map(Object.keys(h.humanBones).map((r) => [h.getRawBoneNode(r), r]));
const joints = [...sbm.joints];
const jbones = new Set(joints.map((j) => j.bone));
const chains = [];
for (const j of joints) {
let p = j.bone.parent;
while (p && !jbones.has(p) && !roleOf.has(p)) p = p.parent;
if (p && jbones.has(p)) continue; // not a chain root
const path = [j];
for (let cur = j; ;) {
const nx = joints.find((k) => { let q = k.bone.parent; while (q && q !== cur.bone && !jbones.has(q)) q = q.parent; return q === cur.bone; });
if (!nx) break;
path.push(nx); cur = nx;
}
const tip = path[path.length - 1].child || path[path.length - 1].bone;
let anc = j.bone.parent;
while (anc && !roleOf.has(anc)) anc = anc.parent;
const role = anc ? roleOf.get(anc) : '?';
const cat = ['head', 'neck', 'jaw'].includes(role) ? 'hair'
: (role === 'hips' || /Leg|Foot|Toes/.test(role)) ? 'skirt'
: ['spine', 'chest', 'upperChest', 'leftShoulder', 'rightShoulder'].includes(role) ? 'torso' : 'other';
let L = 0, prev = wp(j.bone);
for (const k of path.slice(1)) { const q = wp(k.bone); L += q.distanceTo(prev); prev = q; }
L += wp(tip).distanceTo(prev);
chains.push({ root: j, tip, anchor: anc, cat, joints: path, L: Math.max(L, 0.02) });
}
return { chains, roleOf };
}
async function measure(path, mname) {
const vrm = await load(path);
const h = vrm.humanoid, sbm = vrm.springBoneManager;
const { chains, roleOf } = chainsOf(vrm);
const loc = (c) => (c.anchor ? c.anchor.worldToLocal(wp(c.tip)) : wp(c.tip));
// the leg itself: per leg bone the collider whose axis lies closest to the bone (skirt chains may also
// have shifted copies of it — see springbone.leg_proxy_shift — those are not the leg)
const legCols = [];
for (const role of LEG) {
const bone = h.getRawBoneNode(role), child = h.getRawBoneNode(LEG_CHILD[role]);
if (!bone) continue;
const end = child && child !== bone ? bone.worldToLocal(wp(child)) : new THREE.Vector3(0, -0.4, 0);
const seg = new THREE.Line3(new THREE.Vector3(), end);
let best = null;
for (const g of sbm.colliderGroups) for (const col of g.colliders) {
if (col.parent !== bone) continue;
const s = col.shape, m = s.tail ? s.offset.clone().add(s.tail).multiplyScalar(0.5) : s.offset.clone();
const d = seg.closestPointToPoint(m, true, new THREE.Vector3()).distanceTo(m) - (s.radius || 0) * 0.01;
if (!best || d < best[0] - 1e-4) best = [d, col];
}
if (best) legCols.push(best[1]);
}
const node = (n) => h.getNormalizedBoneNode(n);
// VRM 0.x normalized bones (after rotateVRM0) turn the other way round X and Z in three-vrm
const sg = vrm.meta && vrm.meta.metaVersion === '0' ? -1 : 1;
const rot = (n, x, y, z, order) => node(n).rotation.set(x * sg, y, z * sg, order);
for (let i = 0; i < 60; i++) vrm.update(1 / 60); // settle before measuring
vrm.scene.updateMatrixWorld(true);
const rest = chains.map(loc);
const traj = chains.map(() => []);
// skirt opening: neighbouring skirt chains (each chain's 2 nearest roots, within 2× the median
// nearest-root spacing) — how much further apart their tips get than at rest
const hipsY = wp(h.getRawBoneNode('hips')).y;
const ring = chains.filter((c) => (c.cat === 'skirt' && roleOf.get(c.anchor) === 'hips')
|| (c.cat === 'torso' && wp(c.tip).y < hipsY));
const pairs = [];
if (ring.length > 2) {
const rp = ring.map((c) => wp(c.root.bone));
const near = ring.map((c, i) => rp.map((p, j) => [i === j ? Infinity : p.distanceTo(rp[i]), j]).sort((a, b) => a[0] - b[0]));
const med = near.map((n) => n[0][0]).sort((a, b) => a - b)[ring.length >> 1];
const seen = new Set();
near.forEach((n, i) => n.slice(0, 2).forEach(([d, j]) => {
const k = Math.min(i, j) + ':' + Math.max(i, j);
if (d < med * 2 && !seen.has(k)) { seen.add(k); pairs.push([ring[i], ring[j], wp(ring[i].tip).distanceTo(wp(ring[j].tip)), 0]); }
}));
}
let inside = 0, skirtTips = 0;
const N = FRAMES[mname];
const fps = +(process.env.FPS || 60), jit = +(process.env.JITTER || 0);
let acc60 = 0, seed = 12345, nextDt = 1 / fps;
const rnd = () => ((seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648);
for (let f = 0; f < N + 120; f++) {
const p = f < N ? MOTIONS[mname](f) : (mname === 'idle' ? MOTIONS[mname](N - 1) : {});
rot('hips', 0, p.yaw || 0, p.roll || 0);
if (p.spineYaw != null) rot('spine', 0, p.spineYaw, 0);
vrm.scene.position.set(0, p.bob || 0, p.rootZ || 0);
rot('leftUpperLeg', p.lLeg || 0, 0, 0); rot('rightUpperLeg', p.rLeg || 0, 0, 0);
rot('leftLowerLeg', p.lKnee || 0, 0, 0); rot('rightLowerLeg', p.rKnee || 0, 0, 0);
if (p.armDown != null) {
const a = 1.22 * Math.min(1, p.armDown / 0.7);
rot('leftUpperArm', p.armSwingL || 0, 0, -a, 'XYZ'); rot('rightUpperArm', p.armSwingR || 0, 0, a, 'XYZ');
rot('leftLowerArm', 0, p.armSwingL ? -0.3 : 0, 0); rot('rightLowerArm', 0, p.armSwingR ? 0.3 : 0, 0);
}
// physics at FPS (optionally jittered), motion sampled at 60 Hz
acc60 += 1 / 60;
while (acc60 >= nextDt - 1e-9) { vrm.update(nextDt); acc60 -= nextDt; nextDt = (1 / fps) * (1 + jit * (rnd() * 2 - 1)); }
vrm.scene.updateMatrixWorld(true);
chains.forEach((c, i) => traj[i].push(loc(c).sub(rest[i])));
if (f < N) for (const p of pairs) p[3] = Math.max(p[3], wp(p[0].tip).distanceTo(wp(p[1].tip)) - p[2]);
if (legCols.length) for (const c of chains) if (c.cat === 'skirt') {
for (const j of c.joints) {
const t = wp(j.child || j.bone); skirtTips++;
for (const col of legCols) { if (col.shape.calculateCollision(col.colliderMatrix, t, 0, new THREE.Vector3()) < -0.005) { inside++; c.inside = (c.inside || 0) + 1; break; } }
}
}
}
const W0 = (mname === 'walk_real' || mname === 'run') ? 60 : 0; // skip the start-up transient
const hfOf = (i) => { // spectral energy above 8 Hz
const T = traj[i], hi = Math.min(N, T.length), n = hi - W0;
if (n < 32) return 0;
let e = 0;
for (const ax of ['x', 'y', 'z']) {
const xs = []; for (let t = W0; t < hi; t++) xs.push(T[t][ax]);
const mean = xs.reduce((a, b) => a + b, 0) / n;
for (let k = Math.ceil(8 * n / 60); k <= n / 2; k++) {
let re = 0, im = 0;
for (let t = 0; t < n; t++) { const a = 2 * Math.PI * k * t / n; re += (xs[t] - mean) * Math.cos(a); im -= (xs[t] - mean) * Math.sin(a); }
e += (re * re + im * im) * 2 / (n * n);
}
}
return Math.sqrt(e) / chains[i].L * 1000;
};
const hf = chains.map((c, i) => hfOf(i));
const swingOf = (i) => Math.max(0, ...traj[i].slice(W0, N).map((v) => v.length())) / chains[i].L;
const out = { cats: {} };
for (const cat of ['hair', 'skirt', 'torso', 'other']) {
const ids = chains.map((c, i) => (c.cat === cat ? i : -1)).filter((i) => i >= 0);
if (!ids.length) continue;
out.cats[cat] = { n: ids.length, hf: ids.reduce((a, i) => a + hf[i], 0) / ids.length,
hfMax: Math.max(...ids.map((i) => hf[i])), swing: Math.max(...ids.map(swingOf)) };
}
const nd = +(process.env.DETAIL || 3);
out.skirtInside = skirtTips ? inside / skirtTips : null;
if (inside) out.insideWorst = chains.filter((c) => c.inside).sort((a, b) => b.inside - a.inside).slice(0, nd)
.map((c) => [c.root.bone.name, +(c.inside / c.joints.length / (N + 120)).toFixed(3)]);
if (pairs.length) {
const g = pairs.map((p) => p[3]).sort((a, b) => a - b);
out.skirtGap = { pairs: g.length, mean: g.reduce((a, b) => a + b, 0) / g.length, p90: g[Math.floor(g.length * 0.9)], max: g[g.length - 1],
worst: pairs.sort((a, b) => b[3] - a[3]).slice(0, nd).map((p) => [p[0].root.bone.name, p[1].root.bone.name, +p[2].toFixed(3), +p[3].toFixed(3)]) };
}
out.worst = chains.map((c, i) => [hf[i], i]).sort((a, b) => b[0] - a[0]).slice(0, nd)
.map(([v, i]) => ({ cat: chains[i].cat, chain: chains[i].root.bone.name, hf: +v.toFixed(2), swing: +swingOf(i).toFixed(3),
joints: chains[i].joints.length, length: +chains[i].L.toFixed(3) }));
return out;
}
const path = process.argv[2];
if (!path) { console.error('usage: node measure.mjs model.vrm'); process.exit(2); }
const result = { file: path, rest: await restDrift(path), motions: {} };
if (result.rest) {
for (const m of (process.env.MOTIONS || 'idle,walk_real,run').split(',')) result.motions[m] = await measure(path, m);
}
process.stdout.write(JSON.stringify(result) + '\n');
+42
View File
@@ -0,0 +1,42 @@
// Test motions for measure.mjs. Each returns per-frame pose params (radians), 60 fps,
// applied to the VRM's normalized humanoid bones.
// yaw: hips rotation about +Y
// lLeg/rLeg: thigh pitch about +X (negative = leg forward), lKnee/rKnee: knee bend (+X)
const smooth = (t) => t * t * (3 - 2 * t);
export const MOTIONS = {
static: (f) => ({}),
yaw_gentle: (f) => ({ yaw: f < 150 ? Math.sin(f / 60 * Math.PI * 2 * 0.8) * 0.35 : 0 }),
yaw_strong: (f) => ({ yaw: f < 150 ? Math.sin(f / 60 * Math.PI * 2 * 1.2) * 0.8 : 0 }),
leg_raise: (f) => {
const k = f < 40 ? smooth(f / 40) : f < 100 ? 1 : f < 140 ? smooth(1 - (f - 100) / 40) : 0;
return { lLeg: -1.2 * k, lKnee: 1.0 * k };
},
walk: (f) => {
const s = Math.sin(f / 60 * Math.PI * 2 * 0.9);
const a = f < 180 ? 1 : 0;
return {
lLeg: -0.5 * s * a, rLeg: 0.5 * s * a,
lKnee: Math.max(0, s) * 0.6 * a, rKnee: Math.max(0, -s) * 0.6 * a,
yaw: Math.sin(f / 60 * Math.PI * 2 * 0.9) * 0.08 * a,
};
},
};
// idle: arms lowered from the T-pose rest over 1 s (armDown 0.7 ≈ 70°), slight sway.
MOTIONS.idle = (f) => ({ armDown: 0.7 * smooth(Math.min(1, f / 60)), yaw: Math.sin(f / 60 * Math.PI * 2 * 0.25) * 0.05 });
// Realistic gait with root motion (forward along +Z), pelvis bob/twist, arm swing.
// rootZ: metres travelled; bob: pelvis vertical offset; roll: pelvis roll; arm: forward swing
function gait(f, speed, freq, thigh, knee, bob, armSwing) {
const t = f / 60, ph = t * Math.PI * 2 * freq;
const s = Math.sin(ph);
const kneeL = knee * Math.max(0, Math.sin(ph + 0.9)), kneeR = knee * Math.max(0, Math.sin(ph + 0.9 + Math.PI));
return {
rootZ: speed * t, bob: -bob * Math.cos(2 * ph),
yaw: 0.08 * s, roll: 0.04 * Math.sin(2 * ph), spineYaw: -0.1 * s,
lLeg: -thigh * s, rLeg: thigh * s, lKnee: 0.1 + kneeL, rKnee: 0.1 + kneeR,
armDown: 0.7 * smooth(Math.min(1, f / 40)), armSwingL: armSwing * s, armSwingR: -armSwing * s,
};
}
MOTIONS.walk_real = (f) => (f < 300 ? gait(f, 1.2, 0.9, 0.45, 0.9, 0.025, 0.35) : { ...gait(299, 1.2, 0.9, 0, 0, 0, 0), rootZ: 1.2 * 5, lLeg: 0, rLeg: 0, lKnee: 0, rKnee: 0, yaw: 0, roll: 0, spineYaw: 0, bob: 0, armSwingL: 0, armSwingR: 0 });
MOTIONS.run = (f) => (f < 240 ? gait(f, 3.0, 1.4, 0.8, 1.6, 0.05, 0.7) : { rootZ: 3.0 * 4, armDown: 0.7 });
export const FRAMES = { walk_real: 300, run: 240, idle: 240, static: 120, yaw_gentle: 240, yaw_strong: 240, leg_raise: 200, walk: 270 };
+157
View File
@@ -0,0 +1,157 @@
{
"name": "vrmify-qa-harness",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vrmify-qa-harness",
"license": "GPL-3.0",
"dependencies": {
"@pixiv/three-vrm": "3.5.5",
"three": "0.169.0"
}
},
"node_modules/@pixiv/three-vrm": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm/-/three-vrm-3.5.5.tgz",
"integrity": "sha512-RPXy7jYAXs704NIpZlosB0U2ENu21G9DrqGWdQgRe8dShaCo1ugpj+6BVPRCy91nt+MPMA96j5rbsSzEl0HlQA==",
"license": "MIT",
"dependencies": {
"@pixiv/three-vrm-core": "3.5.5",
"@pixiv/three-vrm-materials-hdr-emissive-multiplier": "3.5.5",
"@pixiv/three-vrm-materials-mtoon": "3.5.5",
"@pixiv/three-vrm-materials-v0compat": "3.5.5",
"@pixiv/three-vrm-node-constraint": "3.5.5",
"@pixiv/three-vrm-springbone": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/three-vrm-core": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm-core/-/three-vrm-core-3.5.5.tgz",
"integrity": "sha512-jjaXZbMVRsk2HAGVxqDLmSJyGOJqbMcJpuTHR1TY/TN1FxiKdAzSqdr/ZUcczYipNf8IL9vDT9NX2XmWj+ybRQ==",
"license": "MIT",
"dependencies": {
"@pixiv/types-vrm-0.0": "3.5.5",
"@pixiv/types-vrmc-vrm-1.0": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/three-vrm-materials-hdr-emissive-multiplier": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm-materials-hdr-emissive-multiplier/-/three-vrm-materials-hdr-emissive-multiplier-3.5.5.tgz",
"integrity": "sha512-XnHEqpY4ctYMucpeRcptdphOe0RrW7V5fUvojasD8Ja4MTjjrjCNFxsAvp3+HBkPZbkTM2sDCjJBJNzxN1scrA==",
"license": "MIT",
"dependencies": {
"@pixiv/types-vrmc-materials-hdr-emissive-multiplier-1.0": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/three-vrm-materials-mtoon": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm-materials-mtoon/-/three-vrm-materials-mtoon-3.5.5.tgz",
"integrity": "sha512-7lThHf4OKBOsVenD+R6L6HffNUlKO3Twnhr3+z77MnUsSzsOanjVKJtjSIly9QJ6hgl8YJmI6RnxCKqXcpplog==",
"license": "MIT",
"dependencies": {
"@pixiv/types-vrm-0.0": "3.5.5",
"@pixiv/types-vrmc-materials-mtoon-1.0": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/three-vrm-materials-v0compat": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm-materials-v0compat/-/three-vrm-materials-v0compat-3.5.5.tgz",
"integrity": "sha512-NjsTK4P4qdcr+RoL2gEUNgF2CY/aplNvyHHxI99N7D6CxDZ2H93FQ9aIuLCABj5b+4CIvldWMCcnS7YuLIr3Lw==",
"license": "MIT",
"dependencies": {
"@pixiv/types-vrm-0.0": "3.5.5",
"@pixiv/types-vrmc-materials-mtoon-1.0": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/three-vrm-node-constraint": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm-node-constraint/-/three-vrm-node-constraint-3.5.5.tgz",
"integrity": "sha512-+ShEXioZ5Ctf0EZ5ZViZ4n0jhV/qtJRH0Juw8nRnMNODZXDSr+B3An2lrfRoMhowCzG63GiUG4lXWV9noMXosA==",
"license": "MIT",
"dependencies": {
"@pixiv/types-vrmc-node-constraint-1.0": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/three-vrm-springbone": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/three-vrm-springbone/-/three-vrm-springbone-3.5.5.tgz",
"integrity": "sha512-GWAWrIegGH/u1ZA+Es03Qzc2yafrQOrhtal9jzUIAgU8Wm2DYuqAABOY+du/9RpBRK8D4T8DBPvRGQqvfrQ/uA==",
"license": "MIT",
"dependencies": {
"@pixiv/types-vrm-0.0": "3.5.5",
"@pixiv/types-vrmc-springbone-1.0": "3.5.5",
"@pixiv/types-vrmc-springbone-extended-collider-1.0": "3.5.5"
},
"peerDependencies": {
"three": ">=0.137"
}
},
"node_modules/@pixiv/types-vrm-0.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrm-0.0/-/types-vrm-0.0-3.5.5.tgz",
"integrity": "sha512-2S/0jswMjJTwL9ky8YzDR0CIaP6ZmE+HFuyW/0h+SA+awELf+J7WGgC8lTLM1MXzokO7Z2YVljU2F0fEhlKDIQ==",
"license": "MIT"
},
"node_modules/@pixiv/types-vrmc-materials-hdr-emissive-multiplier-1.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrmc-materials-hdr-emissive-multiplier-1.0/-/types-vrmc-materials-hdr-emissive-multiplier-1.0-3.5.5.tgz",
"integrity": "sha512-IuHw9ehXt2bymqfdFV+f2cCXo8kvJUfh0Zuut8qmKXoEb7HVyLGIQyS4TcQHG5yY22UTWHI1Axo5tQv35lPTgQ==",
"license": "MIT"
},
"node_modules/@pixiv/types-vrmc-materials-mtoon-1.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrmc-materials-mtoon-1.0/-/types-vrmc-materials-mtoon-1.0-3.5.5.tgz",
"integrity": "sha512-GFVuksAJVhrWTXYmI/ucNsE611/d8N6BoZrzhUAAINndt98loyzapW2+DJu+GsUBO5yxd+vLsL0FB16XD9+TGg==",
"license": "MIT"
},
"node_modules/@pixiv/types-vrmc-node-constraint-1.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrmc-node-constraint-1.0/-/types-vrmc-node-constraint-1.0-3.5.5.tgz",
"integrity": "sha512-8EbJ1uzkn9yJQarCChmpG12+mc2c7PimHvQ8H6/tMh/hqp5NtJND0z2lR2KeVxw9A3M+TpF+xgImhLBxGjqwzQ==",
"license": "MIT"
},
"node_modules/@pixiv/types-vrmc-springbone-1.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrmc-springbone-1.0/-/types-vrmc-springbone-1.0-3.5.5.tgz",
"integrity": "sha512-gRQ5Y7PaGiMefvOrJ3hvoSPqpA67jiwyhwh8ItjsPi3yri/Hn3CeXplW3Cbx3ZPbfu+C3Q2MCRa2TqAUOjeMRA==",
"license": "MIT"
},
"node_modules/@pixiv/types-vrmc-springbone-extended-collider-1.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrmc-springbone-extended-collider-1.0/-/types-vrmc-springbone-extended-collider-1.0-3.5.5.tgz",
"integrity": "sha512-+6RxLaAvVguP7upjS/OT4viNPWvac1HBRQY1nmdvfYNQuCMdrMsCZqvy2af0Fcjnda4V9JbKFIpZDC0lrw1+uQ==",
"license": "MIT"
},
"node_modules/@pixiv/types-vrmc-vrm-1.0": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/@pixiv/types-vrmc-vrm-1.0/-/types-vrmc-vrm-1.0-3.5.5.tgz",
"integrity": "sha512-WnemHHo2375BcBdumEhvrupZzsMSzTfdWlokmV9sUX2oC+OAQqJFfBjLF8ppkOhzHpl4MAPJqEfuhBJtNPm9ww==",
"license": "MIT"
},
"node_modules/three": {
"version": "0.169.0",
"resolved": "https://registry.npmjs.org/three/-/three-0.169.0.tgz",
"integrity": "sha512-Ed906MA3dR4TS5riErd4QBsRGPcx+HBDX2O5yYE5GqJeFQTPU+M56Va/f/Oph9X7uZo3W3o4l2ZhBZ6f6qUv0w==",
"license": "MIT"
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"name": "vrmify-qa-harness",
"private": true,
"description": "SpringBone behaviour tests for converted VRM files (three.js + three-vrm, headless)",
"type": "module",
"license": "GPL-3.0",
"dependencies": {
"@pixiv/three-vrm": "3.5.5",
"three": "0.169.0"
}
}
+357
View File
@@ -0,0 +1,357 @@
# -*- coding: utf-8 -*-
"""表情截图对照表:每个模型一行——全身、脸、各个表情预设、口型、眨眼、视线四个方向。
python tools/qa/snapshots.py a.vrm -o a.png
python tools/qa/snapshots.py 旧版/a.vrm 新版/a.vrm -o 对比.png 一个输入一行,上下对照
python tools/qa/snapshots.py model/角色/角色.pmx -o a.png 非 VRM 先用当前代码转一份(临时目录)
python tools/qa/snapshots.py a.vrm --custom -o a.png 再加上模型自己的自定义表情
用无界面的 Edge / Chrome 渲染,渲染部分就是界面预览用的 ui/viewer/viewer-core.js(three-vrm + MToon),
和程序里看到的一样。改表情相关代码前后各拍一张,比对哪里变了;也可以把 tools/regress.py 的样本拍下来看。
模型自己没有的表情格子标「无」,不会拿中性脸冒充。
需要本机装了 Edge 或 Chrome(--browser 指定路径)。WebGL 走软件渲染(SwiftShader),不依赖显卡。
"""
import argparse
import base64
import json
import os
import re
import shutil
import signal
import subprocess
import sys
import tempfile
import threading
import urllib.parse
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
VIEWER = os.path.join(ROOT, "ui", "viewer")
for _p in ("convert", "formats"):
_d = os.path.join(ROOT, _p)
if _d not in sys.path:
sys.path.insert(0, _d)
# 列:[名字, 机位, 表情 {名字: 值}, 视线 (yaw, pitch) 或 None]
SHOTS = [["全身", "body", {}, None], ["脸", "face", {}, None]]
SHOTS += [[n, "face", {n: 1.0}, None] for n in ("happy", "angry", "sad", "relaxed", "surprised",
"aa", "ih", "ou", "ee", "oh", "blink", "blinkLeft", "blinkRight")]
SHOTS += [["look" + k, "face", {}, v] for k, v in
(("Up", (0, 90)), ("Down", (0, -90)), ("Left", (90, 0)), ("Right", (-90, 0)))]
PAGE = r"""<!doctype html><meta charset="utf-8">
<script type="importmap">{ "imports": {
"three": "/viewer/vendor/three/three.module.min.js",
"three/addons/": "/viewer/vendor/three/addons/",
"@pixiv/three-vrm": "/viewer/vendor/three-vrm/three-vrm.module.min.js" } }</script>
<body style="margin:0;background:#222">
<script type="module">
import { Viewer } from '/viewer/viewer-core.js';
const job = await (await fetch('/job')).json();
const send = (o) => fetch('/result', { method: 'POST', body: JSON.stringify(o) });
try {
const S = job.size, PAD = 6, LEFT = 150;
const box = document.createElement('div');
box.style.cssText = `width:${S}px;height:${S}px`;
document.body.append(box);
const v = new Viewer(box, { background: '#8a8f98' });
v.paused = true;
const rows = [];
for (const m of job.models) {
await v.loadUrl(m.url, m.name);
const em = v.vrm.expressionManager, la = v.vrm.lookAt;
const PRESET = new Set(['happy', 'angry', 'sad', 'relaxed', 'surprised', 'aa', 'ih', 'ou', 'ee', 'oh', 'blink',
'blinkLeft', 'blinkRight', 'lookUp', 'lookDown', 'lookLeft', 'lookRight', 'neutral']);
const custom = job.custom && em ? em.expressions.map((e) => e.expressionName).filter((n) => !PRESET.has(n)) : [];
const shots = job.shots.concat(custom.map((n) => [n, 'face', { [n]: 1 }, null]));
v.motion = 'idle'; v._applyMotion(0);
v.vrm.update(1 / 60);
const cells = [];
for (const [label, cam, expr, look] of shots) {
const missing = Object.keys(expr).find((n) => !(em && em.getExpression(n))) || (look && !(la && la.applier));
v.resetExpressions();
for (const [n, w] of Object.entries(expr)) if (!missing) v.setExpression(n, w);
if (look && !missing) v.setLook(look[0], look[1]); else v.setLook(null);
v.vrm.update(0);
// 比界面预览的机位更紧:脸占满格子、全身刚好放下(竖直视角 30°)
const H = v.height, h = v.headY, cy = v.centerY ?? H * 0.52;
const [pos, tgt] = cam === 'face' ? [[0, h - H * 0.02, H * 0.34], [0, h - H * 0.024, 0]]
: [[0, cy, H * 2.1], [0, cy, 0]];
v.camera.position.set(...pos); v.controls.target.set(...tgt); v.controls.update();
const c = document.createElement('canvas'); c.width = c.height = S;
const g = c.getContext('2d');
v.renderer.render(v.scene, v.camera);
g.drawImage(v.renderer.domElement, 0, 0, S, S);
if (missing) { g.fillStyle = 'rgba(0,0,0,.55)'; g.fillRect(0, 0, S, S); g.fillStyle = '#fff'; g.font = `bold ${S / 6}px sans-serif`; g.textAlign = 'center'; g.fillText('无', S / 2, S / 2 + S / 16); }
cells.push([label, c]);
}
v.setLook(null);
rows.push([m.label, cells]);
}
// 每 job.cols 列一段;每段里每个模型一行。每格自带标题(各模型的自定义表情不一样,不能共用表头)
const CAP = 20, ncol = Math.max(...rows.map((r) => r[1].length)), cols = Math.min(job.cols, ncol);
const bands = Math.ceil(ncol / cols), rowH = S + CAP + PAD;
const W = LEFT + cols * (S + PAD) + PAD, H = PAD + bands * (rows.length * rowH + PAD);
const out = document.createElement('canvas'); out.width = W; out.height = H;
const g = out.getContext('2d');
g.fillStyle = '#222'; g.fillRect(0, 0, W, H);
g.font = '14px sans-serif'; g.textBaseline = 'middle';
for (let b = 0; b < bands; b++) {
rows.forEach(([label, cells], r) => {
const y = PAD + b * (rows.length * rowH + PAD) + r * rowH;
g.textAlign = 'left'; g.fillStyle = '#ddd';
const lines = ['']; // 按实际宽度折行
for (const ch of label) {
if (g.measureText(lines[lines.length - 1] + ch).width > LEFT - 14) lines.push('');
lines[lines.length - 1] += ch;
}
lines.slice(0, Math.floor(S / 18)).forEach((w, k) => g.fillText(w, 8, y + CAP + 12 + k * 18));
cells.slice(b * cols, (b + 1) * cols).forEach(([name, c], i) => {
const x = LEFT + PAD + i * (S + PAD);
g.fillStyle = '#ddd'; g.textAlign = 'center';
let t = name;
while (t.length > 1 && g.measureText(t).width > S - 4) t = t.slice(0, -1);
g.fillText(t === name ? t : t.slice(0, -1) + '…', x + S / 2, y + CAP / 2);
g.drawImage(c, x, y + CAP);
});
});
}
await send({ png: out.toDataURL('image/png') });
} catch (e) { await send({ error: String(e && e.stack || e) }); }
</script>
"""
def find_browser(path=None):
cands = [path] if path else []
for env in ("PROGRAMFILES(X86)", "PROGRAMFILES", "LOCALAPPDATA"):
base = os.environ.get(env)
if base:
cands += [os.path.join(base, "Microsoft", "Edge", "Application", "msedge.exe"),
os.path.join(base, "Google", "Chrome", "Application", "chrome.exe")]
cands += [shutil.which(n) for n in ("msedge", "chrome", "google-chrome", "chromium")]
return next((c for c in cands if c and os.path.isfile(c)), None)
# ---- 浏览器进程的清理
# 只结束浏览器主进程是不够的:Windows 上它的子进程(GPU、渲染进程)会留下来,软件渲染的 GPU 进程
# 一直占满一个核。所以结束时连同整棵进程树一起结束;每次开始前再按临时配置目录名找出以前遗留的进程。
PROFILE_PREFIX = "vrmify_snap_profile_"
OWNER_FILE = "owner.pid" # 配置目录里记着是哪个进程开的浏览器,用来判断是不是遗留的
def browser_procs(marker=PROFILE_PREFIX):
"""命令行里带 marker 的进程 → [(进程号, 命令行)](截图浏览器的命令行里有它的临时配置目录)。"""
try:
if os.name == "nt":
ps = ("Get-CimInstance Win32_Process | Where-Object { $_.ProcessId -ne $PID -and "
"$_.CommandLine -like '*%s*' } | ForEach-Object { \"$($_.ProcessId)`t$($_.CommandLine)\" }" % marker)
out = subprocess.run(["powershell", "-NoProfile", "-NonInteractive", "-Command", ps],
capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60).stdout
rows = [ln.split("\t", 1) for ln in out.splitlines() if "\t" in ln]
else:
out = subprocess.run(["ps", "-eo", "pid=,args="], capture_output=True, text=True, timeout=60).stdout
rows = [ln.strip().split(None, 1) for ln in out.splitlines() if marker in ln]
return [(int(p), cmd) for p, cmd in rows if p.strip().isdigit() and int(p) != os.getpid()]
except (OSError, subprocess.SubprocessError, ValueError):
return []
def _kill_pid(pid):
"""结束进程和它的子进程。"""
try:
if os.name == "nt":
subprocess.run(["taskkill", "/PID", str(pid), "/T", "/F"], capture_output=True, timeout=60)
else:
os.kill(pid, signal.SIGKILL)
except (OSError, subprocess.SubprocessError):
pass
def _alive(pid):
try:
if os.name == "nt":
out = subprocess.run(["tasklist", "/FI", "PID eq %d" % pid, "/NH", "/FO", "CSV"],
capture_output=True, text=True, errors="replace", timeout=60).stdout
return '"%d"' % pid in out
os.kill(pid, 0)
return True
except (OSError, subprocess.SubprocessError):
return False
def stop_browser(proc, prof):
"""结束这次截图的浏览器:整棵进程树;主进程先退出了的、树已经断开的,按配置目录名再找一遍。"""
if os.name == "nt":
_kill_pid(proc.pid)
else:
try:
os.killpg(proc.pid, signal.SIGKILL) # 浏览器单独一个进程组(start_new_session)
except OSError:
pass
try:
proc.wait(10)
except subprocess.TimeoutExpired:
proc.kill()
for pid, _ in browser_procs(os.path.basename(prof)):
_kill_pid(pid)
def cleanup_stale(tmp=None):
"""以前遗留的截图浏览器和临时配置目录(异常退出、或者旧版本没关干净的)。正在跑的别的截图
(配置目录里记的进程还活着)不动。"""
tmp = tmp or tempfile.gettempdir()
live = set()
try:
names = [n for n in os.listdir(tmp) if n.startswith(PROFILE_PREFIX)]
except OSError:
names = []
for n in names:
try:
with open(os.path.join(tmp, n, OWNER_FILE)) as f:
owner = int(f.read().strip() or 0)
except (OSError, ValueError):
owner = 0
if owner and owner != os.getpid() and _alive(owner):
live.add(n)
for pid, cmd in browser_procs():
m = re.search(re.escape(PROFILE_PREFIX) + r"[^\s\"'\\/]*", cmd)
if m and m.group(0) not in live:
_kill_pid(pid)
for n in names:
if n not in live:
shutil.rmtree(os.path.join(tmp, n), ignore_errors=True)
def row_label(path, all_paths):
"""行标签:文件名(不带扩展名);有同名的行时前面加上所在文件夹名。"""
def short(p):
return os.path.splitext(os.path.basename(p.split(" · ")[0]))[0] + (" · " + p.split(" · ")[1] if " · " in p else "")
if sum(short(q) == short(path) for q in all_paths) > 1:
return os.path.basename(os.path.dirname(os.path.abspath(path.split(" · ")[0]))) + "/" + short(path)
return short(path)
def to_vrms(paths, tmp, log):
"""→ [(行标签, VRM 路径)];不是 VRM 的先用当前代码转(临时目录,按 VRM 1.0 的默认选项)。"""
import pipeline
out = []
for i, p in enumerate(paths):
if p.lower().endswith(".vrm"):
out.append((p, p))
continue
d = os.path.join(tmp, "conv%d" % i)
log("转换 %s …" % os.path.basename(p))
vrms = [v for v in pipeline.convert_file(p, {}, out_dir=d) if v.lower().endswith(".vrm")]
if not vrms:
raise SystemExit("%s 没有转出 VRM" % p)
out += [(p if len(vrms) == 1 else "%s · %s" % (p, os.path.basename(v)), v) for v in vrms]
return out
def shoot(models, out_png, size=224, custom=False, browser=None, timeout=300, cols=len(SHOTS)):
exe = find_browser(browser)
if not exe:
raise SystemExit("找不到 Edge / Chrome(用 --browser 指定路径)")
files = {"/model/%d.vrm" % i: p for i, (_, p) in enumerate(models)}
models = [(row_label(label, [m[0] for m in models]), p) for label, p in models]
job = {"size": size, "custom": custom, "shots": SHOTS, "cols": cols,
"models": [{"url": "/model/%d.vrm" % i, "name": os.path.basename(p), "label": label}
for i, (label, p) in enumerate(models)]}
result, done = {}, threading.Event()
class H(SimpleHTTPRequestHandler):
def log_message(self, *a):
pass
def _send(self, data, ctype):
self.send_response(200)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
p = urllib.parse.unquote(urllib.parse.urlsplit(self.path).path)
if p == "/":
return self._send(PAGE.encode("utf-8"), "text/html; charset=utf-8")
if p == "/job":
return self._send(json.dumps(job, ensure_ascii=False).encode("utf-8"), "application/json")
if p in files:
with open(files[p], "rb") as f:
return self._send(f.read(), "model/gltf-binary")
return super().do_GET()
def translate_path(self, path):
p = urllib.parse.unquote(urllib.parse.urlsplit(path).path)
parts = [x for x in p[len("/viewer/"):].split("/") if x not in ("", ".", "..")] if p.startswith("/viewer/") else []
return os.path.join(VIEWER, *parts) if parts else os.path.join(VIEWER, "__none__")
def do_POST(self):
n = int(self.headers.get("Content-Length") or 0)
result.update(json.loads(self.rfile.read(n).decode("utf-8")))
self._send(b"{}", "application/json")
done.set()
cleanup_stale()
srv = ThreadingHTTPServer(("127.0.0.1", 0), H)
threading.Thread(target=srv.serve_forever, daemon=True).start()
prof = tempfile.mkdtemp(prefix=PROFILE_PREFIX)
with open(os.path.join(prof, OWNER_FILE), "w") as f:
f.write(str(os.getpid()))
url = "http://127.0.0.1:%d/" % srv.server_address[1]
proc = None
try:
proc = subprocess.Popen([exe, "--headless=new", "--use-angle=swiftshader", "--enable-unsafe-swiftshader",
"--no-first-run", "--no-default-browser-check", "--disable-extensions",
"--user-data-dir=" + prof]
# Linux 的 CI(Ubuntu 24.04 限制了非特权用户命名空间)里 Chrome 的沙箱起不来;只加载本地页面
+ ([] if os.name == "nt" else ["--no-sandbox"]) + [url],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=os.name != "nt")
if not done.wait(timeout):
raise SystemExit("%d 秒内浏览器没有交回截图" % timeout)
finally:
if proc is not None:
stop_browser(proc, prof)
srv.shutdown()
srv.server_close()
shutil.rmtree(prof, ignore_errors=True)
if "error" in result:
raise SystemExit("页面出错:\n" + result["error"])
data = base64.b64decode(result["png"].split(",", 1)[1])
os.makedirs(os.path.dirname(os.path.abspath(out_png)), exist_ok=True)
with open(out_png, "wb") as f:
f.write(data)
return out_png
def main(argv=None):
ap = argparse.ArgumentParser(description="表情截图对照表(每个输入一行)")
ap.add_argument("inputs", nargs="+", help=".vrm,或任何能转换的模型(先用当前代码转)")
ap.add_argument("-o", "--out", default="snapshots.png", help="输出 PNG")
ap.add_argument("--size", type=int, default=224, help="每格边长(像素)")
ap.add_argument("--custom", action="store_true", help="再加上模型自己的自定义表情")
ap.add_argument("--cols", type=int, default=len(SHOTS), help="每段多少列,超出的换到下一段(默认 %d,正好放下标准的一组)" % len(SHOTS))
ap.add_argument("--browser", help="Edge / Chrome 的路径(默认自动找)")
ap.add_argument("--timeout", type=int, default=300)
a = ap.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
tmp = tempfile.mkdtemp(prefix="vrmify_snap_")
try:
models = to_vrms(a.inputs, tmp, print)
print("截图 %d 个模型 …" % len(models))
shoot(models, a.out, a.size, a.custom, a.browser, a.timeout, a.cols)
print("已写出 %s" % os.path.abspath(a.out))
finally:
shutil.rmtree(tmp, ignore_errors=True)
return 0
if __name__ == "__main__":
sys.exit(main())
+187
View File
@@ -0,0 +1,187 @@
# -*- coding: utf-8 -*-
"""真实模型的回归检查:改转换代码前后,同样的输入输出是否一致。
python tools/regress.py record 模型.pmx|目录 ... [--spec 0x] 记下基准(golden)
python tools/regress.py check 用当前代码重转、对比基准
python tools/regress.py check --code 另一份代码的目录 用别的代码(例如 git worktree)转
基准存在 tests/.golden/(不进仓库:模型的许可不允许再分发,衍生的数据也只留在本机):
manifest.json 记样本路径和选项,每个样本一个 JSON(glTF JSON 全文 + 二进制块 sha256)。
check 时每个样本都会:转换 → check_vrm 结构 / Schema 校验 → 和基准对比,列出 JSON 里
不同的路径(最多 20 条)和二进制块是否一致;有错误或差异时退出码为 1。
对比只是提示「哪里变了」,变化是不是预期的要自己判断,确认后重新 record。
"""
import argparse
import hashlib
import json
import os
import shutil
import sys
import tempfile
import time
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
GOLDEN = os.path.join(ROOT, "tests", ".golden")
sys.path.insert(0, os.path.join(HERE, "qa"))
def _use_code(root):
"""让 import pmx2vrm / vrmio 来自 root 这份代码。"""
for p in (os.path.join(root, "formats"), os.path.join(root, "convert")):
sys.path.insert(0, p)
def _convert(pmx, out, spec):
import pmx2vrm
pmx2vrm.convert(pmx, out, spec="1.0" if spec == "1.0" else "0.x",
meta={"title": os.path.splitext(os.path.basename(pmx))[0]})
def _fingerprint(path):
import vrmio
g, b = vrmio.read_glb(path)
return {"json": g, "bin_sha256": hashlib.sha256(b).hexdigest(), "bin_len": len(b),
"size": os.path.getsize(path)}
def _diff(a, b, path="", out=None, limit=20):
out = [] if out is None else out
if len(out) >= limit:
return out
if isinstance(a, dict) and isinstance(b, dict):
for k in list(a) + [k for k in b if k not in a]:
if k not in b:
out.append("- %s/%s" % (path, k))
elif k not in a:
out.append("+ %s/%s" % (path, k))
else:
_diff(a[k], b[k], "%s/%s" % (path, k), out, limit)
if len(out) >= limit:
break
elif isinstance(a, list) and isinstance(b, list):
if len(a) != len(b):
out.append("~ %s 长度 %d → %d" % (path, len(a), len(b)))
for i, (x, y) in enumerate(zip(a, b)):
_diff(x, y, "%s/%d" % (path, i), out, limit)
if len(out) >= limit:
break
elif a != b:
out.append("~ %s:%s → %s" % (path, json.dumps(a, ensure_ascii=False)[:60],
json.dumps(b, ensure_ascii=False)[:60]))
return out
def _samples(paths):
out = []
for p in paths:
if os.path.isdir(p):
for d, _, fs in os.walk(p):
out += [os.path.join(d, f) for f in sorted(fs) if f.lower().endswith(".pmx")]
elif p.lower().endswith(".pmx"):
out.append(p)
return [os.path.abspath(p) for p in out]
def _key(path, spec):
return hashlib.md5(("%s|%s" % (os.path.normcase(path), spec)).encode("utf-8")).hexdigest()[:12]
def record(args):
samples = _samples(args.paths)
if not samples:
print("没有找到 PMX")
return 1
os.makedirs(GOLDEN, exist_ok=True)
tmp = tempfile.mkdtemp(prefix="m2v_regress_")
manifest = {"spec": args.spec, "samples": samples, "recorded": time.strftime("%Y-%m-%d %H:%M"),
"code": os.path.abspath(args.code)}
try:
for n, pmx in enumerate(samples, 1):
out = os.path.join(tmp, "%d.vrm" % n)
t0 = time.time()
_convert(pmx, out, args.spec)
with open(os.path.join(GOLDEN, _key(pmx, args.spec) + ".json"), "w", encoding="utf-8") as f:
json.dump(_fingerprint(out), f, ensure_ascii=False)
print("[%d/%d] %.1fs %s" % (n, len(samples), time.time() - t0, pmx))
finally:
shutil.rmtree(tmp, ignore_errors=True)
with open(os.path.join(GOLDEN, "manifest.json"), "w", encoding="utf-8") as f:
json.dump(manifest, f, ensure_ascii=False, indent=1)
print("基准已记录:%d 个样本 → %s" % (len(samples), GOLDEN))
return 0
def check(args):
import check_vrm
try:
with open(os.path.join(GOLDEN, "manifest.json"), encoding="utf-8") as f:
manifest = json.load(f)
except OSError:
print("还没有基准:先运行 python tools/regress.py record <模型或目录>")
return 1
validators, why = (None, "--no-schema") if args.no_schema else check_vrm.load_validators()
if why:
print("提示:" + why)
spec = manifest["spec"]
tmp = tempfile.mkdtemp(prefix="m2v_regress_")
bad = 0
try:
for n, pmx in enumerate(manifest["samples"], 1):
name = os.path.basename(pmx)
gpath = os.path.join(GOLDEN, _key(pmx, spec) + ".json")
if not os.path.isfile(pmx) or not os.path.isfile(gpath):
print("[%d] 跳过(找不到样本或基准):%s" % (n, pmx))
continue
out = os.path.join(tmp, "%d.vrm" % n)
t0 = time.time()
try:
_convert(pmx, out, spec)
except Exception as e: # noqa: BLE001
print("✗ [%d] %s 转换失败:%s: %s" % (n, name, type(e).__name__, e))
bad += 1
continue
errs, _, _ = check_vrm.check(out, validators)
with open(gpath, encoding="utf-8") as f:
gold = json.load(f)
cur = _fingerprint(out)
diffs = _diff(gold["json"], cur["json"])
same_bin = gold["bin_sha256"] == cur["bin_sha256"]
ok = not errs and not diffs and same_bin
bad += not ok
print("%s [%d] %.1fs %s %.2f → %.2f MB" % ("✓" if ok else "✗", n, time.time() - t0, name,
gold["size"] / 1048576.0, cur["size"] / 1048576.0))
for e in errs[:10]:
print(" 校验:" + e)
for d in diffs:
print(" JSON " + d)
if not same_bin:
print(" 二进制块不同(%d → %d 字节)" % (gold["bin_len"], cur["bin_len"]))
finally:
shutil.rmtree(tmp, ignore_errors=True)
print("对比 %d 个样本,%d 个有差异或错误" % (len(manifest["samples"]), bad))
return 1 if bad else 0
def main(argv=None):
ap = argparse.ArgumentParser(description="真实模型回归检查")
sub = ap.add_subparsers(dest="cmd", required=True)
r = sub.add_parser("record", help="记录基准")
r.add_argument("paths", nargs="+", help="PMX 文件或目录")
r.add_argument("--spec", default="1.0", choices=["1.0", "0x"])
c = sub.add_parser("check", help="用当前代码重转并对比基准")
c.add_argument("--no-schema", action="store_true")
for p in (r, c):
p.add_argument("--code", default=ROOT, help="用哪份代码转换(默认本仓库)")
a = ap.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
_use_code(a.code)
import pmx2vrm # noqa: F401 先从 --code 导入(check_vrm 会把本仓库的路径插到最前面)
return record(a) if a.cmd == "record" else check(a)
if __name__ == "__main__":
sys.exit(main())
+113
View File
@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
"""用本机的真实模型跑一遍骨骼处理和自检(不写 VRM,几秒一个),看哪些模型会在转换日志里报问题。
python tools/scan_models.py 目录或文件 ... # 目录递归找 PMX 和压缩包(压缩包在临时目录里解开)
python tools/scan_models.py ~/Downloads -o 结果.txt
每个 PMX 一行:合并的付与骨数、按比例拆权重的骨数、自检问题;有问题的下面列出来。
改骨骼相关的代码前后各跑一次对比,发版前跑一次。有自检问题时退出码为 1。
"""
import argparse
import glob
import os
import shutil
import sys
import tempfile
import time
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
for _p in ("convert", "formats"):
sys.path.insert(0, os.path.join(ROOT, _p))
import pipeline # noqa: E402
import pmxio # noqa: E402
from vrmconv import checks, mesh, skeleton # noqa: E402
def pmx_files(paths, tmp):
"""→ (PMX 路径, 显示名);压缩包解到 tmp 下,包里套包再解一层。"""
def inside(arc, d):
for q in pipeline.find_inputs(d):
k = pipeline.classify(q)
if k == "pmx":
yield q, os.path.basename(arc) + "/" + os.path.relpath(q, d)
elif k in pipeline.ARCHIVES:
sub = tempfile.mkdtemp(dir=tmp)
pipeline.extract_archive(q, k, sub)
yield from ((f, os.path.basename(arc) + "/" + os.path.relpath(f, sub))
for f in glob.glob(os.path.join(sub, "**", "*.pmx"), recursive=True))
for p in paths:
if os.path.isdir(p):
for f in sorted(glob.glob(os.path.join(p, "**", "*"), recursive=True)):
k = pipeline.classify(f) if os.path.isfile(f) else None
if k == "pmx":
yield f, os.path.relpath(f, p)
elif k in pipeline.ARCHIVES:
yield from pmx_files([f], tmp)
elif pipeline.classify(p) == "pmx":
yield p, os.path.basename(p)
elif pipeline.classify(p) in pipeline.ARCHIVES:
d = tempfile.mkdtemp(dir=tmp)
try:
pipeline.extract_archive(p, pipeline.classify(p), d)
yield from inside(p, d)
except Exception as e: # noqa: BLE001
print("跳过 %s:%s" % (p, e), file=sys.stderr)
def scan(path):
"""→ (合并数, 拆分骨数, [自检问题文字]),和转换时一样的骨骼处理(VRM 1.0、T-pose)。"""
model = pmxio.read_pmx(path)
lines = []
log = lambda m, t=None: lines.append((m, t)) # noqa: E731
skeleton.fix_facing(model, log)
verts, bones = model["vertices"], model["bones"]
ys = [v["pos"][1] for v in verts] or [0.0]
scale = 1.6 / (max(ys) - min(ys)) if max(ys) > min(ys) else 0.08
raw = mesh.vertex_weights(verts, len(bones), {})
bs = skeleton.prepare_bones(bones, True, log, weighted={b for wl in raw for b, w in wl if w > 0})
vw = mesh.vertex_weights(verts, len(bones), bs.alias, base=raw)
_, bpos, vpos, _, _ = skeleton.apply_tpose(verts, bones, bs, vw, True, log)
vw = skeleton.split_partial(bones, bs, vw, model.get("rigid_bodies"), log)
split = next((int(m.split(":")[1].split(" ")[0]) for m, _ in lines if m.startswith("付与骨:")), 0)
checks.report(bones, bs, raw, vw, bpos, vpos, scale, log)
return len(bs.alias), split, [m for m, t in lines if m.startswith("自检")]
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
ap.add_argument("paths", nargs="+")
ap.add_argument("-o", "--out", help="结果另存一份到文件")
a = ap.parse_args()
out = [sys.stdout] + ([open(a.out, "w", encoding="utf-8")] if a.out else [])
def say(s):
for f in out:
print(s, file=f, flush=True)
tmp = tempfile.mkdtemp(prefix="vrmify_scan_")
seen, n, bad, t0 = set(), 0, 0, time.time()
try:
for f, label in pmx_files(a.paths, tmp):
key = (os.path.basename(f), os.path.getsize(f))
if key in seen:
continue
seen.add(key)
n += 1
try:
merged, split, issues = scan(f)
except Exception as e: # noqa: BLE001
say("错误 %s:%s: %s" % (label, type(e).__name__, e))
continue
bad += bool(issues)
say("%s 合并 %3d · 拆分 %3d %s" % ("!!" if issues else "ok", merged, split, label))
for m in issues:
say(" " + m)
finally:
shutil.rmtree(tmp, ignore_errors=True)
say("%d 个 PMX,%d 个有自检问题(%.0f 秒)" % (n, bad, time.time() - t0))
return 1 if bad else 0
if __name__ == "__main__":
sys.exit(main())
+265
View File
@@ -0,0 +1,265 @@
# -*- coding: utf-8 -*-
"""VRMify 增强版:发布前的冒烟测试:启动打包好的 exe,通过它自己的 HTTP 接口转换几个合成模型,检查结果。
python tools/smoke_exe.py dist/VRMify-v0.1.2-win64.zip 发布用的 zip
python tools/smoke_exe.py dist/VRMify 解开的程序文件夹(或 exe 本身)
单元测试测的是源码;这里测的是**打包以后**还能不能用:PyInstaller 漏打的模块(某种输入格式
用到的 fbx2pmx / unitypackage_unpack / archive …)、漏打的页面和预览库、exe 起不来,都只在这里暴露。
步骤(数据目录用临时文件夹,不碰本机的 settings.json / edits.json):
1. 用 tests/synth.py 合成 PMX,再做出 zip、FBX、unitypackage 三种输入;
2. `VRMify.exe --no-open --port N` 启动,等 /api/ping;
3. 版本号要和 ui/version.py 一致;页面、预览组件、three.js / three-vrm / ammo.js 都能取到;
4. 加入四个文件 → 转换 → 等转换结束:每个都成功、各出一个 VRM,日志里没有错误;
5. 每个 VRM 过 tools/qa/check_vrm(装了 jsonschema 时含官方 Schema);
6. 预览(PMX 原生预览、VRM 文件)和调整面板的接口能用。
有任何一项失败退出码为 1,exe 的输出打印在最后。GitHub Actions 的 release.yml 打包后会跑它。
"""
import argparse
import io
import json
import os
import shutil
import socket
import subprocess
import sys
import tarfile
import tempfile
import time
import urllib.error
import urllib.request
import zipfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
for _p in ("tests", "convert", "formats", os.path.join("tools", "qa"), "ui"):
_d = os.path.join(ROOT, _p)
if _d not in sys.path:
sys.path.insert(0, _d)
APP = "VRMify"
STATIC = ["/", "/app.js", "/core.js", "/files.js", "/options.js", "/run.js", "/preview.js", "/tune.js", "/layout.js", "/i18n.js", "/viewer/viewer-core.js", "/viewer/vendor/three/three.module.min.js",
"/viewer/vendor/three-vrm/three-vrm.module.min.js", "/viewer/vendor/three/addons/libs/ammo.wasm.js",
"/viewer/vendor/three/addons/loaders/MMDLoader.js"]
class Failed(Exception):
pass
def make_inputs(folder):
"""合成模型 → {名字: 路径}:PMX、zip(PMX + 贴图)、FBX、unitypackage(里面一个 FBX)。"""
import fbxout
import synth
pmx = synth.build(os.path.join(folder, "pmx"), "合成")
src = os.path.dirname(synth.build(os.path.join(folder, "zip_src"), "合成zip")) # 输出名不和 PMX 的撞
zp = os.path.join(folder, "合成包.zip")
with zipfile.ZipFile(zp, "w") as z:
for d, _, fs in os.walk(src):
for fn in fs:
p = os.path.join(d, fn)
z.write(p, os.path.join("合成包", os.path.relpath(p, src)))
fbx = os.path.join(folder, "fbx", "Synth.fbx")
os.makedirs(os.path.dirname(fbx))
# FBX 用英文骨名:导出会把日文名换成下划线,那样认不出人形骨架、会被「只转换人物」跳过
fbxout.export_pmx(synth.build(os.path.join(folder, "fbx_src"), "Synth", english=True), fbx,
log=lambda *a, **k: None)
pkg = os.path.join(folder, "Synth.unitypackage")
with tarfile.open(pkg, "w:gz") as tf:
guid = "%032x" % 1
tf.add(fbx, guid + "/asset")
info = tarfile.TarInfo(guid + "/pathname")
data = b"Assets/Synth/SynthPkg.fbx" # 输出名不和单独的 FBX 撞
info.size = len(data)
tf.addfile(info, io.BytesIO(data))
return {"pmx": pmx, "zip": zp, "fbx": fbx, "unitypackage": pkg}
def find_exe(target, tmp):
if os.path.isfile(target) and target.lower().endswith(".zip"):
with zipfile.ZipFile(target) as z:
z.extractall(os.path.join(tmp, "app"))
target = os.path.join(tmp, "app")
if os.path.isdir(target):
for d, _, fs in os.walk(target):
if APP + ".exe" in fs:
return os.path.join(d, APP + ".exe")
raise Failed("在 %s 里没找到 %s.exe" % (target, APP))
return target
def free_port():
s = socket.socket()
s.bind(("127.0.0.1", 0))
port = s.getsockname()[1]
s.close()
return port
class Client:
def __init__(self, port):
self.base = "http://127.0.0.1:%d" % port
def get(self, path, timeout=30):
try:
with urllib.request.urlopen(self.base + path, timeout=timeout) as r:
return r.status, r.read()
except urllib.error.HTTPError as e:
return e.code, b""
def json(self, path, body=None, timeout=60):
data = None if body is None else json.dumps(body).encode("utf-8")
req = urllib.request.Request(self.base + path, data=data,
headers={"Content-Type": "application/json"} if data else {})
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read().decode("utf-8"))
def run(exe, inputs, tmp, report, timeout):
import check_vrm
from version import __version__
port = free_port()
env = dict(os.environ, VRMIFY_DATA=os.path.join(tmp, "data"))
out_log = open(os.path.join(tmp, "exe.log"), "wb")
proc = subprocess.Popen([exe, "--no-open", "--port", str(port)], env=env, cwd=tmp,
stdout=out_log, stderr=subprocess.STDOUT)
c = Client(port)
try:
t0 = time.time()
while True:
if proc.poll() is not None:
raise Failed("exe 启动后退出了(退出码 %s)" % proc.returncode)
try:
c.get("/api/ping", timeout=2)
break
except OSError:
if time.time() - t0 > 90:
raise Failed("90 秒内 /api/ping 没有响应")
time.sleep(0.5)
report("启动 %.1f 秒" % (time.time() - t0))
st = c.json("/api/state")
if st.get("version") != __version__:
raise Failed("版本号 %r,ui/version.py 是 %r" % (st.get("version"), __version__))
app_out = st.get("app_out") or ""
if not os.path.normcase(app_out).startswith(os.path.normcase(env["VRMIFY_DATA"])):
raise Failed("默认输出目录 %r 不在数据目录里" % app_out)
report("版本 %s · 默认输出 %s" % (__version__, app_out))
for p in STATIC:
code, body = c.get(p)
if code != 200 or not body:
raise Failed("取不到 %s(HTTP %s)" % (p, code))
report("页面和预览库 %d 个文件都在" % len(STATIC))
ids = c.json("/api/add", {"paths": list(inputs.values())})["added"]
files = {f["id"]: f for f in c.json("/api/state")["files"]}
kinds = sorted(files[i]["kind"] for i in ids)
if len(ids) != len(inputs):
raise Failed("加入了 %d 个文件,应为 %d(%s)" % (len(ids), len(inputs), kinds))
report("加入 %s" % " / ".join(kinds))
# 没转换之前,PMX 的预览是原生预览:/model/… 直接给 PMX 文件
pmx_id = next(i for i in ids if files[i]["kind"] == "pmx")
pv = c.json("/api/preview", {"id": pmx_id})
code, body = c.get(pv.get("url") or "/missing")
if code != 200 or not body.startswith(b"PMX "):
raise Failed("PMX 原生预览取不到:%s" % pv)
seq = c.json("/api/state")["seq"]
if not c.json("/api/convert", {"ids": []}).get("started"):
raise Failed("/api/convert 没有开始")
errors, msgs, t0, finished = [], [], time.time(), None
while finished is None:
if time.time() - t0 > timeout:
raise Failed("%d 秒内没转完" % timeout)
for ev in c.json("/api/events?after=%d" % seq, timeout=30)["events"]:
seq = max(seq, ev["seq"])
if ev.get("type") == "log":
msgs.append(ev["msg"])
if ev.get("tag") == "err":
errors.append(ev["msg"])
if ev.get("type") == "busy" and ev.get("busy") is False:
finished = ev
report("转换 %.1f 秒:成功 %s、失败 %s" % (time.time() - t0, finished.get("done"), finished.get("failed")))
if errors:
raise Failed("日志里有错误:\n" + "\n".join(errors)[-3000:])
# 几个文件会同时转:子进程也是这个 exe(靠 ui/app.py 的 freeze_support);退回一个一个转说明多进程坏了
if any("没法同时转换" in m for m in msgs):
raise Failed("打包后的 exe 开不起转换子进程(退回了一个一个转)")
report(next((m for m in msgs if m.startswith("同时转换")), "一个一个转(这台机器的 CPU / 内存只够一个)"))
validators, why = check_vrm.load_validators()
if why:
report(why)
files = {f["id"]: f for f in c.json("/api/state")["files"]}
vrm_url = None
for i in ids:
f = files[i]
vrms = [o for o in f["outputs"] if o["path"].lower().endswith(".vrm")]
if f["status"] != "done" or len(vrms) != 1:
raise Failed("%s:状态 %s,VRM 输出 %d 个(%s)" % (f["name"], f["status"], len(vrms), f.get("error")))
path = vrms[0]["path"]
if not os.path.isfile(path):
raise Failed("%s:输出文件不存在 %s" % (f["name"], path))
errs, _, summary = check_vrm.check(path, validators)
if errs:
raise Failed("%s 校验失败:\n %s" % (os.path.basename(path), "\n ".join(errs[:10])))
report("%-14s → %s(%.2f MB)" % (f["kind"], os.path.basename(path), os.path.getsize(path) / 1048576.0))
vrm_url = vrm_url or vrms[0]["url"]
code, body = c.get(vrm_url)
if code != 200 or not body.startswith(b"glTF"):
raise Failed("VRM 文件预览取不到:%s" % vrm_url)
tune = c.json("/api/tune", {"id": pmx_id})
if tune.get("error") or not tune.get("expressions"):
raise Failed("调整面板的接口异常:%s" % str(tune)[:300])
report("预览(PMX / VRM)和调整面板接口正常")
if proc.poll() is not None:
raise Failed("exe 中途退出了(退出码 %s)" % proc.returncode)
finally:
proc.terminate()
try:
proc.wait(10)
except subprocess.TimeoutExpired:
proc.kill()
out_log.close()
def main(argv=None):
ap = argparse.ArgumentParser(description="打包后的 exe 冒烟测试")
ap.add_argument("target", help="发布 zip、解开的程序文件夹或 exe")
ap.add_argument("--timeout", type=int, default=300, help="转换的最长等待秒数")
ap.add_argument("--keep", action="store_true", help="保留临时文件夹(看输出和 exe 日志)")
a = ap.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
tmp = tempfile.mkdtemp(prefix="vrmify_smoke_")
ok = False
try:
exe = find_exe(os.path.abspath(a.target), tmp)
print("exe:%s" % exe)
inputs = make_inputs(os.path.join(tmp, "inputs"))
run(exe, inputs, tmp, lambda m: print(" ✓ " + m, flush=True), a.timeout)
ok = True
print("冒烟测试通过")
except Failed as e:
print("✗ %s" % e)
except Exception as e: # noqa: BLE001 接口返回了意料之外的东西:同样算失败,照样打印 exe 输出
print("✗ %s: %s" % (type(e).__name__, e))
finally:
log = os.path.join(tmp, "exe.log")
if not ok and os.path.isfile(log):
with open(log, "rb") as f:
print("---- exe 输出(最后 4000 字)----\n" + f.read().decode("utf-8", "replace")[-4000:])
if a.keep:
print("临时文件夹:%s" % tmp)
else:
shutil.rmtree(tmp, ignore_errors=True)
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())