教程 7:创建你的第一个PROTO (60分钟)


目标与简介

本教程将教你如何创建自定义的PROTO节点。PROTO是Webots中用于创建可重用组件的强大工具。你将学习如何定义PROTO文件、创建字段、构建复杂的几何体,以及如何在多个项目中重复使用你的自定义组件。本教程基于官方教程7

目录

1. 环境准备

在开始本教程之前,请确保你已经完成了前面的教程,并且熟悉基本的Webots操作。我们将创建一个新的项目来学习PROTO的创建和使用。

📝 动手实践 #1: 创建一个新的Webots项目,命名为 proto_tutorial。在场景中添加一个 RectangleArena 作为基础环境。确保仿真处于暂停状态,虚拟时间计数器显示 0:00:00:000。

2. PROTO基础概念

PROTO(Protocol)是Webots中用于创建可重用组件的机制。PROTO文件定义了自定义节点的结构,包括几何体、材质、物理属性等。通过PROTO,你可以创建自己的对象库并在多个项目中重复使用。

📝 动手实践 #2: 了解PROTO文件结构:
  1. 在项目根目录创建 protos 文件夹
  2. PROTO文件使用 .proto 扩展名
  3. 每个PROTO文件定义一个自定义节点
  4. PROTO文件包含字段定义和节点实现
  5. 字段允许在实例化时自定义PROTO的属性

3. 创建简单PROTO

让我们从创建一个简单的PROTO开始。我们将创建一个自定义的立方体PROTO,它可以设置大小和颜色。

📝 动手实践 #3: 创建第一个PROTO文件:
  1. protos 文件夹中创建文件 CustomBox.proto
  2. 使用文本编辑器打开该文件
  3. 添加基本的PROTO结构
  4. 定义字段和节点实现
📝 动手实践 #4: 编写CustomBox.proto文件:
#VRML_SIM R2025a utf8
PROTO CustomBox [
  field SFVec3f    size      1 1 1
  field SFColor    color     0.8 0.8 0.8
  field SFVec3f    translation 0 0 0
  field SFRotation rotation    0 0 1 0
]
{
  Solid {
    translation IS translation
    rotation IS rotation
    children [
      Shape {
        appearance Appearance {
          material Material {
            diffuseColor IS color
          }
        }
        geometry Box {
          size IS size
        }
      }
    ]
    boundingObject Box {
      size IS size
    }
    physics Physics {
      mass 1
    }
  }
}

4. 定义字段

字段是PROTO的核心特性,它们允许你在实例化PROTO时自定义其属性。Webots支持多种字段类型,包括基本类型和复杂类型。

📝 动手实践 #5: 了解字段类型:
  1. SFBool:布尔值(TRUE/FALSE)
  2. SFInt32:32位整数
  3. SFFloat:浮点数
  4. SFString:字符串
  5. SFVec2f:2D向量
  6. SFVec3f:3D向量
  7. SFRotation:旋转
  8. SFColor:颜色
  9. MFNode:节点数组
📝 动手实践 #6: 使用自定义PROTO:
  1. 保存 CustomBox.proto 文件
  2. 在Webots中刷新项目
  3. 在场景树中点击 ADD图标 按钮
  4. 选择 PROTO nodes (Current Project) / CustomBox
  5. 设置 size2 1 0.5
  6. 设置 color1 0 0(红色)
  7. 设置 translation0 0 1

5. 复杂PROTO设计

现在让我们创建一个更复杂的PROTO,包含多个几何体、材质和物理属性。

📝 动手实践 #7: 创建机器人PROTO:
#VRML_SIM R2025a utf8
PROTO SimpleRobot [
  field SFVec3f    bodySize      0.3 0.2 0.1
  field SFColor    bodyColor     0.2 0.6 1.0
  field SFVec3f    headSize      0.1 0.1 0.1
  field SFColor    headColor     0.8 0.6 0.4
  field SFVec3f    translation   0 0 0
  field SFRotation rotation      0 0 1 0
  field SFBool     hasWheels     TRUE
  field SFVec3f    wheelRadius   0.05
  field SFColor    wheelColor    0.3 0.3 0.3
]
{
  Solid {
    translation IS translation
    rotation IS rotation
    children [
      # 身体
      Solid {
        translation 0 0 0.05
        children [
          Shape {
            appearance Appearance {
              material Material {
                diffuseColor IS bodyColor
              }
            }
            geometry Box {
              size IS bodySize
            }
          }
        ]
        boundingObject Box {
          size IS bodySize
        }
        physics Physics {
          mass 1
        }
      }
      # 头部
      Solid {
        translation 0 0 0.15
        children [
          Shape {
            appearance Appearance {
              material Material {
                diffuseColor IS headColor
              }
            }
            geometry Box {
              size IS headSize
            }
          }
        ]
        boundingObject Box {
          size IS headSize
        }
        physics Physics {
          mass 0.5
        }
      }
      # 轮子(可选)
      Group {
        children [
          Solid {
            translation 0.15 0.1 0.05
            children [
              Shape {
                appearance Appearance {
                  material Material {
                    diffuseColor IS wheelColor
                  }
                }
                geometry Cylinder {
                  radius IS wheelRadius
                  height 0.02
                }
              }
            ]
            boundingObject Cylinder {
              radius IS wheelRadius
              height 0.02
            }
            physics Physics {
              mass 0.1
            }
          }
          Solid {
            translation 0.15 -0.1 0.05
            children [
              Shape {
                appearance Appearance {
                  material Material {
                    diffuseColor IS wheelColor
                  }
                }
                geometry Cylinder {
                  radius IS wheelRadius
                  height 0.02
                }
              }
            ]
            boundingObject Cylinder {
              radius IS wheelRadius
              height 0.02
            }
            physics Physics {
              mass 0.1
            }
          }
        ]
      }
    ]
  }
}

