Files
EVEngine/scripts/export_meshret_onnx.py
西风逍遥游 31880f0021 feat(examples): gate examples in CI and make repository scripts UTF-8 on every host
`examples/<name>/` was a tested surface in name only. The source-quality job had
no examples check at all, and scripts/smoke_examples.sh accepted any process that
had not printed one of its error markers: a project that died during startup,
exited after a single frame, or crashed without a marker was reported as PASS.
Nothing stopped a malformed or unregistered example from reaching dev, which is
how 14 examples arrived without READMEs and 20 were never registered in the
overview table.

Source side
- scripts/check_examples.py classifies every directory under examples/ as either
  runnable (main.nut plus a config.nut that assigns the engine `config` table) or
  as a reviewed non-runnable sample listed in NON_RUNNABLE_EXAMPLES with a
  reason. Both kinds must own a README.md and appear in examples/README.md.
  Stale exemptions and overview links to missing directories fail as well.
- `make check/examples` and a new source-quality CI step run the gate.
- scripts/tests/test_check_examples.py covers it and pins each failure mode
  against fixtures.

Runtime side
- scripts/smoke_examples.sh now enforces liveness, not just marker absence: every
  runnable example must still be running after MIN_RUN_SECONDS (default 2s; CI
  passes 2s), and a process that dies inside that window fails whatever its exit
  code. A clean self-exit after the window passes with an explicit note instead
  of being silently accepted as a pass.
- The Linux CI step sets MIN_RUN_SECONDS=2 and keeps RUN_SECONDS=6, so the job's
  wall clock is unchanged (13.7 min of its 20 min budget on dev).

Windows / GBK
- scripts/utf8_stdio.py re-encodes stdout/stderr as UTF-8 and every scripts/*.py
  program calls it as the first statement of its __main__ block. On a Chinese
  Windows a redirected stream falls back to the ANSI code page (cp936), and
  printing a character that code page cannot encode (arrows, superscripts, bullet
  separators, emoji - all common in this repository) raised UnicodeEncodeError
  and turned a checker into a traceback.
- The mirror image is input: read_text() without encoding= decoded UTF-8
  repository text as cp936 and raised UnicodeDecodeError.
- scripts/tests/test_utf8_stdio.py enforces both conventions for every script and
  test module; text file I/O and child-process decoding now pass encoding="utf-8"
  explicitly.
- Unit-test invocations run with `python3 -X utf8` so the runner's own output
  never follows the host code page.

Violations the new gates found on top of dev, fixed here: 14 examples shipped
without a README (fluid3d, mesh-impact-lab, mesh-modifier-lab, spline-tube-lab on
the earlier dev tip, plus pcg-depth-of-field, pcg-detail-overwrite,
pcg-layer-culling, pcg-location-system, pcg-runtime-orchestration,
pcg-scene-player-culling, pcg-snow-wind, pcg-thunder-strike, pcg-ui-scaler and
pcg-underwater added by #397), 20 were missing from the overview table (those 14
plus archspace-editor, geometry-stroke-lab, interior-mapping, layered-animation,
triplanar-decal, terrain-stamping), and scripts/export_meshret_onnx.py had no
UTF-8 stdio guard.

Verified locally: `python -m ruff check scripts` (All checks passed);
scripts/check_test_manifest.py; scripts/check_quality_metadata.py;
scripts/check_architecture_contracts.py --base HEAD; scripts/check_nodiscard.py;
`make -n check/examples`; scripts/check_examples.py (125 examples: 118 runnable,
7 reviewed non-runnable); and `python -X utf8 -m unittest discover -s scripts/tests
-p "test_*.py"` (155 tests, OK). The runtime half of the contract needs a Linux
headless build, so it is verified by the CI examples smoke step rather than
locally.
2026-09-15 20:20:23 +08:00

77 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Export a MeshRet-compatible ONNX stub matching SmrOnnxRunner's I/O contract.
Inputs:
source_rot6d [1,T,J,6]
source_geom [1,S,7]
target_geom [1,S,7]
source_dmi [1,T,P,10]
Output:
target_rot6d [1,T,J,6]
The default graph is an identity passthrough (target_rot6d = source_rot6d) so
engine integration can be validated without MeshRet research weights.
Trained MeshRet checkpoints can be converted to this contract offline.
"""
from __future__ import annotations
import argparse
from pathlib import Path
import utf8_stdio
def build_identity_onnx(path: Path, joints: int = 8, sensors: int = 16, pairs: int = 8, frames: int = 4) -> None:
import numpy as np
import onnx
from onnx import TensorProto, helper, numpy_helper
T, J, S, P = frames, joints, sensors, pairs
source_rot = helper.make_tensor_value_info("source_rot6d", TensorProto.FLOAT, [1, T, J, 6])
source_geom = helper.make_tensor_value_info("source_geom", TensorProto.FLOAT, [1, S, 7])
target_geom = helper.make_tensor_value_info("target_geom", TensorProto.FLOAT, [1, S, 7])
source_dmi = helper.make_tensor_value_info("source_dmi", TensorProto.FLOAT, [1, T, P, 10])
target_rot = helper.make_tensor_value_info("target_rot6d", TensorProto.FLOAT, [1, T, J, 6])
# Keep unused inputs live so loaders accept the full MeshRet feed set.
zero = numpy_helper.from_array(np.zeros((1,), dtype=np.float32), name="zero")
reduce_g = helper.make_node("ReduceSum", ["source_geom"], ["geom_sum"], keepdims=0)
reduce_t = helper.make_node("ReduceSum", ["target_geom"], ["tgeom_sum"], keepdims=0)
reduce_d = helper.make_node("ReduceSum", ["source_dmi"], ["dmi_sum"], keepdims=0)
add_gt = helper.make_node("Add", ["geom_sum", "tgeom_sum"], ["geom_t"])
add_all = helper.make_node("Add", ["geom_t", "dmi_sum"], ["side_sum"])
mul = helper.make_node("Mul", ["side_sum", "zero"], ["side_zero"])
# Broadcast side_zero into a [1,T,J,6] additive identity via Shape+Expand is heavy;
# identity Identity node on source_rot6d is the contract output.
identity = helper.make_node("Identity", ["source_rot6d"], ["target_rot6d"])
graph = helper.make_graph(
[reduce_g, reduce_t, reduce_d, add_gt, add_all, mul, identity],
"meshret_identity",
[source_rot, source_geom, target_geom, source_dmi],
[target_rot],
[zero],
)
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
model.ir_version = 8
onnx.checker.check_model(model)
path.parent.mkdir(parents=True, exist_ok=True)
onnx.save(model, str(path))
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-o", "--output", type=Path, default=Path("build/meshret_identity.onnx"))
parser.add_argument("--joints", type=int, default=8)
parser.add_argument("--sensors", type=int, default=16)
parser.add_argument("--pairs", type=int, default=8)
parser.add_argument("--frames", type=int, default=4)
args = parser.parse_args()
build_identity_onnx(args.output, args.joints, args.sensors, args.pairs, args.frames)
print(f"wrote {args.output}")
if __name__ == "__main__":
utf8_stdio.enable_utf8_stdio()
main()