注意:该库并未积极维护/构建,如果您想做出贡献,请提出问题。
Simulate 是一个用于轻松创建和共享智能代理(例如强化学习)或合成数据生成的模拟环境的库。
使用简单的pip install simulate
安装 Simulate(优先在虚拟环境中)注意: vtk
不是为使用 Python 3.8 的 Apple Silicon 构建的。在这种情况下,请安装 >3.9。
创建虚拟环境,然后在本地安装代码样式/质量工具以及代码库
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。它允许人们快速渲染/探索场景,但不处理物理模拟。为了允许物理模拟,可以通过设置engine="unity"
来使用 Unity 后端(很快也可以设置 Godot 和 Blender Engines 后端)。 Unity 版本将自动下载(如果尚未)并生成以运行模拟。或者,您可以自行下载并使用 Unity 编辑器,然后必须使用 Unity 版本 2021.3.2f1 打开该编辑器。
使用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.PolyData
或pyvista.MultiBlock
对象)和材质和/或灯光、相机、代理特殊物体。
目前提供以下对象创建助手:
Object3D
任何具有网格和/或材质的对象Plane
Sphere
Capsule
Cylinder
Box
Cone
Line
MultipleLines
Tube
Polygon
Ring
Text3D
Triangle
Rectangle
Circle
StructuredGrid
其中许多对象可以通过运行以下示例来可视化:
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
的 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} }
}