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 |
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 |
The TwoChairs.wbt file once loaded by Webots appears as shown in figure 5.1.

Figure 5.1: Two instances of the TwoColorChair prototype in Webots