tscn.rst 15 KB

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