跳转到主要内容

HuggingFace社区驱动的模拟环境开源库

项目描述



GitHub GitHub release Contributor Covenant

模拟

模拟是一个库,可以轻松创建和共享智能代理(例如强化学习)或合成数据生成的模拟环境。

安装

使用简单的pip install simulate安装Simulate(优先在虚拟环境中安装)注意vtk没有为Python 3.8的Apple Silicon构建。在这种情况下,请使用>3.9安装。

为贡献安装(从CONTRIBUTING.md

创建一个虚拟环境,然后在本地上安装代码风格/质量工具以及代码库

pip install --upgrade simulate

在合并PR之前,修复样式(我们使用isort + black

make style

快速浏览

Simulate的API受到优秀的Kubric的API的启发。用户创建一个Scene并在其中添加Assets(对象、摄像机、灯光等)。

一旦创建了场景,您就可以将其保存并作为文件共享。这是一个gIFT文件,即带有相关资源的JSON文件。

您还可以使用后端渲染/模拟引擎(目前是Unity、Blender和Godot)之一渲染场景或进行模拟。

保存/共享格式与引擎无关,并使用图形行业标准。

让我们一起来快速探索。

import simulate as sm

scene = sm.Scene()

项目结构

Python API位于src/simulate中。它允许创建和加载场景,并向后端发送命令。

我们提供几个后端来渲染和/或运行场景。默认后端不需要特定安装,基于pyvista。它允许快速渲染/探索场景,但不处理物理模拟。要允许物理模拟,可以使用Unity后端,例如通过设置engine="unity"(不久还将提供Godot和Blender引擎后端)。将自动下载Unity构建(如果尚未下载)并启动以运行模拟。或者,您可以下载并使用Unity编辑器,然后必须使用Unity版本2021.3.2f1打开。

从Hub或本地文件加载场景

使用Scene.create_from()从本地文件或Hub加载场景,使用scene.save()scene.push_to_hub()本地保存或推送到Hub

from simulate import Scene

scene = Scene.create_from('tests/test_assets/fixtures/Box.gltf')  # either local (priority) or on the Hub with full path to file
scene = Scene.create_from('simulate-tests/Box/glTF/Box.gltf', is_local=False)  # Set priority to the Hub file

scene.save('local_dir/file.gltf')  # Save to a local file
scene.push_to_hub('simulate-tests/Debug/glTF/Box.gltf')  # Save to the Hub - use a token if necessary

scene.show()



创建场景并添加/管理场景中的对象

创建带有上方平面的球体的场景的基本示例

import simulate as sm

scene = sm.Scene()
scene += sm.Plane() + sm.Sphere(position=[0, 1, 0], radius=0.2)

>>> scene
>>> Scene(dimensionality=3, engine='PyVistaEngine')
>>> └── plane_01 (Plane - Mesh: 121 points, 100 cells)
>>>     └── sphere_02 (Sphere - Mesh: 842 points, 870 cells)

scene.show()

对象(以及场景)只是一个树中的节点,提供了可选网格(在底层创建/存储/编辑为pyvista.PolyDatapyvista.MultiBlock对象)以及材质和/或灯光、摄像机、代理等特殊对象。

目前提供了以下对象创建辅助工具

  • Object3D任何具有网格和/或材质的对象
  • 平面
  • 球体
  • 胶囊
  • 圆柱体
  • 盒子
  • 圆锥体
  • 线条
  • 多线条
  • 管子
  • 多边形
  • 环形
  • 三维文本
  • 三角形
  • 矩形
  • 结构化网格
  • ...(见文档)

许多这些对象可以通过运行以下示例进行可视化

python examples/basic/objects.py



对象按树状结构组织

添加/删除对象

  • 使用加号(+)运算符(或备选方法.add(object))将对象添加为先前对象的子对象。
  • 可以使用减号(-)运算符或.remove(object)命令删除对象。
  • 可以一次添加多个对象,通过将列表/元组添加到场景中。
  • 可以使用.clear()清除整个场景。
  • 要添加嵌套对象,只需将其添加到应嵌套的对象下,例如:scene.sphere += sphere_child

访问对象

  • 对象可以作为其父对象的属性直接访问,使用其名称(在创建时给定name属性或从类名加创建计数器自动生成)。
  • 还可以使用.get_node(name)通过名称访问对象。
  • 对象的名称在保存/显示时必须唯一。
  • 任何节点都提供各种tree_*属性,以便快速导航或列出节点树的一部分。

以下是一些操作示例

# Add two copy of the sphere to the scene as children of the root node (using list will add all objects on the same level)
# Using `.copy()` will create a copy of an object (the copy doesn't have any parent or children)
scene += [scene.plane_01.sphere_02.copy(), scene.plane_01.sphere_02.copy()]

>>> scene
>>> Scene(dimensionality=3, engine='pyvista')
>>> ├── plane_01 (Plane - Mesh: 121 points, 100 cells)
>>> │   └── sphere_02 (Sphere - Mesh: 842 points, 870 cells)
>>> ├── sphere_03 (Sphere - Mesh: 842 points, 870 cells)
>>> └── sphere_04 (Sphere - Mesh: 842 points, 870 cells)

# Remove the last added sphere
>>> scene.remove(scene.sphere_04)
>>> Scene(dimensionality=3, engine='pyvista')
>>> ├── plane_01 (Plane - Mesh: 121 points, 100 cells)
>>> │   └── sphere_02 (Sphere - Mesh: 842 points, 870 cells)
>>> └── sphere_03 (Sphere - Mesh: 842 points, 870 cells)

编辑和移动对象

对象可以轻松地进行平移、旋转和缩放

以下是一些示例

# Let's translate our floor (with the first sphere, it's child)
scene.plane_01.translate_x(1)

# Let's scale the second sphere uniformly
scene.sphere_03.scale(0.1)

# Inspect the current position and scaling values
print(scene.plane_01.position)
>>> array([1., 0., 0.])
print(scene.sphere_03.scaling)
>>> array([0.1, 0.1, 0.1])

# We can also translate from a vector and rotate from a quaternion or along the various axis

编辑对象

  • 对象网格可以使用pyvista提供的所有操作符进行编辑

可视化引擎

默认可视化引擎由pyvista的vtk后端提供。

只需使用.show()即可启动可视化引擎。

scene.show()

您可以在integrations目录中找到与其他渲染/仿真引擎的桥梁。

提示

如果您在GCP上运行,请记住不要安装pyvistaqt,如果您已经安装了,请从您的环境中卸载它,因为QT在GCP上工作不理想。

引用

@misc{simulate,
  author = {Thomas Wolf, Edward Beeching, Carl Cochet, Dylan Ebert, Alicia Machado, Nathan Lambert, Clément Romac},
  title = {Simulate},
  year = {2022},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/huggingface/simulate}}
}

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

simulate-0.1.2.tar.gz (138.8 kB 查看哈希值)

上传时间

构建分布

simulate-0.1.2-cp310-cp310-macosx_12_0_arm64.whl (543.5 kB 查看哈希值)

上传时间 CPython 3.10 macOS 12.0+ ARM64

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面