本教程将教你如何创建自定义的PROTO节点。PROTO是Webots中用于创建可重用组件的强大工具。你将学习如何定义PROTO文件、创建字段、构建复杂的几何体,以及如何在多个项目中重复使用你的自定义组件。本教程基于官方教程7。
在开始本教程之前,请确保你已经完成了前面的教程,并且熟悉基本的Webots操作。我们将创建一个新的项目来学习PROTO的创建和使用。
proto_tutorial
。在场景中添加一个 RectangleArena
作为基础环境。确保仿真处于暂停状态,虚拟时间计数器显示 0:00:00:000。
PROTO(Protocol)是Webots中用于创建可重用组件的机制。PROTO文件定义了自定义节点的结构,包括几何体、材质、物理属性等。通过PROTO,你可以创建自己的对象库并在多个项目中重复使用。
protos
文件夹.proto
扩展名让我们从创建一个简单的PROTO开始。我们将创建一个自定义的立方体PROTO,它可以设置大小和颜色。
protos
文件夹中创建文件 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
}
}
}
字段是PROTO的核心特性,它们允许你在实例化PROTO时自定义其属性。Webots支持多种字段类型,包括基本类型和复杂类型。
SFBool
:布尔值(TRUE/FALSE)SFInt32
:32位整数SFFloat
:浮点数SFString
:字符串SFVec2f
:2D向量SFVec3f
:3D向量SFRotation
:旋转SFColor
:颜色MFNode
:节点数组CustomBox.proto
文件size
为 2 1 0.5
color
为 1 0 0
(红色)translation
为 0 0 1
现在让我们创建一个更复杂的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
}
}
]
}
]
}
}
PROTO的主要优势是可以在多个项目中重复使用。让我们学习如何导出和导入PROTO。
my_proto_library
protos
子文件夹CustomBox.proto
和 SimpleRobot.proto
复制到该文件夹README.md
文件描述你的PROTO库my_proto_library.zip
my_proto_library.zip
解压到项目根目录PROTO nodes (Current Project)
中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
}
}
}
#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
}
}
# 其他三条腿(类似结构)
]
}
}
恭喜你完成了Webots PROTO创建教程!你已经学会了:
这些技能将帮助你创建自己的对象库,大大提高Webots项目的开发效率。PROTO是Webots中非常强大的功能,掌握它将使你能够构建复杂的仿真环境。
你已经完成了Webots基础教程系列!现在你可以开始创建自己的复杂仿真项目,或者探索Webots的更多高级功能。