(n_bodies)
return etree.tostring(mjcf, pretty_print=True)
Generates an xml string defining a swimmer with `n_bodies` bodies.
Generates an xml string defining a swimmer with `n_bodies` bodies.
Generates an xml string defining a swimmer with n_bodies bodies .
def _make_model(n_bodies):
  """Generates an xml string defining a swimmer with `n_bodies` bodies."""
  if n_bodies < 3:
    raise ValueError('At least 3 bodies required. Received {}'.format(n_bodies))
  mjcf = etree.fromstring(common.read_model('swimmer.xml'))
  head_body = mjcf.find('./worldbody/body')
  actuator = etree.SubElement(mjcf, 'actuator')
  sensor = etree.SubElement(mjcf, 'sensor')

  parent = head_body
  for body_index in range(n_bodies - 1):
    site_name = 'site_{}'.format(body_index)
    child = _make_body(body_index=body_index)
    child.append(etree.Element('site', name=site_name))
    joint_name = 'joint_{}'.format(body_index)
    joint_limit = 360.0/n_bodies
    joint_range = '{} {}'.format(-joint_limit, joint_limit)
    child.append(etree.Element('joint', {'name': joint_name,
                                         'range': joint_range}))
    motor_name = 'motor_{}'.format(body_index)
    actuator.append(etree.Element('motor', name=motor_name, joint=joint_name))
    velocimeter_name = 'velocimeter_{}'.format(body_index)
    sensor.append(etree.Element('velocimeter', name=velocimeter_name,
                                site=site_name))
    gyro_name = 'gyro_{}'.format(body_index)
    sensor.append(etree.Element('gyro', name=gyro_name, site=site_name))
    parent.append(child)
    parent = child

  # Move tracking cameras further away from the swimmer according to its length.
  cameras = mjcf.findall('./worldbody/body/camera')
  scale = n_bodies / 6.0
  for cam in cameras:
    if cam.get('mode') == 'trackcom':
      old_pos = cam.get('pos').split(' ')
      new_pos = ' '.join([str(float(dim) * scale) for dim in old_pos])
      cam.set('pos', new_pos)

  return etree.tostring(mjcf, pretty_print=True)
def _make_model ( n_bodies ) : if n_bodies < 3 : raise ValueError ( 'At least 3 bodies required. Received {}' . format ( n_bodies ) ) mjcf = etree . fromstring ( common . read_model ( 'swimmer.xml' ) ) head_body = mjcf . find ( './worldbody/body' ) actuator = etree . SubElement ( mjcf , 'actuator' ) sensor = etree . SubElement ( mjcf , 'sensor' ) parent = head_body for body_index in range ( n_bodies - 1 ) : site_name = 'site_{}' . format ( body_index ) child = _make_body ( body_index = body_index ) child . append ( etree . Element ( 'site' , name = site_name ) ) joint_name = 'joint_{}' . format ( body_index ) joint_limit = 360.0 / n_bodies joint_range = '{} {}' . format ( - joint_limit , joint_limit ) child . append ( etree . Element ( 'joint' , { 'name' : joint_name , 'range' : joint_range } ) ) motor_name = 'motor_{}' . format ( body_index ) actuator . append ( etree . Element ( 'motor' , name = motor_name , joint = joint_name ) ) velocimeter_name = 'velocimeter_{}' . format ( body_index ) sensor . append ( etree . Element ( 'velocimeter' , name = velocimeter_name , site = site_name ) ) gyro_name = 'gyro_{}' . format ( body_index ) sensor . append ( etree . Element ( 'gyro' , name = gyro_name , site = site_name ) ) parent . append ( child ) parent = child # Move tracking cameras further away from the swimmer according to its length. cameras = mjcf . findall ( './worldbody/body/camera' ) scale = n_bodies / 6.0 for cam in cameras : if cam . get ( 'mode' ) == 'trackcom' : old_pos = cam . get ( 'pos' ) . split ( ' ' ) new_pos = ' ' . join ( [ str ( float ( dim ) * scale ) for dim in old_pos ] ) cam . set ( 'pos' , new_pos ) return etree . tostring ( mjcf , pretty_print = True )