0 votes

Can 3D objects/meshes be imported/loaded by code from within the running game, or would I have to write my own addon to accomplish this? To be clear, I'm not talking about instantiating a saved scene, but importing a 3d model/mesh from within the game.
I'm developing my infrastructure for an upcoming project and I want the player to be able to mod in-game objects. For example, you might have a drag race simulator and allow the players to design their own 3D car bodies (in Blender) and use them to design their in-game car. And btw, at this stage, I'm not concerned about the security of mods or the problems mods can cause, just possibilities :) Thanks in advance.

in Engine by (310 points)

2 Answers

+1 vote

Yes, you can! Something like this should do the trick:

var cubemeshinstance=MeshInstance.new()
var cubemesh=load('res://cube.msh')
cubemeshinstance.set_mesh(cubemesh)
add_child(cubemeshinstance)
where cube.msh is whatever mesh the user wants to import.

If you want to give it a collider, do the previous from a RigidBody, then do this:
var collshape=BoxShape.new()
collshape.set_extents(Vector3(1,1,1))
add_shape(collshape)
For your application, it sounds like you want to use ConvexPolygonShape or ConcavePolygonShape rather than BoxShape though.

by (56 points)

Thanks for the reply. That is pretty much what I want, but my problem is the .msh file. It is the only file format that I see to define the shape of custom meshes in Godot and it’s created by importing an .obj file in the editor during development. I don’t see any way to either get an .obj into the finished game on the fly, or any way to create an .msh file outside of the Godot editor.

Did you solve this Michael Paul? Or is there really no way to import .obj files for later modding etc?

0 votes

For load directly an .obj

    var meshInstance = MeshInstance.new()
    var mesh = load("res://file.obj")
    meshInstance.set_mesh(mesh)
    call_deferred("add_child", meshInstance)

Works.

by (18 points)
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.