Class reference
GLTFDocument
Inherits Resource
Class for importing and exporting glTF files in and out of Redot.
Description
GLTFDocument supports reading data from a glTF file, buffer, or Redot scene. This data can then be written to the filesystem, buffer, or used to create a Redot scene. All of the data in a glTF scene is stored in the GLTFState class. GLTFDocument processes state objects, but does not contain any scene data itself. GLTFDocument has member variables to store export configuration settings such as the image format, but is otherwise stateless. Multiple scenes can be processed with the same settings using the same GLTFDocument object and different GLTFState objects. GLTFDocument can be extended with arbitrary functionality by extending the GLTFDocumentExtension class and registering it with GLTFDocument via register_gltf_document_extension(). This allows for custom data to be imported and exported.
Properties
String fallback_image_format = "None"
String fallback_image_format = "None"The user-friendly name of the fallback image format. This is used when exporting the glTF file, including writing to a file and writing to a byte array. This property may only be one of "None", "PNG", or "JPEG", and is only used when the image_format is not one of "None", "PNG", or "JPEG". If having multiple extension image formats is desired, that can be done using a GLTFDocumentExtension class - this property only covers the use case of providing a base glTF fallback image when using a custom image format.
float fallback_image_quality = 0.25
float fallback_image_quality = 0.25The quality of the fallback image, if any. For PNG files, this downscales the image on both dimensions by this factor. For JPEG files, this is the lossy quality of the image. A low value is recommended, since including multiple high quality images in a glTF file defeats the file size gains of using a more efficient image format.
String image_format = "PNG"
String image_format = "PNG"The user-friendly name of the export image format. This is used when exporting the glTF file, including writing to a file and writing to a byte array. By default, Redot allows the following options: "None", "PNG", "JPEG", "Lossless WebP", and "Lossy WebP". Support for more image formats can be added in GLTFDocumentExtension classes. A single extension class can provide multiple options for the specific format to use, or even an option that uses multiple formats at once.
float lossy_quality = 0.75
float lossy_quality = 0.75If image_format is a lossy image format, this determines the lossy quality of the image. On a range of 0.0 to 1.0, where 0.0 is the lowest quality and 1.0 is the highest quality. A lossy quality of 1.0 is not the same as lossless.
int root_node_mode = 0
int root_node_mode = 0How to process the root node during export. The default and recommended value is ROOT_NODE_MODE_SINGLE_ROOT. Note: Regardless of how the glTF file is exported, when importing, the root node type and name can be overridden in the scene import settings tab.
int visibility_mode = 0
int visibility_mode = 0How to deal with node visibility during export. This setting does nothing if all nodes are visible. The default and recommended value is VISIBILITY_MODE_INCLUDE_REQUIRED, which uses the KHR_node_visibility extension.
Methods
int append_from_buffer(PackedByteArray bytes, String base_path, GLTFState state, int flags = 0)
int append_from_buffer(PackedByteArray bytes, String base_path, GLTFState state, int flags = 0)Takes a PackedByteArray defining a glTF and imports the data to the given GLTFState object through the state parameter. Note: The base_path tells append_from_buffer() where to find dependencies and can be empty.
int append_from_file(String path, GLTFState state, int flags = 0, String base_path = "")
int append_from_file(String path, GLTFState state, int flags = 0, String base_path = "")Takes a path to a glTF file and imports the data at that file path to the given GLTFState object through the state parameter. Note: The base_path tells append_from_file() where to find dependencies and can be empty.
int append_from_scene(Node node, GLTFState state, int flags = 0)
int append_from_scene(Node node, GLTFState state, int flags = 0)Takes a Redot Engine scene node and exports it and its descendants to the given GLTFState object through the state parameter.
GLTFObjectModelProperty export_object_model_property(GLTFState state, NodePath node_path, Node godot_node, int gltf_node_index) static
GLTFObjectModelProperty export_object_model_property(GLTFState state, NodePath node_path, Node godot_node, int gltf_node_index) staticDetermines a mapping between the given Redot node_path and the corresponding glTF Object Model JSON pointer(s) in the generated glTF file. The details of this mapping are returned in a GLTFObjectModelProperty object. Additional mappings can be supplied via the GLTFDocumentExtension._import_object_model_property() callback method.
PackedByteArray generate_buffer(GLTFState state)
PackedByteArray generate_buffer(GLTFState state)Takes a GLTFState object through the state parameter and returns a glTF PackedByteArray.
Node generate_scene(GLTFState state, float bake_fps = 30, bool trimming = false, bool remove_immutable_tracks = true)
Node generate_scene(GLTFState state, float bake_fps = 30, bool trimming = false, bool remove_immutable_tracks = true)Takes a GLTFState object through the state parameter and returns a Redot Engine scene node. The bake_fps parameter overrides the bake_fps in state.
PackedStringArray get_supported_gltf_extensions() static
PackedStringArray get_supported_gltf_extensions() staticReturns a list of all support glTF extensions, including extensions supported directly by the engine, and extensions supported by user plugins registering GLTFDocumentExtension classes. Note: If this method is run before a GLTFDocumentExtension is registered, its extensions won't be included in the list. Be sure to only run this method after all extensions are registered. If you run this when the engine starts, consider waiting a frame before calling this method to ensure all extensions are registered.
GLTFObjectModelProperty import_object_model_property(GLTFState state, String json_pointer) static
GLTFObjectModelProperty import_object_model_property(GLTFState state, String json_pointer) staticDetermines a mapping between the given glTF Object Model json_pointer and the corresponding Redot node path(s) in the generated Redot scene. The details of this mapping are returned in a GLTFObjectModelProperty object. Additional mappings can be supplied via the GLTFDocumentExtension._export_object_model_property() callback method.
void register_gltf_document_extension(GLTFDocumentExtension extension, bool first_priority = false) static
GLTFDocumentExtension extension, bool first_priority = false) staticRegisters the given GLTFDocumentExtension instance with GLTFDocument. If first_priority is true, this extension will be run first. Otherwise, it will be run last. Note: Like GLTFDocument itself, all GLTFDocumentExtension classes must be stateless in order to function properly. If you need to store data, use the set_additional_data and get_additional_data methods in GLTFState or GLTFNode.
void unregister_gltf_document_extension(GLTFDocumentExtension extension) static
GLTFDocumentExtension extension) staticUnregisters the given GLTFDocumentExtension instance.
int write_to_filesystem(GLTFState state, String path)
int write_to_filesystem(GLTFState state, String path)Takes a GLTFState object through the state parameter and writes a glTF file to the filesystem. Note: The extension of the glTF file determines if it is a .glb binary file or a .gltf text file.
Constants
ROOT_NODE_MODE_SINGLE_ROOT = 0
Treat the Redot scene's root node as the root node of the glTF file, and mark it as the single root node via the GODOT_single_root glTF extension. This will be parsed the same as ROOT_NODE_MODE_KEEP_ROOT if the implementation does not support GODOT_single_root.
ROOT_NODE_MODE_KEEP_ROOT = 1
Treat the Redot scene's root node as the root node of the glTF file, but do not mark it as anything special. An extra root node will be generated when importing into Redot. This uses only vanilla glTF features. This is equivalent to the behavior in Redot 4.1 and earlier.
ROOT_NODE_MODE_MULTI_ROOT = 2
Treat the Redot scene's root node as the name of the glTF scene, and add all of its children as root nodes of the glTF file. This uses only vanilla glTF features. This avoids an extra root node, but only the name of the Redot scene's root node will be preserved, as it will not be saved as a node.
VISIBILITY_MODE_INCLUDE_REQUIRED = 0
If the scene contains any non-visible nodes, include them, mark them as non-visible with KHR_node_visibility, and require that importers respect their non-visibility. Downside: If the importer does not support KHR_node_visibility, the file cannot be imported.
VISIBILITY_MODE_INCLUDE_OPTIONAL = 1
If the scene contains any non-visible nodes, include them, mark them as non-visible with KHR_node_visibility, and do not impose any requirements on importers. Downside: If the importer does not support KHR_node_visibility, invisible objects will be visible.
VISIBILITY_MODE_EXCLUDE = 2
If the scene contains any non-visible nodes, do not include them in the export. This is the same as the behavior in Redot 4.4 and earlier. Downside: Invisible nodes will not exist in the exported file.