0 votes

Hey,
I have many different 3d-meshes imported in scenes from blender.
Now I want to get the respective mesh object from a 2d viewport coordinate of my camera.

What is the best way to handle this?
I read this can be done by adding collision shapes to my meshes and using their input events. But for this I would need to create a new scene for each of my exported scenes, since they are not editable and add a collision shape there, afterwards replacing every loaded scene in my game by the inherited one. Doesn't seem like a clean solution to me.

I know there is another method using a raycast from my camera, but I don't know how to check the collision with a mesh then.

Thanks for your help,
Tobi

in Engine by (179 points)

I can't get why you aren't generating collider based on given mesh. No need to create different scene for each mesh. You could attach project demo, maybe I don't understand your problem and that will help me solve it.

Thanks for your comment, I try my best to explain it a little better. I'm not sure yet how a demo project explaining my issue could look like, I think it may be easier if I describe it this way:

I used blender to create a few 3D buildings, which I exported using the better-collada-exporter. I have a folder with all my materials and one .dae file for each building.

Imported in Godot each .dae file is one scene, which is not editable. Inside the scene is one node with one MeshInstance node. I can't create a collision shape in that scene without creating a new inherited scene. That's what I mean by "creating a new scene for each object".

From my camera's view I want to know which building is at a given screen position.

I managed to implement some other things without creating an inherited scene from each of my exported .dae's, so I am looking for a solution that gets this task done without creating new inherited scenes either.

1 Answer

+1 vote

In your Scene tree select models, right-click and select "Discard Instancing" - that way you can edit them in that scene.

by (378 points)

Ah nice. Then I can go and generate a Collision-Shape, I see. Thanks.
One question building upon this:

Do I lose the connection to my exported scene when doing this?
When I update my mesh in blender and want to reimport it, do I have to delete the mesh and reimport it, making all my modifications again or is there another way to just update the mesh?

About importing, I'm not sure, but I think it won't update. I just can't think of a different way to achieve what you need without creating separate scenes.

So creating inherited scenes is the more common workflow in godot? If it makes more sense I would do that, it just didn't sound like the clearest and easiest way to me. How do other users deal with imported meshes?

That's how I do it. Separated scenes are good if you want to do more stuff. For example I have Sword in my RPG game, that Sword has Particle System, collision and all stuff. I can use that one scene as template for other weapons. It's good because project structure is clean that way.

Thanks again, that sounds interesting. Can you give me a hint on where I can find more about the functionality for using scenes a templates?

By templates I meant that I have one scene that I'm duplicating and just changing some stuff. Then I'm using script to spawn that scene and so stuff with it.
Here is example of Player and Sword spawning (C#):

public override void _Ready()
{
    Node root = (Node)GetTree().GetRoot().GetNode("Spatial");

    PackedScene playerRes = (PackedScene)GD.Load("res://scenes/Player.tscn");
    player = (Player)playerRes.Instance();

    PackedScene swordRes = (PackedScene)GD.Load("res://scenes/Sword.tscn");
    Node sword = swordRes.Instance();
    player.SetTranslation(new Vector3(0, 5, 0));
    player.GetNode("Character/metarig/Skeleton/RightHand").AddChild(sword);
    root.AddChild(player);
}
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.