Skip to content

Commit b264408

Browse files
barikata1984copybara-github
authored andcommitted
dm_control: Import of refs/pull/496/head
PiperOrigin-RevId: 686468082 Change-Id: I5f16943624183b578c1672b408430f66083ba61c
1 parent 6fe299c commit b264408

2 files changed

Lines changed: 50 additions & 26 deletions

File tree

dm_control/mjcf/attribute.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def __init__(self, extension, prefix=''):
424424
def __eq__(self, other):
425425
return self.get_vfs_filename() == other.get_vfs_filename()
426426

427-
def get_vfs_filename(self):
427+
def get_vfs_filename(self, filename_with_hash=True):
428428
"""Returns the name of the asset file as registered in MuJoCo's VFS."""
429429
# Hash the contents of the asset to get a unique identifier.
430430
hash_string = hashlib.sha1(util.to_binary_string(self.contents)).hexdigest()
@@ -435,7 +435,10 @@ def get_vfs_filename(self):
435435
if raw_length > constants.MAX_VFS_FILENAME_LENGTH:
436436
trim_amount = raw_length - constants.MAX_VFS_FILENAME_LENGTH
437437
prefix = prefix[:-trim_amount]
438-
filename = '-'.join([prefix, hash_string])
438+
if filename_with_hash:
439+
filename = '-'.join([prefix, hash_string])
440+
else:
441+
filename = prefix
439442
else:
440443
filename = hash_string
441444

@@ -568,6 +571,7 @@ def to_xml_string(self, prefix_root=None, **kwargs):
568571
"""Returns the asset filename as it will appear in the generated XML."""
569572
del prefix_root # Unused
570573
if self._value is not None:
571-
return self._value.get_vfs_filename()
574+
filename_with_hash = kwargs.get('filename_with_hash', True)
575+
return self._value.get_vfs_filename(filename_with_hash)
572576
else:
573577
return None

dm_control/mjcf/element.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import numpy as np
3333

3434

35-
_raw_property = property # pylint: disable=invalid-name
35+
_raw_property = property # pylint: disable=invalid-name,used-before-assignment
3636

3737
_UNITS = ('K', 'M', 'G', 'T', 'P', 'E')
3838

@@ -751,7 +751,7 @@ def all_children(self):
751751
def to_xml(self, prefix_root=None, debug_context=None,
752752
*,
753753
precision=constants.XML_DEFAULT_PRECISION,
754-
zero_threshold=0):
754+
zero_threshold=0, filename_with_hash=True):
755755
"""Generates an etree._Element corresponding to this MJCF element.
756756
757757
Args:
@@ -766,25 +766,31 @@ def to_xml(self, prefix_root=None, debug_context=None,
766766
quantities.
767767
zero_threshold: (optional) When outputting XML, floating point quantities
768768
whose absolute value falls below this threshold will be treated as zero.
769+
filename_with_hash: (optional) A boolean, whether to append a hash string
770+
to the name of a file when it is registered in MuJoCo's VFS.
769771
770772
Returns:
771773
An etree._Element object.
772774
"""
773775
prefix_root = prefix_root or self.namescope
774776
xml_element = etree.Element(self._spec.name)
775777
self._attributes_to_xml(xml_element, prefix_root, debug_context,
776-
precision=precision, zero_threshold=zero_threshold)
778+
precision=precision, zero_threshold=zero_threshold,
779+
filename_with_hash=filename_with_hash)
777780
self._children_to_xml(xml_element, prefix_root, debug_context,
778-
precision=precision, zero_threshold=zero_threshold)
781+
precision=precision, zero_threshold=zero_threshold,
782+
filename_with_hash=filename_with_hash)
779783
return xml_element
780784

