Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MjSpec python bindings #2353

Open
2 tasks done
milutter opened this issue Jan 13, 2025 · 5 comments
Open
2 tasks done

MjSpec python bindings #2353

milutter opened this issue Jan 13, 2025 · 5 comments
Labels
bug Something isn't working

Comments

@milutter
Copy link
Contributor

milutter commented Jan 13, 2025

My setup

MuJoCo==3.2.6, Python, Ubuntu 22.04

What's happening? What did you expect?

I started playing around with MjSpec and noticed some problems in the API. I commonly use the manual asset dict functionality to create a MjSpec or MjModel. For example mj_spec = MjSpec.from_file(filename, assets=assets) and mj_spec.compile(assets).

There seems to be some issue when doing mj_spec.compile(assets=assets), even though it looks like assets is a valid keyword. While doing mj_spec.compile(assets) seems to work just fine, providing the assets keyword the program dies and a lot of binary code is printed to the console.

When the assets are passed as positional argument, passing None via mj_spec.compile(None) does not work. Historically the assets argument has been dict[str, byte] | None but this does not seem to be true for MjSpec.

When previously working with mjcf, I could get the assets mjcf_root.get_assets(). MjSpec does not seem to provide this option. Would it be possible to add this feature?

Lastly, I am having problems with mujoco.MjSpec in a docker container. For example

import mujoco

xml = """
 <mujoco>
   <worldbody>
     <geom name="red_box" type="box" size=".2 .2 .2" rgba="1 0 0 1"/>
     <geom name="green_sphere" pos=".2 .2 .2" size=".1" rgba="0 1 0 1"/>
   </worldbody>
</mujoco>
"""

spec = mujoco.MjSpec.from_string(xml)
print(spec.geoms[0].name)

yields the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 1: invalid continuation byte. This error seems to be specific to my Docker setup as I do not observe the issues when running outside docker. Do you have any idea what could be causing this difference?

CC: @yuvaltassa @quagla

Confirmations

@milutter milutter added the bug Something isn't working label Jan 13, 2025
@quagla
Copy link
Contributor

quagla commented Jan 13, 2025

I have just submitted ba183ad which adds the attribute assets so you can do

specs.assets = asset_dict
model = spec.compile()

I have removed any keywords from compile so you also don't have the other issues and there is no ambiguity concerning the assets used for compilation vs those that belong to the spec. If you want to retrieve the assets associated to the spec, you can do spec.assets, so for example after attaching a spec you can do

parent.assets.update(child.assets)

In order to add the child assets to the parent assets. I am not sure about the Docker issue. Maybe you can check if you still have this issue now?

@yuvaltassa
Copy link
Collaborator

@quagla should we add a line about this here?
https://mujoco.readthedocs.io/en/latest/python.html#construction

@milutter
Copy link
Contributor Author

Thanks for fixing the interface so fast. I can confirm this issue is resolved in 3.2.7.

+1 to @yuvaltassa point of adding it to the documentation. I think it is quite undocumented that one can pass the meshes explicitly which I prefer as this enables storing the meshes and models at different locations.

Don't worry about the encoding issue. I narrowed it down to how we build the mujoco python package in the docker.

@yuvaltassa
Copy link
Collaborator

yuvaltassa commented Jan 21, 2025

@milutter do you want to update here and send a PR?

Construction

@milutter
Copy link
Contributor Author

Sure I can do that later this week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants