Skip to content

Commit 5aed9b3

Browse files
committed
GLTF: Add export_get_property_list to GLTFDocumentExtension
1 parent ab6b37d commit 5aed9b3

4 files changed

Lines changed: 64 additions & 23 deletions

File tree

modules/gltf/doc_classes/GLTFDocumentExtension.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
Runs when converting the data from a Godot scene node. This method can be used to process the Godot scene node data into a format that can be used by [method _export_node].
2323
</description>
2424
</method>
25+
<method name="_export_get_property_list" qualifiers="virtual">
26+
<return type="Dictionary[]" />
27+
<param index="0" name="root_node" type="Node" />
28+
<description>
29+
Runs prior to the export process. This method is run before [method _export_preflight] when exporting a scene from the editor, or it may not be run at all in other situations.
30+
Unlike the rest of the export methods, this does not run when calling a [GLTFDocument]'s export methods in sequence with everything else, but rather runs before that entire process occurs, allowing configuration to occur beforehand, potentially minutes or hours in advance of [method _export_preflight]. This allows extensions to decide which properties to show in the editor export settings dialog based on the contents of the scene, hiding any settings that are not relevant for that scene. The [param root_node] parameter may be [code]null[/code], in which case all properties should be shown.
31+
</description>
32+
</method>
2533
<method name="_export_node" qualifiers="virtual">
2634
<return type="int" enum="Error" />
2735
<param index="0" name="state" type="GLTFState" />

modules/gltf/editor/editor_scene_exporter_gltf_settings.cpp

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/object/callable_mp.h"
3434
#include "core/object/class_db.h"
35+
#include "core/object/script_language.h"
3536

3637
const uint32_t PROP_EDITOR_SCRIPT_VAR = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE;
3738

