Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Dm control MJCF Attribute

From Leeroopedia
Knowledge Sources
Domains Robotics Simulation, Model Description
Last Updated 2026-02-15 04:00 GMT

Overview

The attribute module defines typed attribute classes that represent every kind of data an MJCF XML attribute can hold, forming the fundamental type system of the PyMJCF object model.

Description

An abstract _Attribute base class provides common logic for value assignment (with type validation), clearing, debug-mode stack trace tracking, conflict handling, and XML serialization via to_xml_string. Each concrete subclass implements the _assign method with type-specific validation and conversion:

  • String -- validates and stores string values
  • Integer -- validates and converts to int (rejecting non-integer floats)
  • Float -- validates and converts to float, with precision-controlled XML output
  • Keyword -- validates against a set of allowed values (case-insensitive)
  • Array -- stores numpy arrays with optional length constraints and dtype enforcement
  • Identifier -- manages unique name identifiers, registering them in the parent element's NameScope and enforcing constraints (no prefix separators, no "world" body name)
  • Reference -- holds either a string or a live mjcf.Element pointer to another element, resolving dead references lazily and handling default class scoping
  • BasePath -- manages base directory paths for asset types (meshdir, texturedir, assetdir)
  • File -- loads binary asset contents from the filesystem or a pre-loaded assets dictionary, wrapping them in Asset or SkinAsset objects

The BaseAsset, Asset, and SkinAsset classes represent binary assets whose VFS filenames include a SHA-1 content hash for deduplication within MuJoCo's virtual file system. SkinAsset additionally caches serialized skin data and invalidates it when the parent namescope revision changes.

Usage

These classes are used internally by the PyMJCF element system. Users typically interact with them indirectly when setting attributes on MJCF elements (e.g., body.pos = [1, 2, 3] creates/updates an Array attribute, geom.name = 'my_geom' creates/updates an Identifier attribute). Direct usage is uncommon outside of the MJCF library internals.

Code Reference

Source Location

Signature

class _Attribute(metaclass=abc.ABCMeta):
    def __init__(self, name, required, parent, value,
                 conflict_allowed, conflict_behavior): ...
    def _check_and_assign(self, new_value): ...
    def _assign(self, value): ...  # abstract
    def clear(self): ...
    def to_xml_string(self, prefix_root, **kwargs): ...

class String(_Attribute): ...
class Integer(_Attribute): ...
class Float(_Attribute): ...
class Keyword(_Attribute): ...
class Array(_Attribute): ...
class Identifier(_Attribute): ...
class Reference(_Attribute): ...
class BasePath(_Attribute): ...

class BaseAsset:
    def get_vfs_filename(self, filename_with_hash=True): ...

class Asset(BaseAsset):
    def __init__(self, contents, extension, prefix=''): ...

class SkinAsset(BaseAsset):
    def __init__(self, contents, parent, extension, prefix=''): ...

class File(_Attribute):
    def _assign(self, value): ...
    def get_contents(self): ...
    def to_xml_string(self, prefix_root=None, **kwargs): ...

Import

from dm_control.mjcf import attribute

I/O Contract

Inputs

Name Type Required Description
name str Yes The attribute name as it appears in the MJCF XML schema
required bool Yes Whether the attribute is required (cannot be cleared)
parent mjcf.Element Yes The parent MJCF element that owns this attribute
value varies No Initial value for the attribute; type depends on subclass
conflict_allowed bool Yes Whether conflicting values are allowed during model merging
conflict_behavior str Yes How to handle conflicts (e.g., replace, error)

Outputs

Name Type Description
value varies The typed attribute value (string, int, float, np.ndarray, etc.)
to_xml_string() str or None The XML-serialized string representation of the attribute value

Usage Examples

Basic Usage

# Attributes are typically created and managed by the MJCF element system.
# When you set a property on an MJCF element, the appropriate attribute
# class is used internally:

from dm_control import mjcf

model = mjcf.RootElement()
body = model.worldbody.add('body', name='torso', pos=[0, 0, 1])
# 'name' -> Identifier attribute
# 'pos' -> Array attribute with dtype=float and length=3

geom = body.add('geom', type='sphere', size=[0.1])
# 'type' -> Keyword attribute with valid_values including 'sphere'
# 'size' -> Array attribute

# File attributes handle asset loading:
model.asset.add('mesh', name='my_mesh', file='/path/to/mesh.stl')
# 'file' -> File attribute that loads the .stl contents

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment