Skip to content

Assets missing from MjSpec.to_zip #3104

@hartikainen

Description

@hartikainen

Intro

Hi!

My setup

Version: mujoco==3.5.0
API: Python
Architecture: macos_arm64

What's happening? What did you expect?

The MjSpec.to_zip documentation says that:

The MjSpec object can be serialized with all of its assets using the function spec.to_zip(file)...

I was expecting that I could serialize a model that references external assets (like meshes, textures, etc.) in the spec, e.g. loaded from XML via <assets> tag. However, MjSpec.to_zip only includes assets that are explicitly present in spec.assets dictionary. It does not iterate over the model elements (meshes, textures, skins, hfields) to check for file references and include those files. This makes the zip file unusable for most of my use cases.

def to_zip(spec: _specs.MjSpec, file: Union[str, IO[bytes]]) -> None:
"""Converts an MjSpec to a zip file.
Args:
spec: The mjSpec to save to a file.
file: The path to the file to save to or the file object to write to.
"""
files_to_zip = spec.assets
files_to_zip[spec.modelname + '.xml'] = spec.to_xml()
if isinstance(file, str):
directory = os.path.dirname(file)
os.makedirs(directory, exist_ok=True)
file = open(file, 'wb')
with zipfile.ZipFile(file, 'w') as zip_file:
for filename, contents in files_to_zip.items():
zip_info = zipfile.ZipInfo(filename)
zip_file.writestr(zip_info, contents)

Steps for reproduction

Load any model with "external" assets defined e.g. in the .xml <assets> tag. Dump the model with spec.to_zip(...) and try loading it.

Minimal model for reproduction

No response

Code required for reproduction

No response

Confirmations

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions