Skip to content

Commit 4cb3465

Browse files
authored
Merge branch 'google-deepmind:main' into musculoskeletal_dog_creation
2 parents b6d930c + 04ff055 commit 4cb3465

10 files changed

Lines changed: 332 additions & 278 deletions

File tree

dm_control/mjcf/export_with_assets_as_zip.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def export_with_assets_as_zip(mjcf_model, out_dir, model_name=None,
4343
quantities.
4444
zero_threshold: (optional) When outputting XML, floating point quantities
4545
whose absolute value falls below this threshold will be treated as zero.
46+
47+
Returns:
48+
The full path to the .zip file.
4649
"""
4750

4851
if model_name is None:
@@ -57,6 +60,8 @@ def export_with_assets_as_zip(mjcf_model, out_dir, model_name=None,
5760

5861
if not os.path.exists(out_dir):
5962
os.makedirs(out_dir)
60-
with zipfile.ZipFile(os.path.join(out_dir, zip_name), 'w') as zip_file:
63+
file_name = os.path.join(out_dir, zip_name)
64+
with zipfile.ZipFile(file_name, 'w') as zip_file:
6165
for filename, contents in files_to_zip.items():
6266
zip_file.writestr(os.path.join(model_name, filename), contents)
67+
return file_name

dm_control/mjcf/export_with_assets_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
_ASSETS_DIR = os.path.join(os.path.dirname(__file__), 'test_assets')
2929
_TEST_MODEL_WITH_ASSETS = os.path.join(_ASSETS_DIR, 'model_with_assets.xml')
3030
_TEST_MODEL_WITH_ASSETDIR = os.path.join(_ASSETS_DIR, 'model_with_assetdir.xml')
31+
_TEST_MODEL_WITH_SEPARATORS = os.path.join(_ASSETS_DIR,
32+
'model_with_separators.xml')
3133
_TEST_MODEL_WITHOUT_ASSETS = os.path.join(_ASSETS_DIR, 'lego_brick.xml')
3234

3335

@@ -89,6 +91,15 @@ def test_default_model_filename(self):
8991
expected_name = mjcf_model.model + '.xml'
9092
self.assertTrue(os.path.isfile(os.path.join(out_dir, expected_name)))
9193

94+
def test_model_with_separators(self):
95+
out_dir = self.create_tempdir().full_path
96+
mjcf_model = mjcf.from_path(
97+
_TEST_MODEL_WITH_SEPARATORS, escape_separators=True
98+
)
99+
mjcf.export_with_assets(mjcf_model, out_dir, out_file_name=None)
100+
expected_name = mjcf_model.model + '.xml'
101+
self.assertTrue(os.path.isfile(os.path.join(out_dir, expected_name)))
102+
92103
def test_exceptions(self):
93104
out_dir = self.create_tempdir().full_path
94105
mjcf_model = mjcf.from_path(_TEST_MODEL_WITH_ASSETS)

dm_control/mjcf/parser.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,19 @@ def _parse_children(xml_element, mjcf_element, escape_separators=False):
199199
if escape_separators:
200200
attributes = {}
201201
for name, value in xml_child.attrib.items():
202-
new_value = value.replace(
203-
constants.PREFIX_SEPARATOR_ESCAPE,
204-
constants.PREFIX_SEPARATOR_ESCAPE * 2)
205-
new_value = new_value.replace(
206-
constants.PREFIX_SEPARATOR, constants.PREFIX_SEPARATOR_ESCAPE)
207-
attributes[name] = new_value
202+
# skip flipping the slash for fields that may contain paths, like
203+
# custom text and asset file.
204+
if name in ['data', 'file', 'meshdir', 'assetdir', 'texturedir',
205+
'content_type', 'fileleft', 'fileright', 'fileback',
206+
'filefront', 'plugin', 'key', 'value']:
207+
attributes[name] = value
208+
else:
209+
new_value = value.replace(
210+
constants.PREFIX_SEPARATOR_ESCAPE,
211+
constants.PREFIX_SEPARATOR_ESCAPE * 2)
212+
new_value = new_value.replace(
213+
constants.PREFIX_SEPARATOR, constants.PREFIX_SEPARATOR_ESCAPE)
214+
attributes[name] = new_value
208215
else:
209216
attributes = dict(xml_child.attrib)
210217
if child_spec.repeated or child_spec.on_demand:

dm_control/mjcf/schema.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ def _parse_attribute(attribute_xml):
160160
else:
161161
try:
162162
attribute_callable = _SCALAR_TYPE_MAP[attribute_type]
163-
except KeyError:
164-
raise ValueError('Invalid attribute type: {}'.format(attribute_type))
163+
except KeyError as exc:
164+
raise ValueError(
165+
'Invalid attribute type: {}'.format(attribute_type)
166+
) from exc
165167

166168
return AttributeSpec(
167169
name=name, type=attribute_callable, required=required,
@@ -215,7 +217,7 @@ def _attachment_frame_spec(is_world_attachment):
215217
body_spec = MUJOCO.children['worldbody'].children['body']
216218
# 'name' and 'childclass' attributes are excluded.
217219
for attrib_name in (
218-
'mocap', 'pos', 'quat', 'axisangle', 'xyaxes', 'zaxis', 'euler'):
220+
'mocap', 'pos', 'quat', 'axisangle', 'xyaxes', 'zaxis', 'euler', 'user'):
219221
frame_spec.attributes[attrib_name] = copy.deepcopy(
220222
body_spec.attributes[attrib_name])
221223

@@ -258,4 +260,3 @@ def override_schema(schema_xml_path):
258260
MUJOCO = parse_schema(schema_xml_path)
259261
FINDABLE_NAMESPACES = frozenset(
260262
collect_namespaces(MUJOCO).union(_ADDITIONAL_FINDABLE_NAMESPACES))
261-

dm_control/mjcf/schema.xml

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<attribute name="actuation" type="keyword" valid_values="disable enable"/>
8888
<attribute name="refsafe" type="keyword" valid_values="disable enable"/>
8989
<attribute name="sensor" type="keyword" valid_values="disable enable"/>
90+
<attribute name="eulerdamp" type="keyword" valid_values="disable enable"/>
9091
<attribute name="override" type="keyword" valid_values="disable enable"/>
9192
<attribute name="energy" type="keyword" valid_values="disable enable"/>
9293
<attribute name="fwdinv" type="keyword" valid_values="disable enable"/>
@@ -266,6 +267,7 @@
266267
<attribute name="damping" type="float"/>
267268
<attribute name="frictionloss" type="float"/>
268269
<attribute name="user" type="array" array_type="float"/>
270+
<attribute name="actuatorgravcomp" type="keyword" valid_values="false true"/>
269271
</attributes>
270272
</element>
271273
<element name="geom">
@@ -442,6 +444,7 @@
442444
<attribute name="user" type="array" array_type="float"/>
443445
<attribute name="kp" type="float"/>
444446
<attribute name="kv" type="float"/>
447+
<attribute name="timeconst" type="float"/>
445448
</attributes>
446449
</element>
447450
<element name="velocity">
@@ -562,7 +565,7 @@
562565
<attribute name="axis" type="array" array_type="float" array_size="3"/>
563566
<attribute name="springdamper" type="array" array_type="float" array_size="2"/>
564567
<attribute name="limited" type="keyword" valid_values="false true"/>
565-
<attribute name="actuatorfrclimited" type="keyword" valid_values="false true"/>
568+
<attribute name="actuatorfrclimited" type="keyword" valid_values="false true auto"/>
566569
<attribute name="solreflimit" type="array" array_type="float" array_size="2"/>
567570
<attribute name="solimplimit" type="array" array_type="float" array_size="5"/>
568571
<attribute name="solreffriction" type="array" array_type="float" array_size="2"/>
@@ -577,6 +580,7 @@
577580
<attribute name="damping" type="float"/>
578581
<attribute name="frictionloss" type="float"/>
579582
<attribute name="user" type="array" array_type="float"/>
583+
<attribute name="actuatorgravcomp" type="keyword" valid_values="false true"/>
580584
</attributes>
581585
</element>
582586
<element name="geom">
@@ -747,6 +751,7 @@
747751
<attribute name="user" type="array" array_type="float"/>
748752
<attribute name="kp" type="float"/>
749753
<attribute name="kv" type="float"/>
754+
<attribute name="timeconst" type="float"/>
750755
</attributes>
751756
</element>
752757
<element name="velocity">
@@ -1100,25 +1105,26 @@
11001105
<attribute name="name" type="identifier"/>
11011106
<attribute name="type" type="keyword" valid_values="grid box cylinder ellipsoid mesh gmsh direct"/>
11021107
<attribute name="count" type="array" array_type="int" array_size="3"/>
1103-
<attribute name="spacing" type="array" array_type="int" array_size="3"/>
1104-
<attribute name="scale" type="array" array_type="int" array_size="3"/>
1108+
<attribute name="spacing" type="array" array_type="float" array_size="3"/>
1109+
<attribute name="scale" type="array" array_type="float" array_size="3"/>
11051110
<attribute name="radius" type="float"/>
1106-
<attribute name="rigid" type="int"/>
1111+
<attribute name="rigid" type="string" valid_values="true false"/>
11071112
<attribute name="mass" type="float"/>
11081113
<attribute name="inertiabox" type="float"/>
11091114
<attribute name="file" type="string"/>
11101115
<attribute name="point" type="string"/>
11111116
<attribute name="element" type="string"/>
11121117
<attribute name="texcoord" type="string"/>
11131118
<attribute name="material" type="string"/>
1114-
<attribute name="rgba" type="array" array_type="float" array_size="2"/>
1119+
<attribute name="rgba" type="array" array_type="float" array_size="4"/>
11151120
<attribute name="flatskin" type="int"/>
1116-
<attribute name="pos" type="array" array_type="int" array_size="3"/>
1117-
<attribute name="quat" type="array" array_type="int" array_size="4"/>
1121+
<attribute name="pos" type="array" array_type="float" array_size="3"/>
1122+
<attribute name="quat" type="array" array_type="float" array_size="4"/>
11181123
<attribute name="axisangle" type="array" array_type="float" array_size="4"/>
11191124
<attribute name="xyaxis" type="array" array_type="float" array_size="6"/>
11201125
<attribute name="zaxis" type="array" array_type="float" array_size="3"/>
11211126
<attribute name="euler" type="array" array_type="float" array_size="3"/>
1127+
<attribute name="dim" type="int" required="true"/>
11221128
</attributes>
11231129
<children>
11241130
<element name="plugin" repeated="true" namespace="flexcomp">
@@ -1138,7 +1144,7 @@
11381144
</element>
11391145
<element name="edge" repeated="true">
11401146
<attributes>
1141-
<attribute name="equality" type="int"/>
1147+
<attribute name="equality" type="string" valid_values="true false"/>
11421148
<attribute name="solref" type="array" array_type="float" array_size="2"/>
11431149
<attribute name="solimp" type="array" array_type="float" array_size="5"/>
11441150
<attribute name="stiffness" type="float"/>
@@ -1157,8 +1163,8 @@
11571163
<attribute name="solimp" type="array" array_type="float" array_size="5"/>
11581164
<attribute name="margin" type="float"/>
11591165
<attribute name="gap" type="float"/>
1160-
<attribute name="internal" type="int"/>
1161-
<attribute name="selfcollide" type="int"/>
1166+
<attribute name="internal" type="string" valid_values="true false"/>
1167+
<attribute name="selfcollide" type="string" valid_values="none narrow bvh sap auto"/>
11621168
<attribute name="activelayers" type="int"/>
11631169
</attributes>
11641170
</element>
@@ -1304,6 +1310,7 @@
13041310
<attribute name="zaxis" type="array" array_type="float" array_size="3"/>
13051311
<attribute name="euler" type="array" array_type="float" array_size="3"/>
13061312
<attribute name="gravcomp" type="float"/>
1313+
<attribute name="user" type="array" array_type="float"/>
13071314
</attributes>
13081315
<children>
13091316
<element name="plugin" repeated="true" namespace="body">
@@ -1348,6 +1355,8 @@
13481355
<attribute name="solimplimit" type="array" array_type="float" array_size="5"/>
13491356
<attribute name="solreffriction" type="array" array_type="float" array_size="2"/>
13501357
<attribute name="solimpfriction" type="array" array_type="float" array_size="5"/>
1358+
<attribute name="actuatorfrclimited" type="keyword" valid_values="false true auto"/>
1359+
<attribute name="actuatorfrcrange" type="array" array_type="float" array_size="2"/>
13511360
<attribute name="stiffness" type="float"/>
13521361
<attribute name="range" type="array" array_type="float" array_size="2"/>
13531362
<attribute name="margin" type="float"/>
@@ -1485,25 +1494,26 @@
14851494
<attribute name="name" type="identifier"/>
14861495
<attribute name="type" type="keyword" valid_values="grid box cylinder ellipsoid mesh gmsh direct"/>
14871496
<attribute name="count" type="array" array_type="int" array_size="3"/>
1488-
<attribute name="spacing" type="array" array_type="int" array_size="3"/>
1489-
<attribute name="scale" type="array" array_type="int" array_size="3"/>
1497+
<attribute name="spacing" type="array" array_type="float" array_size="3"/>
1498+
<attribute name="scale" type="array" array_type="float" array_size="3"/>
14901499
<attribute name="radius" type="float"/>
1491-
<attribute name="rigid" type="int"/>
1500+
<attribute name="rigid" type="string" valid_values="true false"/>
14921501
<attribute name="mass" type="float"/>
14931502
<attribute name="inertiabox" type="float"/>
14941503
<attribute name="file" type="string"/>
14951504
<attribute name="point" type="string"/>
14961505
<attribute name="element" type="string"/>
14971506
<attribute name="texcoord" type="string"/>
14981507
<attribute name="material" type="string"/>
1499-
<attribute name="rgba" type="array" array_type="float" array_size="2"/>
1508+
<attribute name="rgba" type="array" array_type="float" array_size="4"/>
15001509
<attribute name="flatskin" type="int"/>
1501-
<attribute name="pos" type="array" array_type="int" array_size="3"/>
1502-
<attribute name="quat" type="array" array_type="int" array_size="4"/>
1510+
<attribute name="pos" type="array" array_type="float" array_size="3"/>
1511+
<attribute name="quat" type="array" array_type="float" array_size="4"/>
15031512
<attribute name="axisangle" type="array" array_type="float" array_size="4"/>
15041513
<attribute name="xyaxis" type="array" array_type="float" array_size="6"/>
15051514
<attribute name="zaxis" type="array" array_type="float" array_size="3"/>
15061515
<attribute name="euler" type="array" array_type="float" array_size="3"/>
1516+
<attribute name="dim" type="int" required="true"/>
15071517
</attributes>
15081518
<children>
15091519
<element name="plugin" repeated="true" namespace="flexcomp">
@@ -1523,7 +1533,7 @@
15231533
</element>
15241534
<element name="edge" repeated="true">
15251535
<attributes>
1526-
<attribute name="equality" type="int"/>
1536+
<attribute name="equality" type="string" valid_values="true false"/>
15271537
<attribute name="solref" type="array" array_type="float" array_size="2"/>
15281538
<attribute name="solimp" type="array" array_type="float" array_size="5"/>
15291539
<attribute name="stiffness" type="float"/>
@@ -1542,8 +1552,8 @@
15421552
<attribute name="solimp" type="array" array_type="float" array_size="5"/>
15431553
<attribute name="margin" type="float"/>
15441554
<attribute name="gap" type="float"/>
1545-
<attribute name="internal" type="int"/>
1546-
<attribute name="selfcollide" type="int"/>
1555+
<attribute name="internal" type="string" valid_values="true false"/>
1556+
<attribute name="selfcollide" type="string" valid_values="none narrow bvh sap auto"/>
15471557
<attribute name="activelayers" type="int"/>
15481558
</attributes>
15491559
</element>
@@ -1690,7 +1700,7 @@
16901700
<attribute name="dim" type="reference" required="true" reference_namespace="geom"/>
16911701
<attribute name="radius" type="reference" required="true" reference_namespace="geom"/>
16921702
<attribute name="material" type="int"/>
1693-
<attribute name="rgba" type="array" array_type="float" array_size="5"/>
1703+
<attribute name="rgba" type="array" array_type="float" array_size="4"/>
16941704
<attribute name="flatskin" type="array" array_type="float" array_size="2"/>
16951705
<attribute name="body" type="array" array_type="float" array_size="2"/>
16961706
<attribute name="vertex" type="array" array_type="float" array_size="5"/>
@@ -1717,7 +1727,7 @@
17171727
<attribute name="margin" type="float"/>
17181728
<attribute name="gap" type="float"/>
17191729
<attribute name="internal" type="int"/>
1720-
<attribute name="selfcollide" type="int"/>
1730+
<attribute name="selfcollide" type="string" valid_values="none narrow bvh sap auto"/>
17211731
<attribute name="activelayers" type="int"/>
17221732
</attributes>
17231733
</element>
@@ -1989,6 +1999,7 @@
19891999
<attribute name="user" type="array" array_type="float"/>
19902000
<attribute name="kp" type="float"/>
19912001
<attribute name="kv" type="float"/>
2002+
<attribute name="timeconst" type="float"/>
19922003
</attributes>
19932004
</element>
19942005
<element name="velocity" repeated="true" namespace="actuator">
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<mujoco model="Textured cube and mesh">
2+
<compiler meshdir="meshes" texturedir="textures"/>
3+
<asset>
4+
<mesh name="cube" file="cube.stl"/>
5+
<mesh name="another_cube" file="more_meshes/cube.stl"/>
6+
<mesh name="unused_asset_should_not_cause_problems" file="cube.stl"/>
7+
<mesh name="cube_msh" file="cube.msh"/>
8+
<texture name="texture" file="deepmind.png"/>
9+
<material name="mat_texture" texture="texture"/>
10+
<hfield name="hill" file="../textures/deepmind.png" size="0.5 0.5 0.5 0.1"/>
11+
</asset>
12+
<worldbody>
13+
<light diffuse=".5 .5 .5" pos="0 0 3" dir="0 0 -1"/>
14+
<geom name="left/cube" type="mesh" mesh="cube" material="mat_texture"/>
15+
<geom name="right/cube" type="mesh" mesh="another_cube" material="mat_texture" pos="2.5 0. 0."/>
16+
<geom type="mesh" mesh="cube_msh" material="mat_texture" pos="4. 0. 0."/>
17+
<geom type="hfield" hfield="hill" pos="1.2 0. 0" rgba="0. 0.9 0. 1" size="40 40 0.1"/>
18+
</worldbody>
19+
</mujoco>

0 commit comments

Comments
 (0)