Import 3D object from code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Michael Paul
:warning: Old Version Published before Godot 3 was released.

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 :slight_smile: Thanks in advance.

:bust_in_silhouette: Reply From: DrPlamsa

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.

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.

Michael Paul | 2017-12-08 10:55

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

wentdot | 2018-02-26 21:20

:bust_in_silhouette: Reply From: D3m0n92

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.