def make_model(floor_size=None, terrain=False, rangefinders=False, walls_and_ball=False, goal=False): """Returns the model XML string.""" xml_string = common.read_model('quadruped.xml') parser = etree.XMLParser(remove_blank_text=True) mjcf = etree.XML(xml_string, parser) # Set floor size. if floor_size is not None: floor_geom = mjcf.find('.//geom[@name={!r}]'.format('floor')) floor_geom.attrib['size'] = '{} {} .5'.format(floor_size, floor_size) # Remove walls, ball and target. if not walls_and_ball: for wall in _WALLS: wall_geom = xml_tools.find_element(mjcf, 'geom', wall) wall_geom.getparent().remove(wall_geom) # Remove ball. ball_body = xml_tools.find_element(mjcf, 'body', 'ball') ball_body.getparent().remove(ball_body) # Remove target. target_site = xml_tools.find_element(mjcf, 'site', 'target') target_site.getparent().remove(target_site) if not goal: pass # Remove terrain. if not terrain: terrain_geom = xml_tools.find_element(mjcf, 'geom', 'terrain') terrain_geom.getparent().remove(terrain_geom) # Remove rangefinders if they're not used, as range computations can be # expensive, especially in a scene with heightfields. if not rangefinders: rangefinder_sensors = mjcf.findall('.//rangefinder') for rf in rangefinder_sensors: rf.getparent().remove(rf) return etree.tostring(mjcf, pretty_print=True)
def make_model(no_walls=False): xml_string = common.read_model('ant.xml') parser = etree.XMLParser(remove_blank_text=True) mjcf = etree.XML(xml_string, parser) if no_walls: for wall in _WALLS: wall_geom = xml_tools.find_element(mjcf, 'geom', wall) wall_geom.getparent().remove(wall_geom) return etree.tostring(mjcf, pretty_print=True)
def parse_tendon_route(targets): for target in targets: if isinstance(target, list): # The tendon is branching divisor.append(divisor[-1] * len(target)) branch_start.append(spatial.getchildren()[-1]) # Go through each branch separately (depth first) for branch in target: # Add the pulley spatial.append(etree.Element('pulley', divisor=f'{divisor[-1]}')) spatial.append(deepcopy(branch_start[-1])) parse_tendon_route(branch) branch_start.pop() divisor.pop() else: # Load stl file of site m = mesh.Mesh.from_file(target.mesh_file) # Use midpoint of mesh for site position T_midpoint = np.eye(4) T_midpoint[:3, 3] = m.vectors.mean(axis=(0, 1)) # Estimate site's position relative to body's position body = xml_tools.find_element(mjcf, 'geom', target.geom).getparent() T_body = calculate_transformation(body) T_site = np.matmul(np.linalg.inv(T_body), T_midpoint) # Create the site site = etree.Element('site', name=target.name, pos=array_to_string(T_site[:3, 3])) site.attrib["class"] = 'connector' # Add the site into body; either after existing sites or append to end if body.findall('site'): # Don't add the site if it already exists site_exists = False for s in body.findall('site'): if s.get('name') == target.name: site_exists = True break if not site_exists: s.addnext(site) else: body.append(site) # Add site to tendon spatial.append(etree.Element('site', site=target.name))
def make_model(n_boxes): """Returns a tuple containing the model XML string and a dict of assets.""" xml_string = common.read_model('stacker.xml') parser = etree.XMLParser(remove_blank_text=True) mjcf = etree.XML(xml_string, parser) # Remove unused boxes for b in range(n_boxes, 4): box = xml_tools.find_element(mjcf, 'body', 'box' + str(b)) box.getparent().remove(box) return etree.tostring(mjcf, pretty_print=True), common.ASSETS
def test_find_element(self): xml_str = """ <root> <option name='option_name'> <content></content> </option> <world name='world_name'> <geom name='geom_name'/> </world> </root>""" tree = xml_tools.parse(six.StringIO(xml_str)) world = xml_tools.find_element(root=tree, tag='world', name='world_name') self.assertEqual(world.tag, 'world') self.assertEqual(world.attrib['name'], 'world_name') geom = xml_tools.find_element(root=tree, tag='geom', name='geom_name') self.assertEqual(geom.tag, 'geom') self.assertEqual(geom.attrib['name'], 'geom_name') with self.assertRaisesRegexp(ValueError, 'Element with tag'): xml_tools.find_element(root=tree, tag='does_not_exist', name='name') with self.assertRaisesRegexp(ValueError, 'Element with tag'): xml_tools.find_element(root=tree, tag='world', name='does_not_exist')
def make_model(floor_size, remove_ball): """Sets floor size, removes ball and walls (Stand and Move tasks).""" xml_string = common.read_model('dog.xml') parser = etree.XMLParser(remove_blank_text=True) mjcf = etree.XML(xml_string, parser) # set floor size. floor = xml_tools.find_element(mjcf, 'geom', 'floor') floor.attrib['size'] = str(floor_size) + ' ' + str(floor_size) + ' .1' if remove_ball: # Remove ball, target and walls. ball = xml_tools.find_element(mjcf, 'body', 'ball') ball.getparent().remove(ball) target = xml_tools.find_element(mjcf, 'geom', 'target') target.getparent().remove(target) ball_cam = xml_tools.find_element(mjcf, 'camera', 'ball') ball_cam.getparent().remove(ball_cam) head_cam = xml_tools.find_element(mjcf, 'camera', 'head') head_cam.getparent().remove(head_cam) for wall_name in ['px', 'nx', 'py', 'ny']: wall = xml_tools.find_element(mjcf, 'geom', 'wall_' + wall_name) wall.getparent().remove(wall) return etree.tostring(mjcf, pretty_print=True)
def make_maze_model(num_walls): xml_string = common.read_model('quadruped.xml') parser = etree.XMLParser(remove_blank_text=True) mjcf = etree.XML(xml_string, parser) #floor_geom = mjcf.find('.//geom[@name={!r}]'.format('floor')) #floor_geom.attrib['size'] = '{} {} .5'.format(5, 5) # Remove ball. ball_body = xml_tools.find_element(mjcf, 'body', 'ball') ball_body.getparent().remove(ball_body) # Remove target. #target_site = xml_tools.find_element(mjcf, 'site', 'target') #target_site.attrib['pos'] = '12 12 .05' terrain_geom = xml_tools.find_element(mjcf, 'geom', 'terrain') terrain_geom.getparent().remove(terrain_geom) rangefinder_sensors = mjcf.findall( './/rangefinder') for rf in rangefinder_sensors: rf.getparent().remove(rf) return etree.tostring(mjcf, pretty_print=True)
def make_model(use_peg, insert): """Returns a tuple containing the model XML string and a dict of assets.""" xml_string = common.read_model('manipulator.xml') parser = etree.XMLParser(remove_blank_text=True) mjcf = etree.XML(xml_string, parser) # Select the desired prop. if use_peg: required_props = ['peg', 'target_peg'] if insert: required_props += ['slot'] else: required_props = ['ball', 'target_ball'] if insert: required_props += ['cup'] # Remove unused props for unused_prop in _ALL_PROPS.difference(required_props): prop = xml_tools.find_element(mjcf, 'body', unused_prop) prop.getparent().remove(prop) return etree.tostring(mjcf, pretty_print=True), common.ASSETS