@@ -155,8 +156,15 @@ String get_friendly_config_prefix(Ref<GLTFDocumentExtension> p_extension) {
155156
if (!config_prefix.is_empty()) {
156157
return config_prefix;
157158
}
159+
const Ref<Script> script = p_extension->get_script();
160+
if (script.is_valid()) {
161+
config_prefix = String(script->get_global_name()).trim_prefix("GLTFDocumentExtension").trim_suffix("GLTFDocumentExtension");
162+
if (!config_prefix.is_empty()) {
163+
return config_prefix;
164+
}
165+
}
158166
const String class_name = p_extension->get_class_name();
159-
config_prefix = class_name.trim_prefix("GLTFDocumentExtension").trim_suffix("GLTFDocumentExtension").capitalize();
167+
config_prefix = class_name.trim_prefix("GLTFDocumentExtension").trim_suffix("GLTFDocumentExtension");
160168
if (!config_prefix.is_empty()) {
161169
return config_prefix;
162170
}
@@ -187,33 +195,12 @@ void EditorSceneExporterGLTFSettings::generate_property_list(Ref<GLTFDocument> p
187195
_property_list.clear();
188196
_document = p_document;
189197
String image_format_hint_string = "None,PNG,JPEG";
190-
// Add properties from all document extensions.
198+
// If an extension allows saving images in different formats, add to the enum.
191199
for (Ref<GLTFDocumentExtension> &extension : GLTFDocument::get_all_gltf_document_extensions()) {
192-
const Callable on_prop_changed = callable_mp(this, &EditorSceneExporterGLTFSettings::_on_extension_property_list_changed);
193-
if (!extension->is_connected(CoreStringName(property_list_changed), on_prop_changed)) {
194-
extension->connect(CoreStringName(property_list_changed), on_prop_changed);
195-
}
196-
const String config_prefix = get_friendly_config_prefix(extension);
197-
_config_name_to_extension_map[config_prefix] = extension;
198-
// If the extension allows saving in different image formats, add to the enum.
199200
PackedStringArray saveable_image_formats = extension->get_saveable_image_formats();
200201
for (int i = 0; i < saveable_image_formats.size(); i++) {
201202
image_format_hint_string += "," + saveable_image_formats[i];
202203
}
203-
// Look through the extension's properties and find the relevant ones.
204-
List<PropertyInfo> ext_prop_list;
205-
extension->get_property_list(&ext_prop_list);
206-
for (const PropertyInfo &prop : ext_prop_list) {
207-
// We only want properties that will show up in the exporter
208-
// settings list. Exclude Resource's properties, as they are
209-
// not relevant to the exporter. Include any user-defined script
210-
// variables exposed to the editor (PROP_EDITOR_SCRIPT_VAR).
211-
if ((prop.usage & PROP_EDITOR_SCRIPT_VAR) == PROP_EDITOR_SCRIPT_VAR) {
212-
PropertyInfo ext_prop = prop;
213-
ext_prop.name = config_prefix + "/" + prop.name;
214-
_property_list.push_back(ext_prop);
215-
}
216-
}
217204
}
218205
// Add top-level properties (in addition to what _bind_methods registers).
219206
PropertyInfo image_format_prop = PropertyInfo(Variant::STRING, "image_format", PROPERTY_HINT_ENUM, image_format_hint_string);
@@ -231,6 +218,36 @@ void EditorSceneExporterGLTFSettings::generate_property_list(Ref<GLTFDocument> p
231218
PropertyInfo visibility_mode_prop = PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Include & Required,Include & Optional,Exclude");
232219
_property_list.push_back(visibility_mode_prop);
233220
}
221+
// Now that the above code set up base glTF stuff, add properties from all document extensions.
222+
for (Ref<GLTFDocumentExtension> &extension : GLTFDocument::get_all_gltf_document_extensions()) {
223+
// Set up to listen for property changes.
224+
const Callable on_prop_changed = callable_mp(this, &EditorSceneExporterGLTFSettings::_on_extension_property_list_changed);
225+
if (!extension->is_connected(CoreStringName(property_list_changed), on_prop_changed)) {
226+
extension->connect(CoreStringName(property_list_changed), on_prop_changed);
227+
}
228+
const String config_prefix = get_friendly_config_prefix(extension);
229+
_config_name_to_extension_map[config_prefix] = extension;
230+
// Look through the extension's properties and find the relevant ones.
231+
List<PropertyInfo> export_prop_list = extension->export_get_property_list(p_root);
232+
for (const PropertyInfo &prop : export_prop_list) {
233+
PropertyInfo ext_prop = prop;
234+
ext_prop.name = config_prefix + "/" + prop.name;
235+
_property_list.push_back(ext_prop);
236+
}
237+
List<PropertyInfo> ext_prop_list;
238+
extension->get_property_list(&ext_prop_list);
239+
for (const PropertyInfo &prop : ext_prop_list) {
240+
// We only want properties that will show up in the exporter
241+
// settings list. Exclude Resource's properties, as they are
242+
// not relevant to the exporter. Include any user-defined script
243+
// variables exposed to the editor (PROP_EDITOR_SCRIPT_VAR).
244+
if ((prop.usage & PROP_EDITOR_SCRIPT_VAR) == PROP_EDITOR_SCRIPT_VAR) {
245+
PropertyInfo ext_prop = prop;
246+
ext_prop.name = config_prefix + "/" + prop.name;
247+
_property_list.push_back(ext_prop);
248+
}
249+
}
250+
}
234251
}
235252

236253
String EditorSceneExporterGLTFSettings::get_copyright() const {

modules/gltf/extensions/gltf_document_extension.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void GLTFDocumentExtension::_bind_methods() {
4747
GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node");
4848
GDVIRTUAL_BIND(_import_post, "state", "root");
4949
// Export process.
50+
GDVIRTUAL_BIND(_export_get_property_list, "root_node");
5051
GDVIRTUAL_BIND(_export_preflight, "state", "root");
5152
GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node");
5253
GDVIRTUAL_BIND(_export_post_convert, "state", "root");
@@ -151,6 +152,19 @@ Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
151152
}
152153

153154
// Export process.
155+
List<PropertyInfo> GLTFDocumentExtension::export_get_property_list(Node *p_root_node) {
156+
TypedArray<Dictionary> ret_dicts;
157+
GDVIRTUAL_CALL(_export_get_property_list, p_root_node, ret_dicts);
158+
List<PropertyInfo> ret;
159+
if (ret_dicts.is_empty()) {
160+
return ret;
161+
}
162+
for (int i = 0; i < ret_dicts.size(); i++) {
163+
ret.push_back(PropertyInfo::from_dict(ret_dicts[i]));
164+
}
165+
return ret;
166+
}
167+
154168
Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) {
155169
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
156170
Error err = OK;

modules/gltf/extensions/gltf_document_extension.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class GLTFDocumentExtension : public Resource {
5555
virtual Error import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
5656
virtual Error import_post(Ref<GLTFState> p_state, Node *p_node);
5757
// Export process.
58+
virtual List<PropertyInfo> export_get_property_list(Node *p_root_node);
5859
virtual Error export_preflight(Ref<GLTFState> p_state, Node *p_root);
5960
virtual void convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node);
6061
virtual Error export_post_convert(Ref<GLTFState> p_state, Node *p_root);
@@ -81,6 +82,7 @@ class GLTFDocumentExtension : public Resource {
8182
GDVIRTUAL4R(Error, _import_node, Ref<GLTFState>, Ref<GLTFNode>, Dictionary, Node *);
8283
GDVIRTUAL2R(Error, _import_post, Ref<GLTFState>, Node *);
8384
// Export process.
85+
GDVIRTUAL1R(TypedArray<Dictionary>, _export_get_property_list, Node *);
8486
GDVIRTUAL2R(Error, _export_preflight, Ref<GLTFState>, Node *);
8587
GDVIRTUAL3(_convert_scene_node, Ref<GLTFState>, Ref<GLTFNode>, Node *);
8688
GDVIRTUAL2R(Error, _export_post_convert, Ref<GLTFState>, Node *);

0 commit comments

Comments
 (0)