openai / mujoco-py

MuJoCo is a physics engine for detailed, efficient rigid body simulations with contacts. mujoco-py allows using MuJoCo from Python 3.
Other
2.79k stars 810 forks source link

Cannot change site position programmatically when pos is set to zero in XML #793

Open bikcrum opened 4 months ago

bikcrum commented 4 months ago

Describe the bug I am specifying a <site> position in the XML file to zero in all x y z and when I try to change the site position programmatically the value doesn't change and is not reflected in the viewer. However, having a non-zero value for a position in XML allows changing it programmatically and is reflected in the viewer.

To Reproduce

  1. Create a Mujoco XML file. Download a file from the example here (https://github.com/google-deepmind/mujoco/blob/main/doc/_static/pendulum.xml) and add a site.

    
    <mujoco model="energy conserving pendulum">
    <option integrator="RK4">
    <flag constraint="disable" energy="enable"/>
    </option>
    
    <worldbody>
    <light pos="0 0 2"/>
    <geom pos="0 0 -.5" type="plane" size="1 1 .01"/>
    <body pos="0 0 0">
  <site name='my_site' size='0.05' pos='0 0 0' rgba='0.0 0.5 0.5 1.0'/>

  <joint type="hinge" axis="0 1 0"/>
  <geom type="cylinder" size="0.02" fromto="0 -.02 0 0 .02 0"/>
  <geom type="capsule" size="0.02" fromto="0 0 0 .1 0 0"/>
  <body pos="0.1 0 0">
    <joint type="slide" axis="1 0 0" stiffness="200"/>
    <geom type="capsule" size="0.015" fromto="-.1 0 0 .1 0 0"/>
    <body pos=".1 0 0">
      <joint type="ball"/>
      <geom type="box" size=".02" fromto="0 0 0 0 .1 0"/>
      <body pos="0 .1 0">
        <joint axis="1 0 0"/>
        <geom type="capsule" size="0.02" fromto="0 0 0 0 .1 0"/>
      </body>
    </body>
  </body>
</body>

2. Use following function to set and get the position of `site`

import mujoco as mj

model = mj.MjModel.from_xml_path(str("pendulum.xml")) data = mj.MjData(model)

model.site('my_site').pos[:2] = [0.5, 0.5]

mj.mj_forward(model, data)

print(data.site('my_site').xpos)



**Expected behavior**
The result expected here is `[0.5 0.5 0.]`. However, the result comes to be `[0. 0. 0.]`. 

**Workaround**
The current solution is to have a non-zero position in the XML. So,

Change `<site name='my_site' size='0.05' pos='0 0 0' rgba='0.0 0.5 0.5 1.0'/>` 
to 
`<site name='my_site' size='0.05' pos='0.1 0 0' rgba='0.0 0.5 0.5 1.0'/>`

**Desktop**
 - OS: macOS 14.1.2
 - Python Version 3.10.11
 - Mujoco Version 3.1.2
 - mujoco-py version 3.1.2