6. 重用PROTO

PROTO的主要优势是可以在多个项目中重复使用。让我们学习如何导出和导入PROTO。

📝 动手实践 #8: 创建PROTO库:
  1. 创建一个新的项目文件夹 my_proto_library
  2. 在其中创建 protos 子文件夹
  3. CustomBox.protoSimpleRobot.proto 复制到该文件夹
  4. 创建一个 README.md 文件描述你的PROTO库
  5. 将整个文件夹压缩为 my_proto_library.zip
📝 动手实践 #9: 在其他项目中使用PROTO:
  1. 创建一个新的Webots项目
  2. my_proto_library.zip 解压到项目根目录
  3. 刷新项目,PROTO应该出现在 PROTO nodes (Current Project)
  4. 实例化你的自定义PROTO
  5. 测试不同的参数设置

7. 高级PROTO功能

PROTO支持许多高级功能,包括条件语句、循环、数学表达式等。

📝 动手实践 #10: 创建带条件的PROTO:
#VRML_SIM R2025a utf8
PROTO ConditionalBox [
  field SFVec3f    size      1 1 1
  field SFColor    color     0.8 0.8 0.8
  field SFBool     isVisible TRUE
  field SFBool     hasPhysics TRUE
]
{
  Solid {
    children [
      Shape {
        appearance Appearance {
          material Material {
            diffuseColor IS color
          }
        }
        geometry Box {
          size IS size
        }
      }
    ]
    boundingObject Box {
      size IS size
    }
    physics Physics {
      mass 1
    }
  }
}
📝 动手实践 #11: 创建参数化PROTO:
#VRML_SIM R2025a utf8
PROTO ParametricTable [
  field SFFloat    tableHeight    0.7
  field SFFloat    tableWidth     0.8
  field SFFloat    tableDepth     0.6
  field SFFloat    legRadius      0.02
  field SFColor    tableColor     0.6 0.4 0.2
  field SFVec3f    translation    0 0 0
]
{
  Solid {
    translation IS translation
    children [
      # 桌面
      Solid {
        translation 0 0 { tableHeight / 2 }
        children [
          Shape {
            appearance Appearance {
              material Material {
                diffuseColor IS tableColor
              }
            }
            geometry Box {
              size { tableWidth } { tableDepth } 0.05
            }
          }
        ]
        boundingObject Box {
          size { tableWidth } { tableDepth } 0.05
        }
        physics Physics {
          mass 5
        }
      }
      # 四条腿
      Solid {
        translation { tableWidth / 2 - 0.05 } { tableDepth / 2 - 0.05 } { tableHeight / 2 }
        children [
          Shape {
            appearance Appearance {
              material Material {
                diffuseColor IS tableColor
              }
            }
            geometry Cylinder {
              radius IS legRadius
              height { tableHeight }
            }
          }
        ]
        boundingObject Cylinder {
          radius IS legRadius
          height { tableHeight }
        }
        physics Physics {
          mass 0.5
        }
      }
      # 其他三条腿(类似结构)
    ]
  }
}

8. 总结

恭喜你完成了Webots PROTO创建教程!你已经学会了:

  • 如何创建和定义PROTO文件
  • 如何设计和使用字段来参数化PROTO
  • 如何构建复杂的几何体和材质
  • 如何创建可重用的组件库
  • 如何在多个项目中共享PROTO
  • 如何使用高级PROTO功能(条件、参数化等)

这些技能将帮助你创建自己的对象库,大大提高Webots项目的开发效率。PROTO是Webots中非常强大的功能,掌握它将使你能够构建复杂的仿真环境。

下一步

你已经完成了Webots基础教程系列!现在你可以开始创建自己的复杂仿真项目,或者探索Webots的更多高级功能。