DEV Community

Cover image for Why Godot drops objects when creating a MeshLibrary
Robert Cannon
Robert Cannon

Posted on

Why Godot drops objects when creating a MeshLibrary

The Problem

I was recently messing around with GridMaps in Godot Engine which allow you to "paint" meshes on a 3D grid much like you would a 2D tileset. Following the official GripMap tutorial, I noticed some odd behavior creating a MeshLibrary.

Grid Map has a Mesh Library with no meshes

I was using the models from the City Kit Roads pack from Kenney.nl. They render nicely in the scene tree, but when using the Scene -> Convert to MeshLibrary option to create a MeshLibrary the resulting .tres file was empty. In previous projects, this feature has worked for me so I wondered if there was something particular about these assets that was missing something Godot expected.

Scene to Export as Mesh Library

Convert to Mesh Library menu item

The Investigation

I've had luck looking at the Godot source code, in other situations, thanks to it being open source. A quick look at the MeshLibrary code showed something worth looking into:

mesh_library_editor_plugin.cpp#L78-L88

void MeshLibraryEditor::_import_scene(Node *p_scene, /* ... */) {

  // ...

  for (int i = 0; i < p_scene->get_child_count(); i++) {
    Node *child = p_scene->get_child(i);
    if (!Object::cast_to<MeshInstance3D>(child)) {
      if (child->get_child_count() > 0) {
        child = child->get_child(0);
        if (!Object::cast_to<MeshInstance3D>(child)) {
          continue;
        }
      } else {
        continue;
      }
    }

// ...
Enter fullscreen mode Exit fullscreen mode

Here the _import_scene() function is looping over the scenes children and if they are not a MeshInstance3D then it looks at the first grandchild, if there are no grandchildren or the first one isn't a MeshInstance3D then it skips the whole child. That seemed promising, so I opened the asset as a "New Inherited Scene" and sure enough it was nested.

Imported Scene Tree

The Solution

A quick edit as a "New Inherited Scene" to change the hierarchy like this

Modified Scene Tree

And the models were added to the MeshLibrary without issues.

Working Grid Map

Top comments (0)