Webots Reference Manual - chapter 5 - section 3

Webots Reference Manual


5.3 Example

A complete example of prototype definition and instantiation is provided here. The prototype is called TwoColorChair; it defines a simple chair with four legs and a seating part. For simplicity, this prototype does not have bounding objects nor Physics nodes. A more complete example of this prototype named SimpleChair is provided in Webots distribution.

The TwoColorChair prototype allows to specify two colors: one for the legs and one for the seating surface of the chair. The interface also defines a translation field and a rotation field that are associated with the equally named fields of the prototype's Solid base node. This allows to store the position and orientation of the prototype instances.

TwoColorChair.proto:

# A two-color chair

PROTO TwoColorChair [
  field SFVec3f    translation 0 0.91 0
  field SFRotation rotation    0 1 0 0
  field SFColor    legColor    1 1 0
  field SFColor    seatColor   1 0.65 0 ]
{
  Solid {
    translation IS translation
    rotation IS rotation
    children [
      Transform {
        translation 0 0 -0.27
        children [
          Shape {
            appearance Appearance {
              material Material { diffuseColor IS seatColor }
            }
            geometry Box { size 0.6 0.67 0.0275 }
          }
        ]
      }
      Transform {
        translation 0 -0.35 0
        children [
          Shape {
            appearance Appearance {
              material Material { diffuseColor IS seatColor }
            }
            geometry Box { size 0.6 0.075 0.52 }
          }
        ]
      }
      Transform {
        translation 0.25 -0.65 -0.23
        children [
          DEF LEG_SHAPE Shape {
            appearance Appearance {
              material Material { diffuseColor IS legColor }
            }
            geometry Box { size 0.075 0.52 0.075 }
          }
        ]
      }
      Transform {
        translation -0.25 -0.65 -0.23
        children [ USE LEG_SHAPE ]
      }
      Transform {
        translation 0.25 -0.65 0.2
        children [ USE LEG_SHAPE ]
      }
      Transform {
        translation -0.25 -0.65 0.2
        children [ USE LEG_SHAPE ]
      }
    ]
  }
}

As you can observe in this example, it is perfectly valid to have several IS statement for one interface field (seatColor), as long as the types match. It is also possible to use IS statements inside a defined (DEF) node and then to reuse (USE) that node. This is done here with the diffuseColor IS legColor statement placed inside the DEF LEG_SHAPE Shape node which is then reused (USE) several times below.

The ProtoInstantiationExample.wbt file below exemplifies the instantiation of this prototype. Prototypes are instantiated using the regular node syntax. Fields with the default value can be omitted. Fields which value differ from the default must be specified.

TwoChairs.wbt:

#VRML_SIM V6.0 utf8

WorldInfo {
}
Viewpoint {
  orientation 0.545864 0.834589 0.0741162 5.71096
  position -1.21885 1.67546 3.01015
}
Background {
  skyColor [
    0.4 0.7 1
  ]
}
DirectionalLight {
  direction -0.3 -1 -0.5
  castShadows TRUE
}
TwoColorChair {
}
TwoColorChair {
  translation 1.2 0.91 0
  seatColor 0.564706 0.933333 0.564706
}

The TwoChairs.wbt file once loaded by Webots appears as shown in figure 5.1.

two_chairs

Figure 5.1: Two instances of the TwoColorChair prototype in Webots

release 7.0.2
Copyright © 2012 Cyberbotics Ltd. All right reserved.