Godot Version
4.2.2
Question
Hey there!
I am fairly new to GDExtension and, well, Godot in general, so any advice will be appreciated, even not related to the question.
Currently I am trying to integrate a C++ library into Godot using GDExtension, and for that I have to load glTF models from code in the editor . Eventually I will have to use glTF importer from a byte buffer, but for now I am testing with .glb files. For that I have written a function for a custom node that loads a model from a file and adds it to the node’s children:
void CustomNode::loadGltfPath(const godot::String& gltfPath)
{
godot::UtilityFunctions::print("Loading GLTF model: ", gltfPath);
if (!godot::FileAccess::file_exists(gltfPath))
{
godot::UtilityFunctions::print("File ", gltfPath, " does not exist");
return;
}
if (m_gltfNode != nullptr)
{
if (get_children().has(m_gltfNode)) remove_child(m_gltfNode);
memdelete(m_gltfNode);
}
godot::GLTFDocument* gltfDoc = memnew(godot::GLTFDocument);
godot::Ref<godot::GLTFState> gltfState;
gltfState.instantiate();
godot::Error err = gltfDoc->append_from_file(gltfPath, gltfState);
godot::UtilityFunctions::print("Load result: ", err);
m_gltfNode = gltfDoc->generate_scene(gltfState);
m_gltfNode->set_name("glTF model");
add_child(m_gltfNode);
m_gltfNode->set_owner(get_owner());
godot::UtilityFunctions::print(m_gltfNode);
memdelete(gltfDoc);
}
I have also used editor inspector plugin to add a button to the node’s inspector. The button is used to call the loadGltf()
function based on the property in the inspector.
The issue with all of this is that this way the newly created scene is correctly added as a child only at game runtime (if I call the function in _ready()
), while in the editor I can only see an empty Node3D being added.
I have googled around a little bit, and everyone seems to say that adding nodes to the scene in editor requires changing ownership, and I tried to do that in the code but all it does is just add that empty node.
Apparently, the scene generated from a .glb file is valid, since I tried to listing its children, and they all are valid nodes/meshes.
This happens both when I try to load the model in C++ code and an equivalent GDScript @tool script. I suppose there is something wrong with the way I add the node to the scene tree, but I couldn’t find a different approach, so any help is appreciated.