-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathtraversal_utils.py
More file actions
81 lines (67 loc) · 2.69 KB
/
traversal_utils.py
File metadata and controls
81 lines (67 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright 2018 The dm_control Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Utility functions that operate on MJCF elements."""
_ACTUATOR_TAGS = ('general', 'motor', 'position',
'velocity', 'cylinder', 'muscle')
def get_freejoint(element):
"""Retrieves the free joint of a body. Returns `None` if there isn't one."""
if element.tag != 'body':
return None
elif hasattr(element, 'freejoint') and element.freejoint is not None:
return element.freejoint
else:
joints = element.find_all('joint', immediate_children_only=True)
for joint in joints:
if joint.type == 'free':
return joint
return None
def get_attachment_frame(mjcf_model):
return mjcf_model.parent_model.find('attachment_frame', mjcf_model.model)
def get_frame_freejoint(mjcf_model):
frame = get_attachment_frame(mjcf_model)
return get_freejoint(frame)
def get_frame_joints(mjcf_model):
"""Retrieves all joints belonging to the attachment frame of an MJCF model."""
frame = get_attachment_frame(mjcf_model)
if frame:
return frame.find_all('joint', immediate_children_only=True)
else:
return None
def commit_defaults(element, attributes=None):
"""Commits default values into attributes of the specified element.
Args:
element: A PyMJCF element.
attributes: (optional) A list of strings specifying the attributes to be
copied from defaults, or `None` if all attributes should be copied.
"""
dclass = element.dclass
parent = element.parent
while dclass is None and parent != element.root:
dclass = getattr(parent, 'childclass', None)
parent = parent.parent
if dclass is None:
dclass = element.root.default
while dclass != element.root:
if element.tag in _ACTUATOR_TAGS:
tags = _ACTUATOR_TAGS
else:
tags = (element.tag,)
for tag in tags:
default_element = getattr(dclass, tag)
for name, value in default_element.get_attributes().items():
if attributes is None or name in attributes:
if hasattr(element, name) and getattr(element, name) is None:
setattr(element, name, value)
dclass = dclass.parent