tscn.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. :article_outdated: True
  2. .. _doc_tscn_file_format:
  3. TSCN file format
  4. ================
  5. The TSCN (text scene) file format represents a single scene tree inside
  6. Godot. Unlike binary SCN files, TSCN files have the advantage of being mostly
  7. human-readable and easy for version control systems to manage.
  8. The ESCN (exported scene) file format is identical to the TSCN file format, but
  9. is used to indicate to Godot that the file has been exported from another
  10. program and should not be edited by the user from within Godot.
  11. Unlike SCN and TSCN files, during import, ESCN files are compiled to binary
  12. SCN files stored inside the ``.godot/imported/`` folder.
  13. This reduces the data size and speeds up loading, as binary formats are faster
  14. to load compared to text-based formats.
  15. For those looking for a complete description, the parsing is handled in the file
  16. `resource_format_text.cpp <https://github.com/godotengine/godot/blob/master/scene/resources/resource_format_text.cpp>`_
  17. in the ``ResourceFormatLoaderText`` class.
  18. File structure
  19. --------------
  20. There are five main sections inside the TSCN file:
  21. 0. File Descriptor
  22. 1. External resources
  23. 2. Internal resources
  24. 3. Nodes
  25. 4. Connections
  26. The file descriptor looks like ``[gd_scene load_steps=3 format=2]`` and should
  27. be the first entry in the file. The ``load_steps`` parameter is equal to the
  28. total amount of resources (internal and external) plus one (for the file itself).
  29. If the file has no resources, ``load_steps`` is omitted. The engine will
  30. still load the file correctly if ``load_steps`` is incorrect, but this will affect
  31. loading bars and any other piece of code relying on that value.
  32. These sections should appear in order, but it can be hard to distinguish them.
  33. The only difference between them is the first element in the heading for all of
  34. the items in the section. For example, the heading of all external resources
  35. should start with ``[ext_resource .....]``.
  36. A TSCN file may contain single-line comments starting with a semicolon (``;``).
  37. However, comments will be discarded when saving the file using the Godot editor.
  38. Entries inside the file
  39. ~~~~~~~~~~~~~~~~~~~~~~~
  40. A heading looks like
  41. ``[<resource_type> key=value key=value key=value ...]``
  42. where resource_type is one of:
  43. - ``ext_resource``
  44. - ``sub_resource``
  45. - ``node``
  46. - ``connection``
  47. Below every heading comes zero or more ``key = value`` pairs. The
  48. values can be complex datatypes such as Arrays, Transforms, Colors, and
  49. so on. For example, a Node3D looks like:
  50. ::
  51. [node name="Cube" type="Node3D" parent="."]
  52. transform=Transform( 1.0, 0.0, 0.0 ,0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 )
  53. The scene tree
  54. --------------
  55. The scene tree is made up of… nodes! The heading of each node consists of
  56. its name, parent and (most of the time) a type. For example
  57. ``[node type="Camera" name="PlayerCamera" parent="Player/Head"]``
  58. Other valid keywords include:
  59. - ``instance``
  60. - ``instance_placeholder``
  61. - ``owner``
  62. - ``index`` (sets the order of appearance in the tree. If absent, inherited nodes will take precedence over plain ones)
  63. - ``groups``
  64. The first node in the file, which is also the scene root, must not have a
  65. ``parent=Path/To/Node`` entry in its heading. All scene files should have
  66. exactly *one* scene root. If it doesn't, Godot will fail to import the file.
  67. The parent path of other nodes should be absolute, but shouldn't contain
  68. the scene root's name. If the node is a direct child of the scene root,
  69. the path should be ``"."``. Here is an example scene tree
  70. (but without any node content):
  71. ::
  72. [node name="Player" type="Node3D"] ; The scene root
  73. [node name="Arm" parent="." type="Node3D"] ; Parented to the scene root
  74. [node name="Hand" parent="Arm" type="Node3D"]
  75. [node name="Finger" parent="Arm/Hand" type="Node3D"]
  76. Similar to the internal resource, the document for each node is currently
  77. incomplete. Fortunately, it is easy to find out because you can simply
  78. save a file with that node in it. Some example nodes are:
  79. ::
  80. [node type="CollisionShape" name="SphereCollision" parent="SpherePhysics"]
  81. shape = SubResource(8)
  82. transform = Transform( 1.0 , 0.0 , -0.0 , 0.0 , -4.371138828673793e-08 , 1.0 , -0.0 , -1.0 , -4.371138828673793e-08 ,0.0 ,0.0 ,-0.0 )
  83. [node type="MeshInstance3D" name="Sphere" parent="SpherePhysics"]
  84. mesh = SubResource(9)
  85. transform = Transform( 1.0 , 0.0 , -0.0 , 0.0 , 1.0 , -0.0 , -0.0 , -0.0 , 1.0 ,0.0 ,0.0 ,-0.0 )
  86. [node type="OmniLight" name="Lamp" parent="."]
  87. light_energy = 1.0
  88. light_specular = 1.0
  89. transform = Transform( -0.29086464643478394 , -0.7711008191108704 , 0.5663931369781494 , -0.05518905818462372 , 0.6045246720314026 , 0.7946722507476807 , -0.9551711678504944 , 0.199883371591568 , -0.21839118003845215 ,4.076245307922363 ,7.3235554695129395 ,-1.0054539442062378 )
  90. omni_range = 30
  91. shadow_enabled = true
  92. light_negative = false
  93. light_color = Color( 1.0, 1.0, 1.0, 1.0 )
  94. [node type="Camera" name="Camera" parent="."]
  95. projection = 0
  96. near = 0.10000000149011612
  97. fov = 50
  98. transform = Transform( 0.6859206557273865 , -0.32401350140571594 , 0.6515582203865051 , 0.0 , 0.8953956365585327 , 0.44527143239974976 , -0.7276763319969177 , -0.3054208755493164 , 0.6141703724861145 ,14.430776596069336 ,10.093015670776367 ,13.058500289916992 )
  99. far = 100.0
  100. NodePath
  101. ~~~~~~~~
  102. A tree structure is not enough to represent the whole scene. Godot uses a
  103. ``NodePath(Path/To/Node)`` structure to refer to another node or attribute of
  104. the node anywhere in the scene tree. For instance, MeshInstance3D uses
  105. ``NodePath()`` to point to its skeleton. Likewise, Animation tracks use
  106. ``NodePath()`` to point to node properties to animate.
  107. ::
  108. [node name="mesh" type="MeshInstance3D" parent="Armature001"]
  109. mesh = SubResource(1)
  110. skeleton = NodePath("..:")
  111. ::
  112. [sub_resource id=3 type="Animation"]
  113. ...
  114. tracks/0/type = "transform"
  115. tracks/0/path = NodePath("Cube:")
  116. ...
  117. Skeleton
  118. ~~~~~~~~
  119. The Skeleton node inherits the Node3D node, but also may have a list of bones
  120. described in key-value pairs in the format ``bones/Id/Attribute=Value``. The
  121. bone attributes consist of:
  122. - ``name``
  123. - ``parent``
  124. - ``rest``
  125. - ``pose``
  126. - ``enabled``
  127. - ``bound_children``
  128. 1. ``name`` must be the first attribute of each bone.
  129. 2. ``parent`` is the index of parent bone in the bone list, with parent index,
  130. the bone list is built to a bone tree.
  131. 3. ``rest`` is the transform matrix of bone in its "resting" position.
  132. 4. ``pose`` is the pose matrix; use ``rest`` as the basis.
  133. 5. ``bound_children`` is a list of ``NodePath()`` which point to
  134. BoneAttachments belonging to this bone.
  135. Here's an example of a skeleton node with two bones:
  136. ::
  137. [node name="Skeleton" type="Skeleton" parent="Armature001" index="0"]
  138. bones/0/name = "Bone.001"
  139. bones/0/parent = -1
  140. bones/0/rest = Transform( 1, 0, 0, 0, 0, -1, 0, 1, 0, 0.038694, 0.252999, 0.0877164 )
  141. bones/0/pose = Transform( 1.0, 0.0, -0.0, 0.0, 1.0, -0.0, -0.0, -0.0, 1.0, 0.0, 0.0, -0.0 )
  142. bones/0/enabled = true
  143. bones/0/bound_children = [ ]
  144. bones/1/name = "Bone.002"
  145. bones/1/parent = 0
  146. bones/1/rest = Transform( 0.0349042, 0.99939, 0.000512929, -0.721447, 0.0248417, 0.692024, 0.691589, -0.0245245, 0.721874, 0, 5.96046e-08, -1.22688 )
  147. bones/1/pose = Transform( 1.0, 0.0, -0.0, 0.0, 1.0, -0.0, -0.0, -0.0, 1.0, 0.0, 0.0, -0.0 )
  148. bones/1/enabled = true
  149. bones/1/bound_children = [ ]
  150. BoneAttachment
  151. ~~~~~~~~~~~~~~
  152. BoneAttachment node is an intermediate node to describe some node being parented
  153. to a single bone in a Skeleton node. The BoneAttachment has a
  154. ``bone_name=NameOfBone`` attribute, and the corresponding bone being the parent has the
  155. BoneAttachment node in its ``bound_children`` list.
  156. An example of one MeshInstance3D parented to a bone in Skeleton:
  157. ::
  158. [node name="Armature" type="Skeleton" parent="."]
  159. transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, -0.0219986, 0.0125825, 0.0343127)
  160. bones/0/name = "Bone"
  161. bones/0/parent = -1
  162. bones/0/rest = Transform(1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0)
  163. bones/0/pose = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
  164. bones/0/enabled = true
  165. bones/0/bound_children = [NodePath("BoneAttachment:")]
  166. [node name="BoneAttachment" type="BoneAttachment" parent="Armature"]
  167. bone_name = "Bone"
  168. [node name="Cylinder" type="MeshInstance3D" parent="Armature/BoneAttachment"]
  169. mesh = SubResource(1)
  170. transform = Transform(1.0, 0.0, 0.0, 0.0, 1.86265e-09, 1.0, 0.0, -1.0, 0.0, 0.0219986, -0.0343127, 2.25595)
  171. AnimationPlayer
  172. ~~~~~~~~~~~~~~~
  173. AnimationPlayer works as an animation library. It stores animations listed in
  174. the format ``anim/Name=SubResource(ResourceId)``; each line refers to an
  175. Animation resource. All the animation resources use the root node of
  176. AnimationPlayer. The root node is stored as
  177. ``root_node=NodePath(Path/To/Node)``.
  178. ::
  179. [node name="AnimationPlayer" type="AnimationPlayer" parent="." index="1"]
  180. root_node = NodePath("..")
  181. autoplay = ""
  182. playback_process_mode = 1
  183. playback_default_blend_time = 0.0
  184. playback_speed = 1.0
  185. anims/default = SubResource( 2 )
  186. blend_times = [ ]
  187. Resources
  188. ---------
  189. Resources are components that make up the nodes. For example, a MeshInstance3D
  190. node will have an accompanying ArrayMesh resource. The ArrayMesh resource
  191. may be either internal or external to the TSCN file.
  192. References to the resources are handled by ``id`` numbers in the resource's
  193. heading. External resources and internal resources are referred to with
  194. ``ExtResource(id)`` and ``SubResource(id)``, respectively. Because there
  195. have different methods to refer to internal and external resources, you can have
  196. the same ID for both an internal and external resource.
  197. For example, to refer to the resource ``[ext_resource id=3 type="PackedScene"
  198. path=....]``, you would use ``ExtResource(3)``.
  199. External resources
  200. ~~~~~~~~~~~~~~~~~~
  201. External resources are links to resources not contained within the TSCN file
  202. itself. An external resource consists of a path, a type and an ID.
  203. Godot always generates absolute paths relative to the resource directory and
  204. thus prefixed with ``res://``, but paths relative to the TSCN file's location
  205. are also valid.
  206. Some example external resources are:
  207. ::
  208. [ext_resource path="res://characters/player.dae" type="PackedScene" id=1]
  209. [ext_resource path="metal.tres" type="Material" id=2]
  210. Like TSCN files, a TRES file may contain single-line comments starting with a
  211. semicolon (``;``). However, comments will be discarded when saving the resource
  212. using the Godot editor.
  213. Internal resources
  214. ~~~~~~~~~~~~~~~~~~
  215. A TSCN file can contain meshes, materials and other data. These are contained in
  216. the *internal resources* section of the file. The heading for an internal
  217. resource looks similar to those of external resources, except that it doesn't
  218. have a path. Internal resources also have ``key=value`` pairs under each
  219. heading. For example, a capsule collision shape looks like:
  220. ::
  221. [sub_resource type="CapsuleShape" id=2]
  222. radius = 0.5
  223. height = 3.0
  224. Some internal resources contain links to other internal resources (such as a
  225. mesh having a material). In this case, the referring resource must appear
  226. *before* the reference to it. This means that order matters in the file's
  227. internal resources section.
  228. Unfortunately, documentation on the formats for these subresources isn't
  229. complete. Some examples can be found by inspecting saved resource files, but
  230. others can only be found by looking through Godot's source.
  231. ArrayMesh
  232. ~~~~~~~~~
  233. ArrayMesh consists of several surfaces, each in the format ``surface\Index={}``.
  234. Each surface is a set of vertices and a material.
  235. TSCN files support two surface formats:
  236. 1. For the old format, each surface has three essential keys:
  237. - ``primitive``
  238. - ``arrays``
  239. - ``morph_arrays``
  240. i. ``primitive`` is an enumerate variable, ``primitive=4`` which is
  241. ``PRIMITIVE_TRIANGLES`` is frequently used.
  242. ii. ``arrays`` is a two-dimensional array, it contains:
  243. 1. Vertex positions array
  244. 2. Normals array
  245. 3. Tangents array
  246. 4. Vertex colors array
  247. 5. UV array 1
  248. 6. UV array 2
  249. 7. Bone indexes array
  250. 8. Bone weights array
  251. 9. Vertex indexes array
  252. iii. ``morph_arrays`` is an array of morphs. Each morph is exactly an
  253. ``arrays`` without the vertex indexes array.
  254. An example of ArrayMesh:
  255. ::
  256. [sub_resource id=1 type="ArrayMesh"]
  257. surfaces/0 = {
  258. "primitive":4,
  259. "arrays":[
  260. Vector3Array(0.0, 1.0, -1.0, 0.866025, -1.0, -0.5, 0.0, -1.0, -1.0, 0.866025, 1.0, -0.5, 0.866025, -1.0, 0.5, 0.866025, 1.0, 0.5, -8.74228e-08, -1.0, 1.0, -8.74228e-08, 1.0, 1.0, -0.866025, -1.0, 0.5, -0.866025, 1.0, 0.5, -0.866025, -1.0, -0.5, -0.866025, 1.0, -0.5),
  261. Vector3Array(0.0, 0.609973, -0.792383, 0.686239, -0.609973, -0.396191, 0.0, -0.609973, -0.792383, 0.686239, 0.609973, -0.396191, 0.686239, -0.609973, 0.396191, 0.686239, 0.609973, 0.396191, 0.0, -0.609973, 0.792383, 0.0, 0.609973, 0.792383, -0.686239, -0.609973, 0.396191, -0.686239, 0.609973, 0.396191, -0.686239, -0.609973, -0.396191, -0.686239, 0.609973, -0.396191),
  262. null, ; No Tangents,
  263. null, ; no Vertex Colors,
  264. null, ; No UV1,
  265. null, ; No UV2,
  266. null, ; No Bones,
  267. null, ; No Weights,
  268. IntArray(0, 2, 1, 3, 1, 4, 5, 4, 6, 7, 6, 8, 0, 5, 9, 9, 8, 10, 11, 10, 2, 1, 10, 8, 0, 1, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 5, 0, 3, 0, 9, 11, 9, 5, 7, 9, 10, 11, 11, 2, 0, 10, 1, 2, 1, 6, 4, 6, 1, 8)
  269. ],
  270. "morph_arrays":[]
  271. }
  272. Animation
  273. ~~~~~~~~~
  274. An animation resource consists of tracks. Besides, it has ``length``, ``loop``
  275. and ``step`` applied to all the tracks.
  276. 1. ``length`` and ``step`` are both durations in seconds.
  277. Each track is described by a list of key-value pairs in the format
  278. ``tracks/Id/Attribute``. Each track includes:
  279. - ``type``
  280. - ``path``
  281. - ``interp``
  282. - ``keys``
  283. - ``loop_wrap``
  284. - ``imported``
  285. - ``enabled``
  286. 1. The ``type`` must be the first attribute of each track.
  287. The value of ``type`` can be:
  288. - ``transform``
  289. - ``value``
  290. - ``method``
  291. 2. The ``path`` has the format ``NodePath(Path/To/Node:attribute)``.
  292. It's the path to the animated node or attribute, relative to the root node
  293. defined in the AnimationPlayer.
  294. 3. The ``interp`` is the method to interpolate frames from the keyframes.
  295. It is an enum variable with one of the following values:
  296. - ``0`` (constant)
  297. - ``1`` (linear)
  298. - ``2`` (cubic)
  299. 4. The ``keys`` correspond to the keyframes. It appears as a ``PackedFloat32Array()``,
  300. but may have a different structure for tracks with different types.
  301. - A Transform track uses every 12 real numbers in the ``keys`` to describe
  302. a keyframe. The first number is the timestamp. The second number is the
  303. transition followed by a 3-number translation vector, followed by a
  304. 4-number rotation quaternion (X, Y, Z, W) and finally a 3-number
  305. scale vector. The default transition in a Transform track is 1.0.
  306. ::
  307. [sub_resource type="Animation" id=2]
  308. length = 4.95833
  309. loop = false
  310. step = 0.1
  311. tracks/0/type = "transform"
  312. tracks/0/path = NodePath("Armature001")
  313. tracks/0/interp = 1
  314. tracks/0/loop_wrap = true
  315. tracks/0/imported = true
  316. tracks/0/enabled = true
  317. tracks/0/keys = PackedFloat32Array( 0, 1, -0.0358698, -0.829927, 0.444204, 0, 0, 0, 1, 0.815074, 0.815074, 0.815074, 4.95833, 1, -0.0358698, -0.829927, 0.444204, 0, 0, 0, 1, 0.815074, 0.815074, 0.815074 )
  318. tracks/1/type = "transform"
  319. tracks/1/path = NodePath("Armature001/Skeleton:Bone.001")
  320. tracks/1/interp = 1
  321. tracks/1/loop_wrap = true
  322. tracks/1/imported = true
  323. tracks/1/enabled = false
  324. tracks/1/keys = PackedFloat32Array( 0, 1, 0, 5.96046e-08, 0, 0, 0, 0, 1, 1, 1, 1, 4.95833, 1, 0, 5.96046e-08, 0, 0, 0, 0, 1, 1, 1, 1 )