Merge pull request #86183 from demolke/extras

Import/export GLTF extras to `node->meta` and back
This commit is contained in:
Rémi Verschelde 2024-08-30 09:58:47 +02:00
commit d5385491f6
No known key found for this signature in database
GPG Key ID: C3336907360768E1
7 changed files with 253 additions and 7 deletions

View File

@ -1023,6 +1023,14 @@ void Object::remove_meta(const StringName &p_name) {
set_meta(p_name, Variant()); set_meta(p_name, Variant());
} }
void Object::merge_meta_from(const Object *p_src) {
List<StringName> meta_keys;
p_src->get_meta_list(&meta_keys);
for (const StringName &key : meta_keys) {
set_meta(key, p_src->get_meta(key));
}
}
TypedArray<Dictionary> Object::_get_property_list_bind() const { TypedArray<Dictionary> Object::_get_property_list_bind() const {
List<PropertyInfo> lpi; List<PropertyInfo> lpi;
get_property_list(&lpi); get_property_list(&lpi);

View File

@ -895,6 +895,7 @@ public:
MTVIRTUAL void remove_meta(const StringName &p_name); MTVIRTUAL void remove_meta(const StringName &p_name);
MTVIRTUAL Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const; MTVIRTUAL Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const;
MTVIRTUAL void get_meta_list(List<StringName> *p_list) const; MTVIRTUAL void get_meta_list(List<StringName> *p_list) const;
MTVIRTUAL void merge_meta_from(const Object *p_src);
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
void set_edited(bool p_edited); void set_edited(bool p_edited);

View File

@ -4949,7 +4949,9 @@ bool EditorNode::is_object_of_custom_type(const Object *p_object, const StringNa
} }
void EditorNode::progress_add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) { void EditorNode::progress_add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
if (singleton->cmdline_export_mode) { if (!singleton) {
return;
} else if (singleton->cmdline_export_mode) {
print_line(p_task + ": begin: " + p_label + " steps: " + itos(p_steps)); print_line(p_task + ": begin: " + p_label + " steps: " + itos(p_steps));
} else if (singleton->progress_dialog) { } else if (singleton->progress_dialog) {
singleton->progress_dialog->add_task(p_task, p_label, p_steps, p_can_cancel); singleton->progress_dialog->add_task(p_task, p_label, p_steps, p_can_cancel);
@ -4957,7 +4959,9 @@ void EditorNode::progress_add_task(const String &p_task, const String &p_label,
} }
bool EditorNode::progress_task_step(const String &p_task, const String &p_state, int p_step, bool p_force_refresh) { bool EditorNode::progress_task_step(const String &p_task, const String &p_state, int p_step, bool p_force_refresh) {
if (singleton->cmdline_export_mode) { if (!singleton) {
return false;
} else if (singleton->cmdline_export_mode) {
print_line("\t" + p_task + ": step " + itos(p_step) + ": " + p_state); print_line("\t" + p_task + ": step " + itos(p_step) + ": " + p_state);
return false; return false;
} else if (singleton->progress_dialog) { } else if (singleton->progress_dialog) {
@ -4968,7 +4972,9 @@ bool EditorNode::progress_task_step(const String &p_task, const String &p_state,
} }
void EditorNode::progress_end_task(const String &p_task) { void EditorNode::progress_end_task(const String &p_task) {
if (singleton->cmdline_export_mode) { if (!singleton) {
return;
} else if (singleton->cmdline_export_mode) {
print_line(p_task + ": end"); print_line(p_task + ": end");
} else if (singleton->progress_dialog) { } else if (singleton->progress_dialog) {
singleton->progress_dialog->end_task(p_task); singleton->progress_dialog->end_task(p_task);

View File

@ -649,6 +649,9 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<R
String name = p_node->get_name(); String name = p_node->get_name();
NodePath original_path = p_root->get_path_to(p_node); // Used to detect renames due to import hints. NodePath original_path = p_root->get_path_to(p_node); // Used to detect renames due to import hints.
Ref<Resource> original_meta = memnew(Resource); // Create temp resource to hold original meta
original_meta->merge_meta_from(p_node);
bool isroot = p_node == p_root; bool isroot = p_node == p_root;
if (!isroot && _teststr(name, "noimp")) { if (!isroot && _teststr(name, "noimp")) {
@ -1022,6 +1025,8 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<R
print_verbose(vformat("Fix: Renamed %s to %s", original_path, new_path)); print_verbose(vformat("Fix: Renamed %s to %s", original_path, new_path));
r_node_renames.push_back({ original_path, p_node }); r_node_renames.push_back({ original_path, p_node });
} }
// If we created new node instead, merge meta values from the original node.
p_node->merge_meta_from(*original_meta);
} }
return p_node; return p_node;
@ -2452,6 +2457,8 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
mesh_node->set_transform(src_mesh_node->get_transform()); mesh_node->set_transform(src_mesh_node->get_transform());
mesh_node->set_skin(src_mesh_node->get_skin()); mesh_node->set_skin(src_mesh_node->get_skin());
mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path()); mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
mesh_node->merge_meta_from(src_mesh_node);
if (src_mesh_node->get_mesh().is_valid()) { if (src_mesh_node->get_mesh().is_valid()) {
Ref<ArrayMesh> mesh; Ref<ArrayMesh> mesh;
if (!src_mesh_node->get_mesh()->has_mesh()) { if (!src_mesh_node->get_mesh()->has_mesh()) {
@ -2599,6 +2606,7 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
for (int i = 0; i < mesh->get_surface_count(); i++) { for (int i = 0; i < mesh->get_surface_count(); i++) {
mesh_node->set_surface_override_material(i, src_mesh_node->get_surface_material(i)); mesh_node->set_surface_override_material(i, src_mesh_node->get_surface_material(i));
} }
mesh->merge_meta_from(*src_mesh_node->get_mesh());
} }
} }
@ -3114,7 +3122,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
progress.step(TTR("Saving..."), 104); progress.step(TTR("Saving..."), 104);
int flags = 0; int flags = 0;
if (EDITOR_GET("filesystem/on_save/compress_binary_resources")) { if (EditorSettings::get_singleton() && EDITOR_GET("filesystem/on_save/compress_binary_resources")) {
flags |= ResourceSaver::FLAG_COMPRESS; flags |= ResourceSaver::FLAG_COMPRESS;
} }

View File

@ -69,6 +69,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <cstdint> #include <cstdint>
static void _attach_extras_to_meta(const Dictionary &p_extras, Ref<Resource> p_node) {
if (!p_extras.is_empty()) {
p_node->set_meta("extras", p_extras);
}
}
static void _attach_meta_to_extras(Ref<Resource> p_node, Dictionary &p_json) {
if (p_node->has_meta("extras")) {
Dictionary node_extras = p_node->get_meta("extras");
if (p_json.has("extras")) {
Dictionary extras = p_json["extras"];
extras.merge(node_extras);
} else {
p_json["extras"] = node_extras;
}
}
}
static Ref<ImporterMesh> _mesh_to_importer_mesh(Ref<Mesh> p_mesh) { static Ref<ImporterMesh> _mesh_to_importer_mesh(Ref<Mesh> p_mesh) {
Ref<ImporterMesh> importer_mesh; Ref<ImporterMesh> importer_mesh;
importer_mesh.instantiate(); importer_mesh.instantiate();
@ -101,6 +119,7 @@ static Ref<ImporterMesh> _mesh_to_importer_mesh(Ref<Mesh> p_mesh) {
array, p_mesh->surface_get_blend_shape_arrays(surface_i), p_mesh->surface_get_lods(surface_i), mat, array, p_mesh->surface_get_blend_shape_arrays(surface_i), p_mesh->surface_get_lods(surface_i), mat,
mat_name, p_mesh->surface_get_format(surface_i)); mat_name, p_mesh->surface_get_format(surface_i));
} }
importer_mesh->merge_meta_from(*p_mesh);
return importer_mesh; return importer_mesh;
} }
@ -458,7 +477,7 @@ Error GLTFDocument::_serialize_nodes(Ref<GLTFState> p_state) {
if (extensions.is_empty()) { if (extensions.is_empty()) {
node.erase("extensions"); node.erase("extensions");
} }
_attach_meta_to_extras(gltf_node, node);
nodes.push_back(node); nodes.push_back(node);
} }
if (!nodes.is_empty()) { if (!nodes.is_empty()) {
@ -624,6 +643,10 @@ Error GLTFDocument::_parse_nodes(Ref<GLTFState> p_state) {
} }
} }
if (n.has("extras")) {
_attach_extras_to_meta(n["extras"], node);
}
if (n.has("children")) { if (n.has("children")) {
const Array &children = n["children"]; const Array &children = n["children"];
for (int j = 0; j < children.size(); j++) { for (int j = 0; j < children.size(); j++) {
@ -2727,6 +2750,8 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> p_state) {
Dictionary e; Dictionary e;
e["targetNames"] = target_names; e["targetNames"] = target_names;
gltf_mesh["extras"] = e;
_attach_meta_to_extras(import_mesh, gltf_mesh);
weights.resize(target_names.size()); weights.resize(target_names.size());
for (int name_i = 0; name_i < target_names.size(); name_i++) { for (int name_i = 0; name_i < target_names.size(); name_i++) {
@ -2742,8 +2767,6 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> p_state) {
ERR_FAIL_COND_V(target_names.size() != weights.size(), FAILED); ERR_FAIL_COND_V(target_names.size() != weights.size(), FAILED);
gltf_mesh["extras"] = e;
gltf_mesh["primitives"] = primitives; gltf_mesh["primitives"] = primitives;
meshes.push_back(gltf_mesh); meshes.push_back(gltf_mesh);
@ -2776,6 +2799,7 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
Array primitives = d["primitives"]; Array primitives = d["primitives"];
const Dictionary &extras = d.has("extras") ? (Dictionary)d["extras"] : Dictionary(); const Dictionary &extras = d.has("extras") ? (Dictionary)d["extras"] : Dictionary();
_attach_extras_to_meta(extras, mesh);
Ref<ImporterMesh> import_mesh; Ref<ImporterMesh> import_mesh;
import_mesh.instantiate(); import_mesh.instantiate();
String mesh_name = "mesh"; String mesh_name = "mesh";
@ -4170,6 +4194,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
} }
d["extensions"] = extensions; d["extensions"] = extensions;
_attach_meta_to_extras(material, d);
materials.push_back(d); materials.push_back(d);
} }
if (!materials.size()) { if (!materials.size()) {
@ -4372,6 +4397,10 @@ Error GLTFDocument::_parse_materials(Ref<GLTFState> p_state) {
} }
} }
} }
if (material_dict.has("extras")) {
_attach_extras_to_meta(material_dict["extras"], material);
}
p_state->materials.push_back(material); p_state->materials.push_back(material);
} }
@ -5161,6 +5190,7 @@ ImporterMeshInstance3D *GLTFDocument::_generate_mesh_instance(Ref<GLTFState> p_s
return mi; return mi;
} }
mi->set_mesh(import_mesh); mi->set_mesh(import_mesh);
import_mesh->merge_meta_from(*mesh);
return mi; return mi;
} }
@ -5285,6 +5315,7 @@ void GLTFDocument::_convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
gltf_root = current_node_i; gltf_root = current_node_i;
p_state->root_nodes.push_back(gltf_root); p_state->root_nodes.push_back(gltf_root);
} }
gltf_node->merge_meta_from(p_current);
_create_gltf_node(p_state, p_current, current_node_i, p_gltf_parent, gltf_root, gltf_node); _create_gltf_node(p_state, p_current, current_node_i, p_gltf_parent, gltf_root, gltf_node);
for (int node_i = 0; node_i < p_current->get_child_count(); node_i++) { for (int node_i = 0; node_i < p_current->get_child_count(); node_i++) {
_convert_scene_node(p_state, p_current->get_child(node_i), current_node_i, gltf_root); _convert_scene_node(p_state, p_current->get_child(node_i), current_node_i, gltf_root);
@ -5676,6 +5707,8 @@ void GLTFDocument::_generate_scene_node(Ref<GLTFState> p_state, const GLTFNodeIn
current_node->set_transform(gltf_node->transform); current_node->set_transform(gltf_node->transform);
} }
current_node->merge_meta_from(*gltf_node);
p_state->scene_nodes.insert(p_node_index, current_node); p_state->scene_nodes.insert(p_node_index, current_node);
for (int i = 0; i < gltf_node->children.size(); ++i) { for (int i = 0; i < gltf_node->children.size(); ++i) {
_generate_scene_node(p_state, gltf_node->children[i], current_node, p_scene_root); _generate_scene_node(p_state, gltf_node->children[i], current_node, p_scene_root);

View File

@ -0,0 +1,165 @@
/**************************************************************************/
/* test_gltf_extras.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef TEST_GLTF_EXTRAS_H
#define TEST_GLTF_EXTRAS_H
#include "tests/test_macros.h"
#ifdef TOOLS_ENABLED
#include "core/os/os.h"
#include "editor/import/3d/resource_importer_scene.h"
#include "modules/gltf/editor/editor_scene_importer_gltf.h"
#include "modules/gltf/gltf_document.h"
#include "modules/gltf/gltf_state.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/main/window.h"
#include "scene/resources/3d/primitive_meshes.h"
#include "scene/resources/material.h"
#include "scene/resources/packed_scene.h"
namespace TestGltfExtras {
static Node *_gltf_export_then_import(Node *p_root, String &p_tempfilebase) {
Ref<GLTFDocument> doc;
doc.instantiate();
Ref<GLTFState> state;
state.instantiate();
Error err = doc->append_from_scene(p_root, state, EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS);
CHECK_MESSAGE(err == OK, "GLTF state generation failed.");
err = doc->write_to_filesystem(state, p_tempfilebase + ".gltf");
CHECK_MESSAGE(err == OK, "Writing GLTF to cache dir failed.");
// Setting up importers.
Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene("PackedScene", true));
ResourceFormatImporter::get_singleton()->add_importer(import_scene);
Ref<EditorSceneFormatImporterGLTF> import_gltf;
import_gltf.instantiate();
ResourceImporterScene::add_scene_importer(import_gltf);
// GTLF importer behaves differently outside of editor, it's too late to modify Engine::get_editor_hint
// as the registration of runtime extensions already happened, so remove them. See modules/gltf/register_types.cpp
GLTFDocument::unregister_all_gltf_document_extensions();
HashMap<StringName, Variant> options(20);
options["nodes/root_type"] = "";
options["nodes/root_name"] = "";
options["nodes/apply_root_scale"] = true;
options["nodes/root_scale"] = 1.0;
options["meshes/ensure_tangents"] = true;
options["meshes/generate_lods"] = false;
options["meshes/create_shadow_meshes"] = true;
options["meshes/light_baking"] = 1;
options["meshes/lightmap_texel_size"] = 0.2;
options["meshes/force_disable_compression"] = false;
options["skins/use_named_skins"] = true;
options["animation/import"] = true;
options["animation/fps"] = 30;
options["animation/trimming"] = false;
options["animation/remove_immutable_tracks"] = true;
options["import_script/path"] = "";
options["_subresources"] = Dictionary();
options["gltf/naming_version"] = 1;
// Process gltf file, note that this generates `.scn` resource from the 2nd argument.
err = import_scene->import(p_tempfilebase + ".gltf", p_tempfilebase, options, nullptr, nullptr, nullptr);
CHECK_MESSAGE(err == OK, "GLTF import failed.");
ResourceImporterScene::remove_scene_importer(import_gltf);
Ref<PackedScene> packed_scene = ResourceLoader::load(p_tempfilebase + ".scn", "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
CHECK_MESSAGE(err == OK, "Loading scene failed.");
Node *p_scene = packed_scene->instantiate();
return p_scene;
}
TEST_CASE("[SceneTree][Node] GLTF test mesh and material meta export and import") {
// Setup scene.
Ref<StandardMaterial3D> original_material = memnew(StandardMaterial3D);
original_material->set_albedo(Color(1.0, .0, .0));
original_material->set_name("material");
Dictionary material_dict;
material_dict["node_type"] = "material";
original_material->set_meta("extras", material_dict);
Ref<PlaneMesh> original_meshdata = memnew(PlaneMesh);
original_meshdata->set_name("planemesh");
Dictionary meshdata_dict;
meshdata_dict["node_type"] = "planemesh";
original_meshdata->set_meta("extras", meshdata_dict);
original_meshdata->surface_set_material(0, original_material);
MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
original_mesh_instance->set_mesh(original_meshdata);
original_mesh_instance->set_name("mesh_instance_3d");
Dictionary mesh_instance_dict;
mesh_instance_dict["node_type"] = "mesh_instance_3d";
original_mesh_instance->set_meta("extras", mesh_instance_dict);
Node3D *original = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(original);
original->add_child(original_mesh_instance);
original->set_name("node3d");
Dictionary node_dict;
node_dict["node_type"] = "node3d";
original->set_meta("extras", node_dict);
original->set_meta("meta_not_nested_under_extras", "should not propagate");
// Convert to GLFT and back.
String tempfile = OS::get_singleton()->get_cache_path().path_join("gltf_extras");
Node *loaded = _gltf_export_then_import(original, tempfile);
// Compare the results.
CHECK(loaded->get_name() == "node3d");
CHECK(Dictionary(loaded->get_meta("extras")).size() == 1);
CHECK(Dictionary(loaded->get_meta("extras"))["node_type"] == "node3d");
CHECK_FALSE(loaded->has_meta("meta_not_nested_under_extras"));
CHECK_FALSE(Dictionary(loaded->get_meta("extras")).has("meta_not_nested_under_extras"));
MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(loaded->find_child("mesh_instance_3d", false, true));
CHECK(mesh_instance_3d->get_name() == "mesh_instance_3d");
CHECK(Dictionary(mesh_instance_3d->get_meta("extras"))["node_type"] == "mesh_instance_3d");
Ref<Mesh> mesh = mesh_instance_3d->get_mesh();
CHECK(Dictionary(mesh->get_meta("extras"))["node_type"] == "planemesh");
Ref<Material> material = mesh->surface_get_material(0);
CHECK(material->get_name() == "material");
CHECK(Dictionary(material->get_meta("extras"))["node_type"] == "material");
memdelete(original_mesh_instance);
memdelete(original);
memdelete(loaded);
}
} // namespace TestGltfExtras
#endif // TOOLS_ENABLED
#endif // TEST_GLTF_EXTRAS_H

View File

@ -174,6 +174,31 @@ TEST_CASE("[Object] Metadata") {
CHECK_MESSAGE( CHECK_MESSAGE(
meta_list2.size() == 0, meta_list2.size() == 0,
"The metadata list should contain 0 items after removing all metadata items."); "The metadata list should contain 0 items after removing all metadata items.");
Object other;
object.set_meta("conflicting_meta", "string");
object.set_meta("not_conflicting_meta", 123);
other.set_meta("conflicting_meta", Color(0, 1, 0));
other.set_meta("other_meta", "other");
object.merge_meta_from(&other);
CHECK_MESSAGE(
Color(object.get_meta("conflicting_meta")).is_equal_approx(Color(0, 1, 0)),
"String meta should be overwritten with Color after merging.");
CHECK_MESSAGE(
int(object.get_meta("not_conflicting_meta")) == 123,
"Not conflicting meta on destination should be kept intact.");
CHECK_MESSAGE(
object.get_meta("other_meta", String()) == "other",
"Not conflicting meta name on source should merged.");
List<StringName> meta_list3;
object.get_meta_list(&meta_list3);
CHECK_MESSAGE(
meta_list3.size() == 3,
"The metadata list should contain 3 items after merging meta from two objects.");
} }
TEST_CASE("[Object] Construction") { TEST_CASE("[Object] Construction") {