0 votes

I have created multiple scenes in Godot and want to place them as a 3D Grid.

Some example scenes:
https://ibb.co/tQb0zY9
https://ibb.co/XLCwXJP

When I convert them to an Meshlib only the default cube meshes (1 x 1 x 1 size) are added to it, not like I have created them.

Is there any way to add the scenes as I created them to a 3D Grid? Or do I have to redo all work in Blender?

Godot version 2.3.2
in Engine by (130 points)

1 Answer

+1 vote

The "Convert to -> MeshLibrary" button always stretches meshes so that their bounds fit exactly in one block. However, this annoying behavior can be avoided, by not using the button.

MeshLibrary extends Resource, and has all the method you need to create it in GDScript.

To create an empty MeshLibrary, just call MeshLibrary.new(). Once it is filled, use ResourceSaver.save().
Make yourself a list of Meshes, (or just iterate over a Directory containing them), then for each one, call my_mesh_library.create_item(), and set:

  • item name
  • item mesh
  • item shape (use my_mesh.create_convex_shape())
  • item preview (use get_editor_interface().make_mesh_previews() in a tool script extending EditorScript).

Writing code to create your MeshLibrary is tedious, but allows for more possibilities, and future Mesh additions are more practical.

Edit: Not to be confused between Mesh and MeshInstance. Mesh is a Resource that contains faces, Materials, etc, whereas MeshInstance is a Spatial that displays a Mesh. (get_node("Mesh1").mesh)

by (2,276 points)
edited by

Thank you very much!

I created a test file with a couple of meshes, but when I set the mesh: TestMeshlib.set_item_mesh(0, get_node("Mesh1"))

The mesh is not set for a reason. (The node of the type MeshInstance Mesh1 exists).

When I run: print(TestMeshlib.get_item_mesh(0)) it outputs: [Object:null]

The code looks like this at the moment:

func _ready():
    var TestMeshlib = MeshLibrary.new()
    TestMeshlib.create_item(0)
    TestMeshlib.set_item_name(0, "Box")
    TestMeshlib.set_item_mesh(0, get_node("Mesh1"))
    ResourceSaver.save("res://TestMeshlib.meshlib", TestMeshlib)

I tired it with get_node("Mesh1").mesh, but then I get the error: Invalid get index 'mesh' (on base: 'null instance').

My new code (Test.gd):

extends Spatial

func _ready():
    var TestMeshlib = MeshLibrary.new()
    TestMeshlib.create_item(0)
    TestMeshlib.set_item_name(0, "Box")
    TestMeshlib.set_item_mesh(0, get_node("Mesh1").mesh)
    ResourceSaver.save("res://TestMeshlib.meshlib", TestMeshlib)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.