tscn.rst 16 KB

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