1919import os
2020import platform
2121import subprocess
22- from typing import IO , Union
22+ from typing import Any , IO , Union , Sequence
2323from typing_extensions import TypeAlias
2424import warnings
2525import zipfile
5050 'native, arm64 build of Python.' )
5151
5252from mujoco import _specs
53+ from mujoco import _structs
5354from mujoco ._callbacks import *
5455from mujoco ._constants import *
5556from mujoco ._enums import *
8889 _specs .MjsPlugin ,
8990]
9091
92+
9193def to_zip (spec : _specs .MjSpec , file : Union [str , IO [bytes ]]) -> None :
9294 """Converts a spec to a zip file.
9395
@@ -106,7 +108,68 @@ def to_zip(spec: _specs.MjSpec, file: Union[str, IO[bytes]]) -> None:
106108 zip_info = zipfile .ZipInfo (os .path .join (spec .modelname , filename ))
107109 zip_file .writestr (zip_info , contents )
108110
111+
112+ class _MjBindModel :
113+ def __init__ (self , elements : Sequence [Any ]):
114+ self .elements = elements
115+
116+ def __getattr__ (self , key : str ):
117+ items = []
118+ for e in self .elements :
119+ items .extend (getattr (e , key ))
120+ return items
121+
122+
123+ class _MjBindData :
124+ def __init__ (self , elements : Sequence [Any ]):
125+ self .elements = elements
126+
127+ def __getattr__ (self , key : str ):
128+ items = []
129+ for e in self .elements :
130+ items .extend (getattr (e , key ))
131+ return items
132+
133+
134+ def _bind_model (
135+ model : _structs .MjModel , specs : Union [Sequence [MjStruct ], MjStruct ]
136+ ):
137+ """Bind a Mujoco spec to a mjModel.
138+
139+ Args:
140+ model: The mjModel to bind to.
141+ specs: The mjSpec elements to use for binding, can be a single element or a
142+ sequence.
143+ Returns:
144+ A MjModelGroupedViews object or a list of the same type.
145+ """
146+ if isinstance (specs , Sequence ):
147+ return _MjBindModel ([model .bind_scalar (s ) for s in specs ])
148+ else :
149+ return model .bind_scalar (specs )
150+
151+
152+ def _bind_data (
153+ data : _structs .MjData , specs : Union [Sequence [MjStruct ], MjStruct ]
154+ ):
155+ """Bind a Mujoco spec to a mjData.
156+
157+ Args:
158+ data: The mjData to bind to.
159+ specs: The mjSpec elements to use for binding, can be a single element or a
160+ sequence.
161+ Returns:
162+ A MjDataGroupedViews object or a list of the same type.
163+ """
164+ if isinstance (specs , Sequence ):
165+ return _MjBindData ([data .bind_scalar (s ) for s in specs ])
166+ else :
167+ return data .bind_scalar (specs )
168+
169+
109170_specs .MjSpec .to_zip = to_zip
171+ _structs .MjData .bind = _bind_data
172+ _structs .MjModel .bind = _bind_model
110173
111174HEADERS_DIR = os .path .join (os .path .dirname (__file__ ), 'include/mujoco' )
112175PLUGINS_DIR = os .path .join (os .path .dirname (__file__ ), 'plugin' )
0 commit comments