🏗️ Model#

In MotrixSim, the model SceneModel and the data SceneData are essential components for building a simulation environment and are used throughout the entire physics simulation process. This chapter mainly introduces the creation and usage of SceneModel, while SceneData will be described in detail in the next chapter.

Basic Concepts#

SceneModel describes the model, i.e., all time-invariant quantities. This includes geometric shapes, mass properties, joint connections, actuator configurations, and other static information. During the entire simulation, SceneModel remains unchanged, while all time-varying dynamic states (positions, velocities, forces, etc.) are stored in SceneData.

SceneModel mainly contains the following:

Category

Description

Components

Joints Joint, Bodies Body, Links Link, Geomtries Geometry, Sensors Sensor, Actuators Actuator, Sites Site, etc.

Simulation Parameters

Options (including timestep, gravity, etc.)

Creating a Model#

Loading from File#

The most common way is to create a model from an MJCF or URDF file:

# The scene description file
path = "examples/assets/empty.xml"
# Load the scene model
model = load_model(path)

For the complete example, see examples/getting_started/empty.py.

Loading from String#

You can also create a model directly from an MJCF string:

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
    with RenderApp() as render:
        # Load the scene model
        model = load_mjcf_str(mjcf)

For the complete example, see examples/getting_started/load_from_str.py.

Component Access (Named Access)#

After creating a model, you often need to access its components to set parameters, retrieve information, or perform control. MotrixSim provides convenient named access interfaces for model components, supporting direct access to various components by name or index.

Below is an example of accessing a joint by name and index. For the complete example, see examples/physics/joint.py.

Basic Access Methods#

  • Access by name

 hinge = model.get_joint("hinge")
  • Access by index (an interface for name-to-index conversion is also provided)

# 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}")

Batch Access#

In addition to accessing individual components, you can also retrieve lists of component objects or names in batch:

# ----------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}")

The access methods support the model components mentioned in Basic Concepts. For detailed access methods, see: API Quick Reference - Named Access.

API Reference#

For more APIs related to SceneModel, see SceneModel API