编辑: 这段代码有效:
def randomize_model(self, model_path):
tree = ET.parse(model_path)
root = tree.getroot()
for body in root.iter('body'):
regex = re.compile(F'object[0-{self.num_blocks}]')
if re.match(regex, body.attrib['name']):
body.set('mass', str(np.random.choice(np.logspace(-6.64, 1, base=2)))) # in KG, logarithmically sampled from .01 kg to 1kg
body.set('friction', str(np.random.choice(np.logspace(-3.3, 2.32, base=2)))) # .1, 5
body.set('damping', str(np.random.choice(np.logspace(-6.64, -2.3, base=2)))) #
# TODO: scale size so that larger blocks are heavier
# ET.SubElement(root[3], body)
new_tree = ET.ElementTree(root)
new_tree.write(F"/tmp/stack{self.num_blocks}.xml")
return
但是下面没有。为什么? -我认为部分原因是无提示错误,因为np.random.choice ...不是字符串。但是,为什么ElementTree没有引发异常或错误?我对导致问题的原因一无所知
def randomize_model(self, model_path):
tree = xml.etree.ElementTree.parse(model_path)
root = tree.getroot()
bodies = root.iter('body')
for body in bodies:
# The logic below ends up deleting the 'body' nodes for some reason
regex = re.compile(F'object[0-{self.num_blocks}]')
if re.match(regex, body.attrib['name']):
body.set('mass', np.random.choice(np.logspace(-6.64, 1, base=2))) # in KG, logarithmically sampled from .01 kg to 1kg
body.set('friction', np.random.choice(np.logspace(-3.3, 2.32, base=2))) # .1, 5
body.set('damping', np.random.choice(np.logspace(-6.64, -2.3, base=2))) #
tree.write(F"/tmp/output2.xml")
return
为什么此代码删除在for循环中找到的“ body”节点? tree.write行中的节点被完全擦除。
要告诉您节点已删除,请检查/tmp/output2.xml所有名称= object *的XML元素都将被删除。文件已消失。
这是加载到上面randomize_model的model_path中的model_xml.xml:
<?xml version="1.0" encoding="utf-8"?>
<mujoco>
<compiler angle="radian" coordinate="local" meshdir="../stls/fetch" texturedir="../textures"></compiler>
<option timestep="0.002">
<flag warmstart="enable"></flag>
</option>
<include file="shared.xml"></include>
<worldbody>
<geom name="floor0" pos="0.8 0.75 0" size="0.85 0.70 1" type="plane" condim="3" material="floor_mat"></geom>
<body name="floor0" pos="0.8 0.75 0">
<site name="target0" pos="0 0 0.5" size="0.02 0.02 0.02" rgba="0 1 0 0.3" type="sphere"></site>
<site name="target1" pos="0 0 0.5" size="0.02 0.02 0.02" rgba="1 1 0 0.3" type="sphere"></site>
</body>
<include file="robot.xml"></include>
<body pos="1.3 0.75 0.2" name="table0">
<geom size="0.25 0.35 0.2" type="box" mass="2000" material="table_mat"></geom>
</body>
<body name="object0" pos="0.025 0.025 0.025">
<joint name="object0:joint" type="free" damping="0.01"></joint>
<geom size="0.025 0.025 0.025" type="box" condim="3" name="object0" material="block0_mat" mass="2"></geom>
<site name="object0" pos="0 0 0" size="0.02 0.02 0.02" rgba="1 0 0 1" type="sphere"></site>
</body>
<body name="object1" pos="0.025 0.025 0.025">
<joint name="object1:joint" type="free" damping="0.01"></joint>
<geom size="0.025 0.025 0.025" type="box" condim="3" name="object1" material="block1_mat" mass="2"></geom>
<site name="object1" pos="0 0 0" size="0.02 0.02 0.02" rgba="1 0 0 1" type="sphere"></site>
</body>
<light directional="true" ambient="0.2 0.2 0.2" diffuse="0.8 0.8 0.8" specular="0.3 0.3 0.3" castshadow="false" pos="0 0 4" dir="0 0 -1" name="light0"></light>
</worldbody>
<actuator>
<position ctrllimited="true" ctrlrange="0 0.2" joint="robot0:l_gripper_finger_joint" kp="30000" name="robot0:l_gripper_finger_joint" user="1"></position>
<position ctrllimited="true" ctrlrange="0 0.2" joint="robot0:r_gripper_finger_joint" kp="30000" name="robot0:r_gripper_finger_joint" user="1"></position>
</actuator>
</mujoco>