🏗️ 模型(SceneModel)#
MotrixSim 中的模型 SceneModel 和数据 SceneData 是构建仿真环境的必要组成对象,贯穿整个物理仿真过程。本章节主要介绍 SceneModel 的创建和使用方法,而 SceneData 将在下一章节详细描述。
基本概念#
SceneModel 是对模型的描述,即所有不随时间变化的量。这包括几何形状、质量属性、关节连接关系、执行器配置等静态信息。在整个仿真过程中,SceneModel 保持不变,而所有随时间变化的动态状态(位置、速度、力等)都存储在 SceneData 中。
SceneModel 主要包含以下内容:
创建模型#
从文件加载#
最常用的方式是从 MJCF 或 URDF 文件创建模型:
# The scene description file
path = "examples/assets/empty.xml"
# Load the scene model
model = load_model(path)
完整示例代码参见 examples/empty.py。
从字符串加载#
也可以直接从 MJCF 字符串创建模型:
mjcf = """<mujoco>
<option timestep="0.001"/>
<worldbody>
<light name="light" pos="0 0 3"/>
<geom name="floor" size="0 0 0.05" type="plane"/>
<body pos="0 0 0.1">
<freejoint/>
<geom type="sphere" size=".1" />
</body>
<body pos="0.01 0 0.3">
<freejoint/>
<geom type="sphere" size=".1" />
</body>
</worldbody>
</mujoco>"""
def main():
# Create render window for visualization
render = RenderApp()
# Load the scene model
model = load_mjcf_str(mjcf)
完整示例代码参见 examples/load_from_str.py。
组件访问 (Named Access)#
创建模型后,我们通常需要访问模型中的组件来设置参数、获取信息或进行控制。MotrixSim 为模型组件提供了便捷的命名访问接口,支持通过名称或索引直接访问各种组件。
下面通过 joint 的访问示例,展示如何通过名称和索引来进行访问。完整示例代码参见 examples/joint.py。
基本访问方式#
通过名称访问
hinge = model.get_joint("hinge")
通过索引访问 (也提供了从名称到索引的接口)
# Try to visit "joint_A"
joint_A_index = model.get_joint_index("joint_A")
joint_A = model.get_joint(joint_A_index)
assert joint_A is not None, "Expect joint_A in the model"
print(f"joint_addr is {joint_A_index}, joint_A is : {joint_A}, name is : {joint_A.name}")
批量访问#
除了单个组件访问,还可以批量获取组件对象或名称列表:
# ----------Try to access joint data----------
# How many joints in the model?
num_joints = model.num_joints
# Get the all joints in the model
joints = model.joints
# The name list of joints in the model
joint_names = model.joint_names
print(f"num_joints :{num_joints}, joint_names : {joint_names}, joints : {joints}")
访问方式支持 基本概念中提到的模型组件。详细的访问方法,请参阅:API 快速参考 - Named Access。
API Reference#
更多与 SceneModel 相关的 API,请参考 SceneModel API