781785
def _attributes_to_xml(self, xml_element, prefix_root, debug_context=None,
782-
*, precision, zero_threshold):
786+
*, precision, zero_threshold, filename_with_hash):
783787
del debug_context # Unused.
784788
for attribute_name, attribute in self._attributes.items():
785-
attribute_value = attribute.to_xml_string(prefix_root,
786-
precision=precision,
787-
zero_threshold=zero_threshold)
789+
attribute_value = attribute.to_xml_string(
790+
prefix_root,
791+
precision=precision,
792+
zero_threshold=zero_threshold,
793+
filename_with_hash=filename_with_hash)
788794
if attribute_name == self._spec.identifier and attribute_value is None:
789795
xml_element.set(attribute_name, self.full_identifier)
790796
elif attribute_value is None:
@@ -793,11 +799,12 @@ def _attributes_to_xml(self, xml_element, prefix_root, debug_context=None,
793799
xml_element.set(attribute_name, attribute_value)
794800

795801
def _children_to_xml(self, xml_element, prefix_root, debug_context=None,
796-
*, precision, zero_threshold):
802+
*, precision, zero_threshold, filename_with_hash):
797803
for child in self.all_children():
798804
child_xml = child.to_xml(prefix_root, debug_context,
799805
precision=precision,
800-
zero_threshold=zero_threshold)
806+
zero_threshold=zero_threshold,
807+
filename_with_hash=filename_with_hash)
801808
if (child_xml.attrib or len(child_xml) # pylint: disable=g-explicit-length-test
802809
or child.spec.repeated or child.spec.on_demand):
803810
xml_element.append(child_xml)
@@ -811,7 +818,7 @@ def to_xml_string(self, prefix_root=None,
811818
self_only=False, pretty_print=True, debug_context=None,
812819
*,
813820
precision=constants.XML_DEFAULT_PRECISION,
814-
zero_threshold=0):
821+
zero_threshold=0, filename_with_hash=True):
815822
"""Generates an XML string corresponding to this MJCF element.
816823
817824
Args:
@@ -830,13 +837,16 @@ def to_xml_string(self, prefix_root=None,
830837
quantities.
831838
zero_threshold: (optional) When outputting XML, floating point quantities
832839
whose absolute value falls below this threshold will be treated as zero.
840+
filename_with_hash: (optional) A boolean, whether to append a hash string
841+
to the name of a file when it is registered in MuJoCo's VFS.
833842
834843
Returns:
835844
A string.
836845
"""
837846
xml_element = self.to_xml(prefix_root, debug_context,
838847
precision=precision,
839-
zero_threshold=zero_threshold)
848+
zero_threshold=zero_threshold,
849+
filename_with_hash=filename_with_hash)
840850
if self_only and len(xml_element) > 0: # pylint: disable=g-explicit-length-test
841851
etree.strip_elements(xml_element, '*')
842852
xml_element.text = '...'
@@ -1073,10 +1083,12 @@ def prefixed_identifier(self, prefix_root=None):
10731083
def to_xml(self, prefix_root=None, debug_context=None,
10741084
*,
10751085
precision=constants.XML_DEFAULT_PRECISION,
1076-
zero_threshold=0):
1086+
zero_threshold=0,
1087+
filename_with_hash=True):
10771088
xml_element = (super().to_xml(prefix_root, debug_context,
10781089
precision=precision,
1079-
zero_threshold=zero_threshold))
1090+
zero_threshold=zero_threshold,
1091+
filename_with_hash=filename_with_hash))
10801092
xml_element.set('name', self.prefixed_identifier(prefix_root))
10811093
return xml_element
10821094

@@ -1104,10 +1116,12 @@ class _AttachmentFrameChild(_ElementImpl):
11041116
def to_xml(self, prefix_root=None, debug_context=None,
11051117
*,
11061118
precision=constants.XML_DEFAULT_PRECISION,
1107-
zero_threshold=0):
1119+
zero_threshold=0,
1120+
filename_with_hash=True):
11081121
xml_element = (super().to_xml(prefix_root, debug_context,
11091122
precision=precision,
1110-
zero_threshold=zero_threshold))
1123+
zero_threshold=zero_threshold,
1124+
filename_with_hash=filename_with_hash))
11111125
if self.spec.namespace is not None:
11121126
if self.name:
11131127
name = (self._parent.prefixed_identifier(prefix_root) +
@@ -1147,18 +1161,22 @@ def all_children(self):
11471161
def to_xml(self, prefix_root=None, debug_context=None,
11481162
*,
11491163
precision=constants.XML_DEFAULT_PRECISION,
1150-
zero_threshold=0):
1164+
zero_threshold=0,
1165+
filename_with_hash=True):
11511166
prefix_root = prefix_root or self.namescope
11521167
xml_element = (super().to_xml(prefix_root, debug_context,
11531168
precision=precision,
1154-
zero_threshold=zero_threshold))
1169+
zero_threshold=zero_threshold,
1170+
filename_with_hash=filename_with_hash))
11551171
if isinstance(self._parent, RootElement):
11561172
root_default = etree.Element(self._spec.name)
11571173
root_default.append(xml_element)
11581174
for attachment in self._attachments.values():
1159-
attachment_xml = attachment.to_xml(prefix_root, debug_context,
1160-
precision=precision,
1161-
zero_threshold=zero_threshold)
1175+
attachment_xml = attachment.to_xml(
1176+
prefix_root, debug_context,
1177+
precision=precision,
1178+
zero_threshold=zero_threshold,
1179+
filename_with_hash=filename_with_hash)
11621180
for attachment_child_xml in attachment_xml:
11631181
root_default.append(attachment_child_xml)
11641182
xml_element = root_default
@@ -1173,12 +1191,14 @@ class _ActuatorElement(_ElementImpl):
11731191
def _children_to_xml(self, xml_element, prefix_root, debug_context=None,
11741192
*,
11751193
precision=constants.XML_DEFAULT_PRECISION,
1176-
zero_threshold=0):
1194+
zero_threshold=0,
1195+
filename_with_hash=True):
11771196
debug_comments = {}
11781197
for child in self.all_children():
11791198
child_xml = child.to_xml(prefix_root, debug_context,
11801199
precision=precision,
1181-
zero_threshold=zero_threshold)
1200+
zero_threshold=zero_threshold,
1201+
filename_with_hash=filename_with_hash)
11821202
if debugging.debug_mode() and debug_context:
11831203
debug_comment = debug_context.register_element_for_debugging(child)
11841204
debug_comments[child_xml] = debug_comment

0 commit comments

Comments
 (0)