tscn.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. .. _doc_tscn_file_format:
  2. TSCN file format
  3. ================
  4. The TSCN (text scene) file format represents a single scene tree inside
  5. Godot. Unlike binary SCN files, TSCN files have the advantage of being mostly
  6. human-readable and easy for version control systems to manage.
  7. The ESCN (exported scene) file format is identical to the TSCN file format, but
  8. is used to indicate to Godot that the file has been exported from another
  9. program and should not be edited by the user from within Godot.
  10. Unlike SCN and TSCN files, during import, ESCN files are compiled to binary
  11. SCN files stored inside the ``.godot/imported/`` folder.
  12. This reduces the data size and speeds up loading, as binary formats are faster
  13. to load compared to text-based formats.
  14. To make files more compact, properties equal to the default value are not stored
  15. in scene/resource files. It is possible to write them manually, but they will be
  16. discarded when saving the file.
  17. For those looking for a complete description, the parsing is handled in the file
  18. `resource_format_text.cpp <https://github.com/godotengine/godot/blob/master/scene/resources/resource_format_text.cpp>`_
  19. in the ``ResourceFormatLoaderText`` class.
  20. .. note::
  21. The scene and resource file formats have changed significantly in Godot 4,
  22. with the introduction of string-based UIDs to replace incremental integer
  23. IDs.
  24. Mesh, skeleton and animation data is also stored differently compared to Godot 3.
  25. You can read about some of the changes in this article:
  26. `Animation data rework for 4.0 <https://godotengine.org/article/animation-data-redesign-40/>`__
  27. Scenes and resources saved with Godot 4.x contain ``format=3`` in their
  28. header, whereas Godot 3.x uses ``format=2`` instead.
  29. File structure
  30. --------------
  31. There are five main sections inside the TSCN file:
  32. 0. File descriptor
  33. 1. External resources
  34. 2. Internal resources
  35. 3. Nodes
  36. 4. Connections
  37. The file descriptor looks like ``[gd_scene load_steps=4 format=3 uid="uid://cecaux1sm7mo0"]``
  38. and should be the first entry in the file. The ``load_steps`` parameter is equal to the
  39. total amount of resources (internal and external) plus one (for the file itself).
  40. If the file has no resources, ``load_steps`` is omitted. The engine will
  41. still load the file correctly if ``load_steps`` is incorrect, but this will affect
  42. loading bars and any other piece of code relying on that value.
  43. ``uid`` is a unique string-based identifier representing the scene. This is
  44. used by the engine to track files that are moved around, even while the editor
  45. is closed. Scripts can also load UID-based resources using the ``uid://`` path
  46. prefix to avoid relying on filesystem paths. This makes it possible to move
  47. around a file in the project, but still be able to load it in scripts without
  48. having to modify the script. Godot does not use external files to keep track of
  49. IDs, which means no central metadata storage location is required within the
  50. project. See `this pull request <https://github.com/godotengine/godot/pull/50786>`__
  51. for detailed information.
  52. These sections should appear in order, but it can be hard to distinguish them.
  53. The only difference between them is the first element in the heading for all of
  54. the items in the section. For example, the heading of all external resources
  55. should start with ``[ext_resource ...]``.
  56. A TSCN file may contain single-line comments starting with a semicolon (``;``).
  57. However, comments will be discarded when saving the file using the Godot editor.
  58. Whitespace within a TSCN file is not significant (except within strings), but
  59. extraneous whitespace will be discarded when saving the file.
  60. Entries inside the file
  61. ~~~~~~~~~~~~~~~~~~~~~~~
  62. A heading looks like
  63. ``[<resource_type> key1=value1 key2=value2 key3=value3 ...]``
  64. where resource_type is one of:
  65. - ``ext_resource``
  66. - ``sub_resource``
  67. - ``node``
  68. - ``connection``
  69. Below every heading comes zero or more ``key = value`` pairs. The
  70. values can be complex datatypes such as Arrays, Transforms, Colors, and
  71. so on. For example, a Node3D looks like:
  72. ::
  73. [node name="Cube" type="Node3D"]
  74. transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 2, 3)
  75. The scene tree
  76. --------------
  77. The scene tree is made up of… nodes! The heading of each node consists of
  78. its name, parent and (most of the time) a type. For example:
  79. ``[node name="PlayerCamera" type="Camera" parent="Player/Head"]``
  80. Other valid keywords include:
  81. - ``instance``
  82. - ``instance_placeholder``
  83. - ``owner``
  84. - ``index`` (sets the order of appearance in the tree; if absent, inherited nodes will take precedence over plain ones)
  85. - ``groups``
  86. The first node in the file, which is also the scene root, must **not** have a
  87. ``parent="Path/To/Node"`` entry in its heading. All scene files should have
  88. exactly *one* scene root. If it doesn't, Godot will fail to import the file.
  89. The parent path of other nodes should be absolute, but shouldn't contain
  90. the scene root's name. If the node is a direct child of the scene root,
  91. the path should be ``"."``. Here is an example scene tree
  92. (but without any node content):
  93. ::
  94. [node name="Player" type="Node3D"] ; The scene root
  95. [node name="Arm" type="Node3D" parent="."] ; Parented to the scene root
  96. [node name="Hand" type="Node3D" parent="Arm"] ; Child of "Arm"
  97. [node name="Finger" type="Node3D" parent="Arm/Hand"] ; Child of "Hand"
  98. .. tip::
  99. To make the file structure easier to grasp, you can save a file with any
  100. given node or resource and then inspect it yourself in an external editor. You
  101. can also make incremental changes in the Godot editor, and keep an external
  102. text editor open on the ``.tscn`` or ``.tres`` file with auto-reload enabled
  103. to see what changes.
  104. Here is an example of a scene containing a RigidBody3D-based ball with
  105. collision, visuals (mesh + light) and a camera parented to the RigidBody3D:
  106. ::
  107. [gd_scene load_steps=4 format=3 uid="uid://cecaux1sm7mo0"]
  108. [sub_resource type="SphereShape3D" id="SphereShape3D_tj6p1"]
  109. [sub_resource type="SphereMesh" id="SphereMesh_4w3ye"]
  110. [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_k54se"]
  111. albedo_color = Color(1, 0.639216, 0.309804, 1)
  112. [node name="Ball" type="RigidBody3D"]
  113. [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
  114. shape = SubResource("SphereShape3D_tj6p1")
  115. [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
  116. mesh = SubResource("SphereMesh_4w3ye")
  117. surface_material_override/0 = SubResource("StandardMaterial3D_k54se")
  118. [node name="OmniLight3D" type="OmniLight3D" parent="."]
  119. light_color = Color(1, 0.698039, 0.321569, 1)
  120. omni_range = 10.0
  121. [node name="Camera3D" type="Camera3D" parent="."]
  122. transform = Transform3D(1, 0, 0, 0, 0.939693, 0.34202, 0, -0.34202, 0.939693, 0, 1, 3)
  123. NodePath
  124. ~~~~~~~~
  125. A tree structure is not enough to represent the whole scene. Godot uses a
  126. ``NodePath(Path/To/Node)`` structure to refer to another node or attribute of
  127. the node anywhere in the scene tree. Paths are relative to the current node,
  128. with ``NodePath(".")`` pointing to the current node and ``NodePath("")``
  129. pointing to no node at all.
  130. For instance, MeshInstance3D uses ``NodePath()`` to point to its skeleton.
  131. Likewise, Animation tracks use ``NodePath()`` to point to node properties to
  132. animate.
  133. NodePath can also point to a property using a ``:property_name`` suffix, and
  134. even point to a specific component for vector, transform and color types. This
  135. is used by Animation resources to point to specific properties to animate. For
  136. example, ``NodePath("MeshInstance3D:scale.x")`` points to the ``x`` component of
  137. the ``scale`` Vector3 property in MeshInstance3D.
  138. For example, the ``skeleton`` property in the MeshInstance3D node called
  139. ``mesh`` points to its parent, ``Armature01``:
  140. ::
  141. [node name="mesh" type="MeshInstance3D" parent="Armature01"]
  142. skeleton = NodePath("..")
  143. Skeleton3D
  144. ~~~~~~~~~~
  145. The :ref:`class_Skeleton3D` node inherits the Node3D node, but may also have a
  146. list of bones described in key-value pairs in the format
  147. ``bones/<id>/<attribute> = value``. The bone attributes consist of:
  148. - ``position``: Vector3
  149. - ``rotation``: Quaternion
  150. - ``scale``: Vector3
  151. These attributes are all optional. For instance, a bone may only define
  152. ``position`` or ``rotation`` without defining the other properties.
  153. Here's an example of a skeleton node with two bones:
  154. ::
  155. [node name="Skeleton3D" type="Skeleton3D" parent="PlayerModel/Robot_Skeleton" index="0"]
  156. bones/1/position = Vector3(0.114471, 2.19771, -0.197845)
  157. bones/1/rotation = Quaternion(0.191422, -0.0471201, -0.00831942, 0.980341)
  158. bones/2/position = Vector3(-2.59096e-05, 0.236002, 0.000347473)
  159. bones/2/rotation = Quaternion(-0.0580488, 0.0310587, -0.0085914, 0.997794)
  160. bones/2/scale = Vector3(0.9276, 0.9276, 0.9276)
  161. BoneAttachment3D
  162. ~~~~~~~~~~~~~~~~
  163. The :ref:`class_BoneAttachment3D` node is an intermediate node to describe some
  164. node being parented to a single bone in a Skeleton node. The BoneAttachment has
  165. a ``bone_name = "name of bone"`` property, as well as a property for the matching
  166. bone index.
  167. An example of a :ref:`class_Marker3D` node parented to a bone in Skeleton:
  168. ::
  169. [node name="GunBone" type="BoneAttachment3D" parent="PlayerModel/Robot_Skeleton/Skeleton3D" index="5"]
  170. transform = Transform3D(0.333531, 0.128981, -0.933896, 0.567174, 0.763886, 0.308015, 0.753209, -0.632331, 0.181604, -0.323915, 1.07098, 0.0497144)
  171. bone_name = "hand.R"
  172. bone_idx = 55
  173. [node name="ShootFrom" type="Marker3D" parent="PlayerModel/Robot_Skeleton/Skeleton3D/GunBone"]
  174. transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.4, 0)
  175. AnimationPlayer
  176. ~~~~~~~~~~~~~~~
  177. The :ref:`class_AnimationPlayer` node works with one or more animation libraries
  178. stored in :ref:`class_AnimationLibrary` resources. An animation library is a
  179. collection of individual :ref:`class_Animation` resources, whose structure is
  180. documented :ref:`here <doc_tscn_animation>`.
  181. This split between animations themselves and animation libraries was done in
  182. Godot 4, so that animations can be imported separately from 3D meshes, which is
  183. a common workflow in 3D animation software. See the `original pull request
  184. <https://github.com/godotengine/godot/pull/59980>`__ for details.
  185. If the library name is empty, then it acts acts the unique source of animations
  186. for this AnimationPlayer. This allows using ``<animation_name>`` directly to
  187. play animations from script. If you name the library, then you must play it as
  188. ``<library_name>/<animation_name>``. This ensures backwards compatibility and
  189. keeps the existing workflow if you don't want to use multiple animation
  190. libraries.
  191. Resources
  192. ---------
  193. Resources are components that make up the nodes. For example, a MeshInstance3D
  194. node will have an accompanying ArrayMesh resource. The ArrayMesh resource
  195. may be either internal or external to the TSCN file.
  196. References to the resources are handled by unique string-based IDs in the
  197. resource's heading. This is different from the ``uid`` property, which each
  198. external resource also has (but subresources don't).
  199. External resources and internal resources are referred to with
  200. ``ExtResource("id")`` and ``SubResource("id")``, respectively. Because there
  201. have different methods to refer to internal and external resources, you can have
  202. the same ID for both an internal and external resource.
  203. For example, to refer to the resource
  204. ``[ext_resource type="Material" uid="uid://c4cp0al3ljsjv" path="res://material.tres" id="1_7bt6s"]``,
  205. you would use ``ExtResource("1_7bt6s")``.
  206. External resources
  207. ~~~~~~~~~~~~~~~~~~
  208. External resources are links to resources not contained within the TSCN file
  209. itself. An external resource consists of a path, a type, a UID (used to map its
  210. filesystem location to a unique identifier) and an ID (used to refer to the
  211. resource in the scene file).
  212. Godot always generates absolute paths relative to the resource directory and
  213. thus prefixed with ``res://``, but paths relative to the TSCN file's location
  214. are also valid.
  215. Some example external resources are:
  216. ::
  217. [ext_resource type="Texture2D" uid="uid://ccbm14ebjmpy1" path="res://gradient.tres" id="2_eorut"]
  218. [ext_resource type="Material" uid="uid://c4cp0al3ljsjv" path="material.tres" id="1_7bt6s"]
  219. Like TSCN files, a TRES file may contain single-line comments starting with a
  220. semicolon (``;``). However, comments will be discarded when saving the resource
  221. using the Godot editor.
  222. Whitespace within a TRES file is not significant (except within strings), but
  223. extraneous whitespace will be discarded when saving the file.
  224. Internal resources
  225. ~~~~~~~~~~~~~~~~~~
  226. A TSCN file can contain meshes, materials and other data. These are contained in
  227. the *internal resources* section of the file. The heading for an internal
  228. resource looks similar to those of external resources, except that it doesn't
  229. have a path. Internal resources also have ``key=value`` pairs under each
  230. heading. For example, a capsule collision shape looks like:
  231. ::
  232. [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_fdxgg"]
  233. radius = 1.0
  234. height = 3.0
  235. Some internal resources contain links to other internal resources (such as a
  236. mesh having a material). In this case, the referring resource must appear
  237. *before* the reference to it. This means that order matters in the file's
  238. internal resources section.
  239. ArrayMesh
  240. ~~~~~~~~~
  241. An ArrayMesh consists of several surfaces contained in the ``_surfaces`` array
  242. (notice the leading underscore). Each surface's data is stored in a dictionary
  243. with the following keys:
  244. - ``aabb``: The computed axis-aligned bounding box for visibility.
  245. - ``attribute_data``: Vertex attribute data, such as normals, tangents, vertex
  246. colors, UV1, UV2 and custom vertex data.
  247. - ``bone_aabbs``: The axis-aligned bounding box of each bone for visibility.
  248. - ``format``: The surface's buffer format.
  249. - ``index_count``: The number of indices in the surface. This must match
  250. ``index_data``'s size.
  251. - ``index_data``: The index data, which determines which vertices from
  252. ``vertex_data`` are drawn.
  253. - ``lods``: Level of detail variations, stored as an array. Each LOD level
  254. represents two values in the array. The first value is the percentage of
  255. screen space the LOD level is most suited for (edge length); the second value
  256. is the list of indices that should be drawn for the given LOD level.
  257. - ``material``: The material used when drawing the surface.
  258. - ``name``: The surface's name. This can be used in scripts and is imported from
  259. 3D DCCs.
  260. - ``primitive``: The surface's primitive type, matching the ``Mesh.PrimitiveType`` Godot enum. ``0`` = points, ``1`` = lines, ``2`` = line
  261. strip, ``3`` = triangles (most common), ``4`` = triangle strip.
  262. - ``skin_data``: Bone weight data.
  263. - ``vertex_count``: Number of vertices in the surface. This must match ``vertex_data``'s size.
  264. - ``vertex_data``: The vertex position data.
  265. Here's an example of an ArrayMesh saved to its own ``.tres`` file. Some fields were shortened with ``...`` for brevity:
  266. ::
  267. [gd_resource type="ArrayMesh" load_steps=2 format=3 uid="uid://dww8o7hsqrhx5"]
  268. [ext_resource type="Material" path="res://player/model/playerobot.tres" id="1_r3bjq"]
  269. [resource]
  270. resource_name = "player_Sphere_016"
  271. _surfaces = [{
  272. "aabb": AABB(-0.207928, 1.21409, -0.14545, 0.415856, 0.226569, 0.223374),
  273. "attribute_data": PackedByteArray(63, 121, ..., 117, 63),
  274. "bone_aabbs": [AABB(0, 0, 0, -1, -1, -1), ..., AABB(-0.207928, 1.21409, -0.14545, 0.134291, 0.226569, 0.223374)],
  275. "format": 7191,
  276. "index_count": 1224,
  277. "index_data": PackedByteArray(30, 0, ..., 150, 4),
  278. "lods": [0.0382013, PackedByteArray(33, 1, ..., 150, 4)],
  279. "material": ExtResource("1_r3bjq"),
  280. "name": "playerobot",
  281. "primitive": 3,
  282. "skin_data": PackedByteArray(15, 0, ..., 0, 0),
  283. "vertex_count": 1250,
  284. "vertex_data": PackedByteArray(196, 169, ..., 11, 38)
  285. }]
  286. blend_shape_mode = 0
  287. .. _doc_tscn_animation:
  288. Animation
  289. ~~~~~~~~~
  290. Each animation has the following properties:
  291. - ``length``: The animation's length in seconds. Note that keyframes may be
  292. placed outside the ``[0; length]`` interval, but they may have no effect
  293. depending on the interpolation mode chosen.
  294. - ``loop_mode``: ``0`` = no looping, ``1`` = wrap-around looping, ``2`` =
  295. clamped looping.
  296. - ``step``: The step size to use when editing this animation in the editor.
  297. This is only used in the editor; it doesn't affect animation playback in any way.
  298. Each track is described by a list of key-value pairs in the format
  299. ``tracks/<id>/<attribute>``. Each track includes:
  300. - ``type``: The track's type. This defines what kind of properties may be
  301. animated by this track, and how it'll be exposed to the user in the editor.
  302. Valid types are ``value`` (generic property track), ``position_3d``,
  303. ``rotation_3d``, ``scale_3d``, ``blend_shape`` (optimized 3D animation
  304. tracks), ``method`` (method call tracks), ``bezier`` (Bezier curve tracks),
  305. ``audio`` (audio playback tracks), ``animation`` (tracks that play other
  306. animations).
  307. - ``imported``: ``true`` if the track was created from an imported 3D scene,
  308. ``false`` if it was manually created by the user in the Godot editor or using
  309. a script.
  310. - ``enabled``: ``true`` if the track is effective, ``false`` if it was disabled
  311. in the editor.
  312. - ``path``: Path to the node property that will be affected by the track. The
  313. property is written after the node path with a ``:`` separator.
  314. - ``interp``: The interpolation mode to use. ``0`` = nearest, ``1`` = linear,
  315. ``2`` = cubic, ``3`` = linear angle, ``4`` = cubic angle.
  316. - ``loop_wrap``: ``true`` if the track is designed to wrap around when the
  317. animation is looping, ``false`` if the track clamps to the first/last
  318. keyframes.
  319. - ``keys``: The animation track's values. This attribute's structure depends on the ``type``.
  320. Here is a scene containing an AnimationPlayer that scales down a cube over time
  321. using a generic property track. The AnimationLibrary workflow was not used, so
  322. the animation library has an empty name (but the animation is still given a
  323. ``scale_down`` name). Note that the ``RESET`` track was not created in this
  324. AnimationPlayer for brevity:
  325. ::
  326. [gd_scene load_steps=4 format=3 uid="uid://cdyt3nktp6y6"]
  327. [sub_resource type="Animation" id="Animation_r2qdp"]
  328. resource_name = "scale_down"
  329. length = 1.5
  330. loop_mode = 2
  331. step = 0.05
  332. tracks/0/type = "value"
  333. tracks/0/imported = false
  334. tracks/0/enabled = true
  335. tracks/0/path = NodePath("Box:scale")
  336. tracks/0/interp = 1
  337. tracks/0/loop_wrap = true
  338. tracks/0/keys = {
  339. "times": PackedFloat32Array(0, 1),
  340. "transitions": PackedFloat32Array(1, 1),
  341. "update": 0,
  342. "values": [Vector3(1, 1, 1), Vector3(0, 0, 0)]
  343. }
  344. [sub_resource type="AnimationLibrary" id="AnimationLibrary_4qx36"]
  345. _data = {
  346. "scale_down": SubResource("Animation_r2qdp")
  347. }
  348. [sub_resource type="BoxMesh" id="BoxMesh_u688r"]
  349. [node name="Node3D" type="Node3D"]
  350. [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
  351. autoplay = "scale_down"
  352. libraries = {
  353. "": SubResource("AnimationLibrary_4qx36")
  354. }
  355. [node name="Box" type="MeshInstance3D" parent="."]
  356. mesh = SubResource("BoxMesh_u688r")
  357. For generic property ``value`` tracks, ``keys`` is a dictionary containing 3
  358. arrays with positions in ``times`` (PackedFloat32Array), easing values in
  359. ``transitions`` (PackedFloat32Array) and values in ``values`` (Array). There is
  360. an additional ``update`` property, which is an integer with the values ``0`` =
  361. continuous, ``1`` = discrete, ``2`` = capture.
  362. Here is a second Animation resource that makes use of the 3D Position and 3D
  363. Rotation tracks. These tracks (in addition to the 3D Scale track) replace
  364. Transform tracks from Godot 3. They are optimized for fast playback and can
  365. optionally be compressed.
  366. The downside of these optimized track types is that they can't use custom easing
  367. values. Instead, all keyframes use linear interpolation. That said, you can
  368. still opt for using nearest or cubic interpolation for all keyframes in a given
  369. track by changing the track's interpolation mode.
  370. ::
  371. [sub_resource type="Animation" id="Animation_r2qdp"]
  372. resource_name = "move_and_rotate"
  373. length = 1.5
  374. loop_mode = 2
  375. step = 0.05
  376. tracks/0/type = "position_3d"
  377. tracks/0/imported = false
  378. tracks/0/enabled = true
  379. tracks/0/path = NodePath("Box")
  380. tracks/0/interp = 1
  381. tracks/0/loop_wrap = true
  382. tracks/0/keys = PackedFloat32Array(0, 1, 0, 0, 0, 1.5, 1, 1.5, 1, 0)
  383. tracks/1/type = "rotation_3d"
  384. tracks/1/imported = false
  385. tracks/1/enabled = true
  386. tracks/1/path = NodePath("Box")
  387. tracks/1/interp = 1
  388. tracks/1/loop_wrap = true
  389. tracks/1/keys = PackedFloat32Array(0, 1, 0.211, -0.047, 0.211, 0.953, 1.5, 1, 0.005, 0.976, -0.216, 0.022)
  390. For 3D position, rotation and scale tracks, ``keys`` is a PackedFloat32Array
  391. with all values stored in a sequence.
  392. In the visual guide below, ``T`` is the keyframe's time in seconds since the
  393. start of the animation, ``E`` is the keyframe's transition (currently always
  394. ``1``). For 3D position and scale tracks, ``X``, ``Y``, ``Z`` are the Vector3's
  395. coordinates. For 3D rotation tracks, ``X``, ``Y``, ``Z`` and ``W`` are the
  396. Quaternion's coordinates.
  397. ::
  398. # For 3D position and scale, which use Vector3:
  399. tracks/<id>/keys = PackedFloat32Array(T, E, X, Y, Z, T, E, X, Y, Z, ...)
  400. # For 3D rotation, which use Quaternion:
  401. tracks/<id>/keys = PackedFloat32Array(T, E, X, Y, Z, W, T, E, X, Y, Z, W, ...)