Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading model files at runtime #24768

Closed
crabctrl opened this issue Jan 4, 2019 · 29 comments
Closed

Loading model files at runtime #24768

crabctrl opened this issue Jan 4, 2019 · 29 comments

Comments

@crabctrl
Copy link

crabctrl commented Jan 4, 2019

Godot version:
N/A

OS/device including version:
N/A

Issue description:
You currently cannot load files such as OBJ, DAE, and GLTF/GLB at runtime.

Steps to reproduce:
N/A

Minimal reproduction project:
N/A

I understand that packing the import plugins with every exported binary isn't great for the many people who don't need this; that's not what I'm asking for. It would be useful to have a compile flag that allowed the import plugins to be compiled into the binary, for those who are willing to compile. Additionally, although I don't know much about how these plugins work, we'd clearly need more than just a compile flag since even when running a game using the editor's binary you still can't easily import files at runtime.

Some members of the community have questioned why you'd ever need this, which is fair enough, so let me give my use case. I'm developing an FPS where maps are downloaded from servers at runtime. Godot scenes can contain scripts & signals which makes them a security risk. Yes, I can strip out scripts programmatically, but I'm not done yet. Needing to import the maps into Godot makes life more difficult for mapmakers, turning away valuable community members from the game. It's much easier for a mapmaker to just click "Export as glTF" in Blender than it is for them to first model it in Blender and then import it into Godot.

Of course, that's just me and my use case. Others don't need this, which is why it should probably be a separate compile flag rather than the default functionality.

Thank you for your time.

@Chaosus Chaosus changed the title Loading model files at runtime [FEATURE REQUEST] Loading model files at runtime Jan 4, 2019
@gonherdg
Copy link

I am looking for this feature too!! Many projects depends on being moddables. Importing meshes and textures in a safety way is what I need, it would be GREAT.

@fire
Copy link
Member

fire commented Jan 9, 2020

Please see #34193

The api is close to:

   var importer = SceneImporterGLTF.new()
   var root = importer.import_gltf("user://example.glb")

@chitalian
Copy link

@fire Where is the API Documentation for SceneImporterGLTF?

@fire
Copy link
Member

fire commented Jan 14, 2020

It's still work in progress and not in 3.2, but I can show you this.

image

@chitalian
Copy link

@fire This looks awesome! I am so excited for this.

I will need to import GLTF models at runtime for a project. Would you say that functionality is currently at a usable state in your scene_export_native_gltf branch? Anything I should be aware of when building? I'd be happy to provide feedback if I am able to get this working.

@fire
Copy link
Member

fire commented Jan 14, 2020

Please try and comment on your problems.

Remember that res:// is read only and user:// is writable.

You probably need to use the duplicate_and_reown function though.

[Edited]
I think you need to add the node as a child and change all the owners [in the tree] to be get_owner().

