|
19 | 19 | import os |
20 | 20 | import platform |
21 | 21 | import subprocess |
| 22 | +from typing import Union, IO |
22 | 23 | import warnings |
| 24 | +import zipfile |
23 | 25 |
|
24 | 26 | # Extend the path to enable multiple directories to contribute to the same |
25 | 27 | # package. Without this line, the `mujoco-mjx` package would not be able to |
|
53 | 55 | from mujoco._functions import * |
54 | 56 | from mujoco._render import * |
55 | 57 | from mujoco._specs import * |
| 58 | +from mujoco._specs import MjSpec |
56 | 59 | from mujoco._structs import * |
57 | 60 | from mujoco.gl_context import * |
58 | 61 | from mujoco.renderer import Renderer |
59 | 62 |
|
| 63 | + |
| 64 | +def to_zip(spec: MjSpec, file: Union[str, IO[bytes]]) -> None: |
| 65 | + """Converts a spec to a zip file. |
| 66 | +
|
| 67 | + Args: |
| 68 | + spec: The mjSpec to save to a file. |
| 69 | + file: The path to the file to save to or the file object to write to. |
| 70 | + """ |
| 71 | + files_to_zip = spec.assets |
| 72 | + files_to_zip[spec.modelname + '.xml'] = spec.to_xml() |
| 73 | + if isinstance(file, str): |
| 74 | + directory = os.path.dirname(file) |
| 75 | + os.makedirs(directory, exist_ok=True) |
| 76 | + file = open(file, 'wb') |
| 77 | + with zipfile.ZipFile(file, 'w') as zip_file: |
| 78 | + for filename, contents in files_to_zip.items(): |
| 79 | + zip_info = zipfile.ZipInfo(os.path.join(spec.modelname, filename)) |
| 80 | + zip_file.writestr(zip_info, contents) |
| 81 | + |
| 82 | +MjSpec.to_zip = to_zip |
| 83 | + |
60 | 84 | HEADERS_DIR = os.path.join(os.path.dirname(__file__), 'include/mujoco') |
61 | 85 | PLUGINS_DIR = os.path.join(os.path.dirname(__file__), 'plugin') |
62 | 86 |
|
|
0 commit comments