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

mujoco Tutorial code bug report #2531

Open
2 tasks done
KindChuankai opened this issue Mar 25, 2025 · 0 comments
Open
2 tasks done

mujoco Tutorial code bug report #2531

KindChuankai opened this issue Mar 25, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@KindChuankai
Copy link

Intro

Hi!

I am a graduate student at university of Leeds, I recently try to learn how to use mujoco.

My setup

All is up to date and normal.

What's happening? What did you expect?

In the section of 'Analysis of contact forces' and in the line of code,
ax[0,0].legend(iter(lines), ('normal z', 'friction x', 'friction y'));
the iter() function should not be used because ‘lines’ is already an iterable, and the len() method cannot be applied to it. It reports error like "TypeError: object of type 'list_iterator' has no len()".

Semicolon is also redundant.

The correct code is
ax[0,0].legend(lines, ('normal z', 'friction x', 'friction y'))

Steps for reproduction

  1. Load the model below and run the code below.

  2. Report error like:
    TypeError: object of type 'list_iterator' has no len()

Minimal model for reproduction

If you encountered the issue in a complex model, please simplify it as much as possible (while still reproducing the issue).

minimal XML
<mujoco>
  <worldbody>
    <light pos="0 0 1"/>
    <geom type="sphere" size="1" rgba="1 0 0 1"/>
  </worldbody>
</mujoco>

Code required for reproduction

import matplotlib.pyplot as plt

import mujoco
import numpy as np
import cv2
import time

def show_video(frames, fps=30, window_size=(3840, 2160)):
delay = int(1000 / fps)

cv2.namedWindow("Video", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Video", window_size[0], window_size[1])

for frame in frames:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
frame = cv2.resize(frame, window_size)
cv2.imshow("Video", frame)
if cv2.waitKey(delay) & 0xFF == ord('q'):
break

cv2.destroyAllWindows()

free_body_MJCF = """




"""

model = mujoco.MjModel.from_xml_string(free_body_MJCF)
data = mujoco.MjData(model)

n_steps = 499

allocate

sim_time = np.zeros(n_steps)
ncon = np.zeros(n_steps)
force = np.zeros((n_steps,3))
velocity = np.zeros((n_steps, model.nv))
penetration = np.zeros(n_steps)
acceleration = np.zeros((n_steps, model.nv))
forcetorque = np.zeros(6)

random initial rotational velocity:

mujoco.mj_resetData(model, data)
data.qvel[3:6] = 2*np.random.randn(3)

simulate and save data

for i in range(n_steps):
mujoco.mj_step(model, data)
sim_time[i] = data.time
ncon[i] = data.ncon
velocity[i] = data.qvel[:]
acceleration[i] = data.qacc[:]

iterate over active contacts, save force and distance

for j,c in enumerate(data.contact):
mujoco.mj_contactForce(model, data, j, forcetorque)
force[i] += forcetorque[0:3]
penetration[i] = min(penetration[i], c.dist)

we could also do

force[i] += data.qfrc_constraint[0:3]

do you see why?

plot

_, ax = plt.subplots(3, 2, sharex=True, figsize=(10, 10))

lines = ax[0,0].plot(sim_time, force)
ax[0,0].set_title('contact force')
ax[0,0].set_ylabel('Newton')
ax[0,0].legend(iter(lines), ('normal z', 'friction x', 'friction y'));

ax[0,0].legend(list(iter(lines)), ('normal z', 'friction x', 'friction y'))

ax[1,0].plot(sim_time, acceleration)
ax[1,0].set_title('acceleration')
ax[1,0].set_ylabel('(meter,radian)/s/s')

ax[2,0].plot(sim_time, velocity)
ax[2,0].set_title('velocity')
ax[2,0].set_ylabel('(meter,radian)/s')
ax[2,0].set_xlabel('second')

ax[0,1].plot(sim_time, ncon)
ax[0,1].set_title('number of contacts')
ax[0,1].set_yticks(range(6))

ax[1,1].plot(sim_time, force[:,0])
ax[1,1].set_yscale('log')
ax[1,1].set_title('normal (z) force - log scale')
ax[1,1].set_ylabel('Newton')
z_gravity = -model.opt.gravity[2]
mg = model.body("box_and_sphere").mass[0] * z_gravity
mg_line = ax[1,1].plot(sim_time, np.ones(n_steps)mg, label='mg', linewidth=1)
ax[1,1].legend()

ax[2,1].plot(sim_time, 1000*penetration)
ax[2,1].set_title('penetration depth')
ax[2,1].set_ylabel('millimeter')
ax[2,1].set_xlabel('second')

plt.tight_layout()
plt.show()

Confirmations

@KindChuankai KindChuankai added the bug Something isn't working label Mar 25, 2025
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

1 participant