void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const {

[Edited]

This is too difficult to use. I'll change it so it instances at the child of this node.

@chitalian
Copy link

Thanks for your quick feedback!

Using your suggestion here:

Please see #34193

The api is close to:

   var importer = SceneImporterGLTF.new()
   var root = importer.import_gltf("user://example.glb")

I tried importing my own local gltf file using this Box.glb sample to my user:// dir.
I created an empty scene with a single Base Node class with the following code:

extends Node

func _ready():
	var importer = SceneImporterGLTF.new()
	var root = importer.import_gltf("user://Box.glb")

and I got this error

E 0:00:01.004   import_gltf: Condition ' err != OK ' is true. returned: __null
  <C++ Source>  modules/gltf/editor_scene_importer_gltf.h:74 @ import_gltf()
  <Stack Trace> Testing_Area.gd:13 @ _ready()

Is there anything else you would recommend to help me get this working?

@fire
Copy link
Member

fire commented Jan 14, 2020

Update your branch and use:

extends Spatial

func _process(_delta):
	if Input.is_action_pressed("ui_accept"):
		var importer = SceneImporterGLTF.new()
		importer.import_gltf(self, "res://door.glb")

@fire
Copy link
Member

fire commented Jan 14, 2020

At fire@f00f49c

Api changed to:

extends Spatial

func _process(_delta):
	if Input.is_action_pressed("ui_accept"):
		var importer = SceneImporterGLTF.new()
		var node = importer.import_gltf("res://door.glb")
		add_child(node)

@chitalian
Copy link

@fire This is working great so far! Thanks so much.

Please reach out if you would like help with any further development. I can also write some docs too!

@fire
Copy link
Member

fire commented Jan 19, 2020

How can I contact you? I use discord often.

@PravinKumar95
Copy link

will this feature be available in 3.2.2?

@Calinou
Copy link
Member

Calinou commented May 7, 2020

@PravinKumar95 The WIP pull request above isn't in a mergeable state yet, so no.

@fire
Copy link
Member

fire commented May 7, 2020

I can update my PR to be in 3.2, but I don't think it will be merged in 3.2.

For me, 4.0 currently can't even load gltf2 into meshes.

I've been using the exporter for my projects.

The github master PR pipeline is heavily delayed, so I have been waiting for my Skeleton Inspector PR to be looked. At least until the next time a cool idea shows up.

The only big changes in my testing is I removed CSG import because there's no robust api for getting a mesh out. Will need to revisit.

Another bug is related to when the root node is a mesh in gltf / godot scene. My fixed tried to fix export, but then import broke.

@PravinKumar95
Copy link

I can update my PR to be in 3.2, but I don't think it will be merged in 3.2.

This will be good enough for me. I only need runtime import of mesh and materials. Thanks

@Mek101
Copy link

Mek101 commented May 11, 2020

Is there also a way to load compiled scripts at runtime? (C# or third-party bindings)

@fire
Copy link
Member

fire commented May 11, 2020

@Mek101 This is the wrong topic for addressing loading compiled scripts from C# or GDNative.

@fire
Copy link
Member

fire commented May 12, 2020

@PravinKumar95 I've been developing the changes here. https://github.com/godot-extended-libraries/gltf Please file issues if you have problems.

Known issues:

  • Root nodes with skeletons / mesh instances disappear
  • CSG support was removed until I get a look at how to get data without modifying the csg class

@clayjohn
Copy link
Member

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

@fire
Copy link
Member

fire commented May 27, 2020

I have an module that implements runtime model import. Anyone who is interested can make a proposal and I'll link when I make the pull request.

Link: https://github.com/godot-extended-libraries/gltf

@fbcosentino
Copy link
Contributor

@fire I see the repo above is archived as it was merged into 4.0, but how difficult would it be to compile it as GDNative for 3.x? Is it something I can do using your archived repo or is the code 4.x specific?

@fire
Copy link
Member

fire commented Jun 26, 2021

Please use gltf module from https://godotengine.org/asset-library/asset/964

Source is from https://github.com/V-Sekai/godot-gltf-module/tree/gdnative

@fbcosentino
Copy link
Contributor

I'm failing to make it work at runtime. Works perfectly in editor, but SceneImporterGLTF (from comments above) is not recognized and EditorSceneImporterGLTF only works in the editor...

@Calinou

This comment has been minimized.

@fbcosentino
Copy link
Contributor

There is a GDNative library included, and I'm not even exporting anything, I'm failing to make it work while running a project in debug from the editor.

@fire
Copy link
Member

fire commented Jun 27, 2021

@Calinou https://github.com/V-Sekai/godot-gltf-module/tree/gdnative is a GDNative module.

Do you have a sample of your project?

Remember that you manually call PackedSceneGLTF since none of the editor code is in the runtime.

Try 2.

godot-glftf-gdnative.zip

Should contain addons/godot_gltf/PackedSceneGLTF.gdns

The api is:

PackedSceneGLTF.import_gltf_scene(path, flags, bake_fps, state)

@fbcosentino
Copy link
Contributor

fbcosentino commented Jun 27, 2021

Thanks, the API is what I was missing.

The above API didn't work directly (the class is registered but the method was not found), I actually had to load the GDNative as below (taken from import_vrm.gd):

var flags = 0
var bake_fps = 1000

var gstate = load("res://addons/godot_gltf/GLTFState.gdns").new()
var gltf = load("res://addons/godot_gltf/PackedSceneGLTF.gdns").new()
var instance = gltf.import_gltf_scene(path, flags, bake_fps, gstate)

That worked perfectly for models outside res://
(Pasting the code in case others search here in the future)

@cloakd
Copy link

cloakd commented Dec 14, 2021

Managed to get the above working thanks @fbcosentino, however when exporting to HTML5 it then stops working, any ideas?

@Calinou
Copy link
Member

Calinou commented Dec 14, 2021

Managed to get the above working thanks @fbcosentino, however when exporting to HTML5 it then stops working, any ideas?

You need to enable GDNative in the HTML5 export preset for GDNative add-ons to work there. The add-on also needs to be compiled for use in HTML5. I think you also need to use a secure context (HTTPS hosting) that sends the required cross-origin isolation headers as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests