Skip to content

Commit

Permalink
Fix temporary file handling in RodModelToMjcf to avoid file locks o…
Browse files Browse the repository at this point in the history
…n Windows
  • Loading branch information
flferretti committed Feb 26, 2025
1 parent eab4885 commit bbc5792
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/jaxsim/mujoco/loaders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pathlib
import tempfile
import warnings
Expand Down Expand Up @@ -355,17 +356,22 @@ def convert(
msg = "The Mujoco model has the following extra/missing joints: '{}'"
raise ValueError(msg.format(extra_joints))

# Windows locks open files, so we use mkstemp() to create a temporary file without keeping it open.
with tempfile.NamedTemporaryFile(
mode="w+", suffix=".xml", prefix=f"{rod_model.name}_"
) as mjcf_file:
suffix=".xml", prefix=f"{rod_model.name}_", delete=False
) as tmp:
temp_filename = tmp.name

try:
# Convert the in-memory Mujoco model to MJCF.
mj.mj_saveLastXML(mjcf_file.name, mj_model)
mj.mj_saveLastXML(temp_filename, mj_model)

# Parse the MJCF string as XML (etree).
# We need to post-process the file to include additional elements.
# Parse the MJCF file as XML.
parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse(source=mjcf_file, parser=parser)
tree = ET.parse(source=temp_filename, parser=parser)

finally:
os.remove(temp_filename)

# Get the root element.
root: ET._Element = tree.getroot()
Expand Down

0 comments on commit bbc5792

Please sign in to comment.