Store asset keys relative to the MJCF. This CL fixes cases when loading a spec with "from_zip" that fails on zipped folders that contain a root directory.

PiperOrigin-RevId: 825484553
Change-Id: I79c6f542edbd3ffd167d0b5cecc704c32c8d6824
This commit is contained in:
Google DeepMind
2025-10-29 04:23:18 -07:00
committed by Copybara-Service
parent 010d919244
commit d769440924
5 changed files with 31 additions and 0 deletions
+10
View File
@@ -124,15 +124,25 @@ def from_zip(file: Union[str, IO[bytes]]) -> _specs.MjSpec:
if not zipfile.is_zipfile(file):
raise ValueError(f'File {file} is not a zip file.')
with zipfile.ZipFile(file, 'r') as zip_file:
xml_dir = None
for zip_info in zip_file.infolist():
if not zip_info.filename.endswith(os.path.sep):
with zip_file.open(zip_info.filename) as f:
if zip_info.filename.endswith('.xml'):
xml_string = f.read()
xml_dir = os.path.dirname(zip_info.filename)
else:
assets[zip_info.filename] = f.read()
if not xml_string:
raise ValueError('No XML file found in zip file.')
relative_assets = {}
for key, value in assets.items():
new_key = os.path.relpath(key, xml_dir)
relative_assets[new_key] = value
assets = relative_assets
return _specs.MjSpec.from_string(xml_string, assets=assets)