How to use custom class for imported model in Godot 4?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kyuth

I have made a model in Blender and imported it to Godot 4 Beta 10 as a .glb file. If I doubleclick the .glb file it opens import-settings where I can choose the Node’s root type, for example a StaticBody3D. Then I can load and instantiate the .glb file as a Scene and have a StaticBody3D in my game. Great.

But I have created a custom class that extends StaticBody3D and I would like to use that when I instantiate the node. I would like to choose my script from the import settings as I choose the root type but that option is not available.

I have found only one way to do that. The steps are:

  1. Select .glb, right mouser click → New Inherited scene
  2. Click the movie-symbol on the right side of the root-node → Open anyway
  3. Select the root-node of the new scene, add a new Node3d
  4. Click the Node3D with right mouse button → Make scene root
  5. Right click previous root node that is not root anymore → Save branch as scene…
  6. Assing the custom script to the new .tscn scene.

This is hilariously cumbersome and must be repeated every time when the model changes. Any ideas how to do that easier?

:bust_in_silhouette: Reply From: Inces

The way You presented here is not only tedious, it is also blocking any option of reimporting assets.

What You are looking for is post_import tool scripting. Create new script :

tool
extends EditorScenePostImport


func post_import(scene):
   

And than You can manipulate the root ( scene ) any way You want, but You have to return it. For example :

func post_import(scene):
    scene.replace_by(yourcustombody)
            return yourcustombody

Or manually rechild mesh instances to new custom body and so on, You will figure it out :slight_smile:

Now You just place this script in Import Settings in custom script slot and press reimport

Thanks, using an import script fixes the problem.

I’m using C# and apparently C# import scripts are broken but I managed to write one in GDScript that creates a node from my class and assigns it to the imported scene:

@tool 

extends EditorScenePostImport

func _post_import(scene):
    print("importing small ship")

    var smallShipClassScript = load("res://src/SmallShip.cs")
    var smallShipNode = smallShipClassScript.new();
    scene.replace_by(smallShipNode);
    scene.free()
    return smallShipNode

kyuth | 2023-01-04 10:14