Newton 提供了一个简单的 OpenGL 渲染器,用于可视化模拟。渲染器需要安装 pyglet(版本 >= 2.0)。
python复制代码
renderer = newton.utils.SimRendererOpenGL(model=model, path="Newton Simulator", scaling=2.0)
# at every frame:
renderer.begin_frame(sim_time)
renderer.render(state)
renderer.end_frame()
# pause the simulation (blocks the control flow):
renderer.pause = True
| Key(s) | Description |
| W, A, S, D (or arrow keys) + mouse drag | 像在 FPS 游戏中一样移动摄像头 |
| X | 切换线框渲染 |
| B | 切换背面剔除 |
| C | 切换坐标系轴的可见性 |
| G | 切换地面网格 |
| D | 切换深度渲染 |
| I | 切换左上角的信息文本 |
| SPACE | 暂停/继续模拟 |
TAB
跳过渲染,在后台继续模拟(可在运行渲染器时加快 RL 训练速度)
[键盘快捷键]
1.2 USD 渲染
您也可以将模拟渲染为时间采样的美元阶段,以便在 Omniverse 中可视化,而不是实时渲染。
python复制代码
renderer = newton.utils.SimRenderer(model=model, path="simulation.usd", scaling=2.0)
# at every frame:
renderer.begin_frame(sim_time)
renderer.render(state)
renderer.end_frame()
# to save the USD stage:
renderer.save()
1.3 示例:创建一个粒子链
python复制代码
import newton
builder = newton.ModelBuilder()
# anchor point (zero mass)
builder.add_particle((0, 1.0, 0.0), (0.0, 0.0, 0.0), 0.0)
# build chain
for i in range(1, 10):
builder.add_particle((i, 1.0, 0.0), (0.0, 0.0, 0.0), 1.0)
builder.add_spring(i - 1, i, 1.0e3, 0.0, 0)
model = builder.finalize("cpu")
print(f"{model.spring_indices.numpy()=}")
print(f"{model.particle_count=}")
print(f"{model.particle_mass.numpy()=}")
# On macOS and Linux.
curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows.
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
运行基本示例:
bash复制代码
# An example with basic dependencies
uv run newton/examples/example_quadruped.py
# An example that requires extras
uv run --all-extras newton/examples/example_humanoid.py
更新项目锁文件中的所有依赖项,运行后记得提交 uv.lock:
bash复制代码
uv lock -U
7.1.2 使用 venv
本说明适用于希望使用 venv 或 Conda(如 Miniforge)建立开发环境的用户。
bash复制代码
python -m venv .venv
On macOS and Linux.
source .venv/bin/activate
On Windows (console).
.venv\Scripts\activate.bat
On Windows (PowerShell).
.venv\Scripts\Activate.ps1
# An example with basic dependencies
python newton/examples/example_quadruped.py
# An example that requires extras
python newton/examples/example_humanoid.py
7.2 运行测试
Newton 测试套件可通过 uv run -m newton.tests 或 python -m newton.tests 运行。默认情况下,测试套件将在最多八个进程上并行执行。通过 --help 标志可查看测试运行器的可用选项。
uv run --all-extras -m newton.tests --coverage --coverage-html htmlcov
可使用网络浏览器打开文件 htmlcov/index.html,查看覆盖率报告。
7.3 代码格式化和内核
预提交(pre-commit)可用于确保本地代码符合 Newton 的检查。从版本库顶部运行
bash复制代码
# With uv installed
uvx pre-commit run -a
# With venv
python -m pip install pre-commit
pre-commit run -a
自动运行 git commit 的预提交钩子:
bash复制代码
# With uv installed
uvx pre-commit install
# With venv
pre-commit install
可通过预提交卸载卸载钩子。
7.4 构建文档
以下是构建文档的几种方法。
7.4.1 使用 uv
bash复制代码
rm -rf docs/_build
uv run --extra docs sphinx-build -W -b html docs docs/_build/html
Alternatively using the Makefile
uv sync --extra docs
source .venv/bin/activate
cd docs
make clean
make html
7.4.2 使用 venv
bash复制代码
python -m pip install -e .[docs]
python -m sphinx -W -b html docs docs/_build/html
# Alternatively using the Makefile
cd docs
make clean
make html
7.5 测试文档代码片段
doctest Sphinx 生成器用于确保文档中的代码片段保持最新。
可以通过以下方式运行 doctests
bash复制代码
# With uv installed
uv run --extra docs sphinx-build -W -b doctest docs docs/_build/doctest
# With venv
python -m sphinx -W -b doctest docs docs/_build/doctest
更多信息,请参阅 sphinx.ext.doctest 文档。
7.6 打包
要生成 .whl 文件(例如,测试分发包的创建),请运行
bash复制代码
uv build --wheel
输出结果将放在 dist/ 子目录下。
八、贡献指南
为开发 Newton 做出贡献的方式包括
在 GitHub 上报告错误和申请新功能。
在 GitHub 上提问、分享您的工作或参与讨论主题。
向 Newton 代码库添加新示例。
改进文档。
贡献错误修复或新功能。
8.1 代码贡献
欢迎社区贡献代码。我们不需要正式的《贡献者许可协议》(CLA),而是使用《开发者原产地证书》(Developer Certificate of Origin)来确保贡献者有权向本项目提交贡献。请确保所有提交都添加了签名,签名的电子邮件地址必须与提交作者的电子邮件地址一致,以同意 DCO 条款对每项特定贡献的规定。
DCO 全文如下:
Version 1.1
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.