tscn.rst 15 KB

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