Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GLTF Scene Importer populate node metadata with GLTF extras #39024

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions editor/import/editor_scene_importer_gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/*************************************************************************/

#include "editor_scene_importer_gltf.h"
#include "core/core_string_names.h"
#include "core/crypto/crypto_core.h"
#include "core/io/json.h"
#include "core/math/disjoint_set.h"
Expand Down Expand Up @@ -273,6 +274,9 @@ Error EditorSceneImporterGLTF::_parse_nodes(GLTFState &state) {
if (n.has("mesh")) {
node->mesh = n["mesh"];
}
if (n.has("extras")) {
node->extras = (Dictionary)n["extras"];
}
if (n.has("skin")) {
node->skin = n["skin"];
}
Expand Down Expand Up @@ -2486,6 +2490,13 @@ BoneAttachment *EditorSceneImporterGLTF::_generate_bone_attachment(GLTFState &st
return bone_attachment;
}

void EditorSceneImporterGLTF::_set_meta_from_gltf_extras(GLTFState &state, Node *node, const GLTFNode *gltf_node) {
if (state.import_meta && !gltf_node->extras.empty()) {
const Dictionary &extras = gltf_node->extras;
node->set(CoreStringNames::get_singleton()->_meta, extras);
}
}

MeshInstance *EditorSceneImporterGLTF::_generate_mesh_instance(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index) {
const GLTFNode *gltf_node = state.nodes[node_index];

Expand All @@ -2497,6 +2508,8 @@ MeshInstance *EditorSceneImporterGLTF::_generate_mesh_instance(GLTFState &state,
GLTFMesh &mesh = state.meshes.write[gltf_node->mesh];
mi->set_mesh(mesh.mesh);

_set_meta_from_gltf_extras(state, mi, gltf_node);

if (mesh.mesh->get_name() == "") {
mesh.mesh->set_name(gltf_node->name);
}
Expand All @@ -2516,6 +2529,8 @@ Camera *EditorSceneImporterGLTF::_generate_camera(GLTFState &state, Node *scene_
Camera *camera = memnew(Camera);
print_verbose("glTF: Creating camera for: " + gltf_node->name);

_set_meta_from_gltf_extras(state, camera, gltf_node);

const GLTFCamera &c = state.cameras[gltf_node->camera];
if (c.perspective) {
camera->set_perspective(c.fov_size, c.znear, c.zfar);
Expand All @@ -2532,6 +2547,8 @@ Spatial *EditorSceneImporterGLTF::_generate_spatial(GLTFState &state, Node *scen
Spatial *spatial = memnew(Spatial);
print_verbose("glTF: Creating spatial for: " + gltf_node->name);

_set_meta_from_gltf_extras(state, spatial, gltf_node);

return spatial;
}

Expand Down Expand Up @@ -2990,6 +3007,7 @@ Node *EditorSceneImporterGLTF::import_scene(const String &p_path, uint32_t p_fla
state.major_version = version.get_slice(".", 0).to_int();
state.minor_version = version.get_slice(".", 1).to_int();
state.use_named_skin_binds = p_flags & IMPORT_USE_NAMED_SKIN_BINDS;
state.import_meta = p_flags & IMPORT_META;

/* STEP 0 PARSE SCENE */
Error err = _parse_scenes(state);
Expand Down
4 changes: 4 additions & 0 deletions editor/import/editor_scene_importer_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {

Transform xform;
String name;
Dictionary extras;

GLTFMeshIndex mesh;
GLTFCameraIndex camera;
Expand Down Expand Up @@ -300,6 +301,7 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
Vector<uint8_t> glb_data;

bool use_named_skin_binds;
bool import_meta;

Vector<GLTFNode *> nodes;
Vector<Vector<uint8_t> > buffers;
Expand Down Expand Up @@ -395,6 +397,8 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {

Error _parse_animations(GLTFState &state);

void _set_meta_from_gltf_extras(GLTFState &state, Node *node, const GLTFNode *gltf_node);

BoneAttachment *_generate_bone_attachment(GLTFState &state, Skeleton *skeleton, const GLTFNodeIndex node_index);
MeshInstance *_generate_mesh_instance(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index);
Camera *_generate_camera(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index);
Expand Down
5 changes: 5 additions & 0 deletions editor/import/resource_importer_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void EditorSceneImporter::_bind_methods() {

BIND_CONSTANT(IMPORT_SCENE);
BIND_CONSTANT(IMPORT_ANIMATION);
BIND_CONSTANT(IMPORT_META);
BIND_CONSTANT(IMPORT_ANIMATION_DETECT_LOOP);
BIND_CONSTANT(IMPORT_ANIMATION_OPTIMIZE);
BIND_CONSTANT(IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS);
Expand Down Expand Up @@ -1161,6 +1162,7 @@ void ResourceImporterScene::get_import_options(List<ImportOption> *r_options, in
bool animations_out = p_preset == PRESET_SEPARATE_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS;

r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "nodes/root_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001"), 1.0));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "nodes/meta"), false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added metadata doesn't seem particularly compat breaking, so I guess it could be on by default?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with many 3D modeling tools but I can imagine some tools abusing custom properties to store a lot of data (maybe editor plugins?) - so I'm just concerned the user might unknowingly end up with a lot of data in his scene that they never use (and currently not even immediately see as the metadata can be displayed only through a plugin or needs to be inspected by code).

Also if someone already used metadata assuming (performance-wise) there's going to be only their keys/values it might significantly slow down their code if they use some 3D tool that fills the object metadata with a lot of stuff.

But as already stated - I don't really know how things usually work in gamedev/3Dmodeling.

r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/custom_script", PROPERTY_HINT_FILE, script_ext_hint), ""));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "nodes/storage", PROPERTY_HINT_ENUM, "Single Scene,Instanced Sub-Scenes"), scenes_out ? 1 : 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/location", PROPERTY_HINT_ENUM, "Node,Mesh"), (meshes_out || materials_out) ? 1 : 0));
Expand Down Expand Up @@ -1299,6 +1301,9 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
float fps = p_options["animation/fps"];

int import_flags = EditorSceneImporter::IMPORT_ANIMATION_DETECT_LOOP;
if (bool(p_options["nodes/meta"]))
import_flags |= EditorSceneImporter::IMPORT_META;

if (!bool(p_options["animation/optimizer/remove_unused_tracks"]))
import_flags |= EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS;

Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class EditorSceneImporter : public Reference {
IMPORT_MATERIALS_IN_INSTANCES = 1024,
IMPORT_USE_COMPRESSION = 2048,
IMPORT_USE_NAMED_SKIN_BINDS = 4096,

IMPORT_META = 8192,
};

virtual uint32_t get_import_flags() const;
Expand Down