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

Append or not append a hash string to the name of a file when to_xml_string() is called #496

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions dm_control/mjcf/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def __init__(self, extension, prefix=''):
def __eq__(self, other):
return self.get_vfs_filename() == other.get_vfs_filename()

def get_vfs_filename(self):
def get_vfs_filename(self, filename_with_hash=True):
"""Returns the name of the asset file as registered in MuJoCo's VFS."""
# Hash the contents of the asset to get a unique identifier.
hash_string = hashlib.sha1(util.to_binary_string(self.contents)).hexdigest()
Expand All @@ -435,7 +435,10 @@ def get_vfs_filename(self):
if raw_length > constants.MAX_VFS_FILENAME_LENGTH:
trim_amount = raw_length - constants.MAX_VFS_FILENAME_LENGTH
prefix = prefix[:-trim_amount]
filename = '-'.join([prefix, hash_string])
if filename_with_hash:
filename = '-'.join([prefix, hash_string])
else:
filename = prefix
else:
filename = hash_string

Expand Down Expand Up @@ -568,6 +571,7 @@ def to_xml_string(self, prefix_root=None, **kwargs):
"""Returns the asset filename as it will appear in the generated XML."""
del prefix_root # Unused
if self._value is not None:
return self._value.get_vfs_filename()
filename_with_hash = kwargs.get("filename_with_hash", True)
return self._value.get_vfs_filename(filename_with_hash)
else:
return None
54 changes: 36 additions & 18 deletions dm_control/mjcf/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def all_children(self):
def to_xml(self, prefix_root=None, debug_context=None,
*,
precision=constants.XML_DEFAULT_PRECISION,
zero_threshold=0):
zero_threshold=0, filename_with_hash=True):
"""Generates an etree._Element corresponding to this MJCF element.

Args:
Expand All @@ -766,25 +766,30 @@ def to_xml(self, prefix_root=None, debug_context=None,
quantities.
zero_threshold: (optional) When outputting XML, floating point quantities
whose absolute value falls below this threshold will be treated as zero.
filename_with_hash: (optional) A boolean, whether to append a hash string
to the name of a file when it is registered in MuJoCo's VFS.

Returns:
An etree._Element object.
"""
prefix_root = prefix_root or self.namescope
xml_element = etree.Element(self._spec.name)
self._attributes_to_xml(xml_element, prefix_root, debug_context,
precision=precision, zero_threshold=zero_threshold)
precision=precision, zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
self._children_to_xml(xml_element, prefix_root, debug_context,
precision=precision, zero_threshold=zero_threshold)
precision=precision, zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
return xml_element

def _attributes_to_xml(self, xml_element, prefix_root, debug_context=None,
*, precision, zero_threshold):
*, precision, zero_threshold, filename_with_hash):
del debug_context # Unused.
for attribute_name, attribute in self._attributes.items():
attribute_value = attribute.to_xml_string(prefix_root,
precision=precision,
zero_threshold=zero_threshold)
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
if attribute_name == self._spec.identifier and attribute_value is None:
xml_element.set(attribute_name, self.full_identifier)
elif attribute_value is None:
Expand All @@ -793,11 +798,12 @@ def _attributes_to_xml(self, xml_element, prefix_root, debug_context=None,
xml_element.set(attribute_name, attribute_value)

def _children_to_xml(self, xml_element, prefix_root, debug_context=None,
*, precision, zero_threshold):
*, precision, zero_threshold, filename_with_hash):
for child in self.all_children():
child_xml = child.to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold)
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
if (child_xml.attrib or len(child_xml) # pylint: disable=g-explicit-length-test
or child.spec.repeated or child.spec.on_demand):
xml_element.append(child_xml)
Expand All @@ -811,7 +817,7 @@ def to_xml_string(self, prefix_root=None,
self_only=False, pretty_print=True, debug_context=None,
*,
precision=constants.XML_DEFAULT_PRECISION,
zero_threshold=0):
zero_threshold=0, filename_with_hash=True):
"""Generates an XML string corresponding to this MJCF element.

Args:
Expand All @@ -830,13 +836,16 @@ def to_xml_string(self, prefix_root=None,
quantities.
zero_threshold: (optional) When outputting XML, floating point quantities
whose absolute value falls below this threshold will be treated as zero.
filename_with_hash: (optional) A boolean, whether to append a hash string
to the name of a file when it is registered in MuJoCo's VFS.

Returns:
A string.
"""
xml_element = self.to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold)
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
if self_only and len(xml_element) > 0: # pylint: disable=g-explicit-length-test
etree.strip_elements(xml_element, '*')
xml_element.text = '...'
Expand Down Expand Up @@ -1073,10 +1082,12 @@ def prefixed_identifier(self, prefix_root=None):
def to_xml(self, prefix_root=None, debug_context=None,
*,
precision=constants.XML_DEFAULT_PRECISION,
zero_threshold=0):
zero_threshold=0,
filename_with_hash=True):
xml_element = (super().to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold))
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash))
xml_element.set('name', self.prefixed_identifier(prefix_root))
return xml_element

Expand Down Expand Up @@ -1104,10 +1115,12 @@ class _AttachmentFrameChild(_ElementImpl):
def to_xml(self, prefix_root=None, debug_context=None,
*,
precision=constants.XML_DEFAULT_PRECISION,
zero_threshold=0):
zero_threshold=0,
filename_with_hash=True):
xml_element = (super().to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold))
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash))
if self.spec.namespace is not None:
if self.name:
name = (self._parent.prefixed_identifier(prefix_root) +
Expand Down Expand Up @@ -1147,18 +1160,21 @@ def all_children(self):
def to_xml(self, prefix_root=None, debug_context=None,
*,
precision=constants.XML_DEFAULT_PRECISION,
zero_threshold=0):
zero_threshold=0,
filename_with_hash=True):
prefix_root = prefix_root or self.namescope
xml_element = (super().to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold))
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash))
if isinstance(self._parent, RootElement):
root_default = etree.Element(self._spec.name)
root_default.append(xml_element)
for attachment in self._attachments.values():
attachment_xml = attachment.to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold)
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
for attachment_child_xml in attachment_xml:
root_default.append(attachment_child_xml)
xml_element = root_default
Expand All @@ -1173,12 +1189,14 @@ class _ActuatorElement(_ElementImpl):
def _children_to_xml(self, xml_element, prefix_root, debug_context=None,
*,
precision=constants.XML_DEFAULT_PRECISION,
zero_threshold=0):
zero_threshold=0,
filename_with_hash=True):
debug_comments = {}
for child in self.all_children():
child_xml = child.to_xml(prefix_root, debug_context,
precision=precision,
zero_threshold=zero_threshold)
zero_threshold=zero_threshold,
filename_with_hash=filename_with_hash)
if debugging.debug_mode() and debug_context:
debug_comment = debug_context.register_element_for_debugging(child)
debug_comments[child_xml] = debug_comment
Expand Down