Script Import File Issue

Godot Version

4.7

Question

Goal

I’m importing my assets into godot, the downloaded assets have many useless nodes, I want to get rid of them

I can’t change the asset structures in blender, it will take too much time since I have many assets

My solution

I created a script to delete those nodes, this is the script, it deletes the parent of all the nodes with a parent, the only node I need (the mesh instance 3d) doesn’t have a child

@tool
extends EditorScenePostImport

func _post_import(scene: Node) -> Object:
	var node = scene
	while node.get_child_count(true) > 0:
		node = delete_node_parent(node)
	return node

func delete_node_parent(node: Node) -> Node:
	var child = node.get_child(0)
	node.remove_child(child)
	child.owner = null
	set_children_owner(child, child)
	return child

func set_children_owner(node, owner):
	for n in node.get_children():
		n.owner = owner
		set_children_owner(n, owner)

I put that script in the import settings of the assets I want to modify

this code seems to work because it removes all the useless nodes, this is the result in the editor

Issue

the main issue is when I import the asset running the import script, the asset becomes EXTREMELY big for no reason (I’m not changing the root scale)

in comparison, this is the same asset imported without running the import script (normal dimension)

can someone help me to understand how to fix my code and import the assets correctly?

-

-

-

Another solution

I created another import script using a different approach: it selects the last child (the mesh instance 3d) that I want to keep and replace the scene with only that node (the _post_import return the scene that will be saved in the asset)

@tool
extends EditorScenePostImport


const OBJECT_SCALE: float = 8


func _post_import(scene: Node) -> Object:
	var last_child: Node = get_last_child(scene)
	if last_child is MeshInstance3D:
		return last_child
	push_error("scene doesn't contain a mesh")
	return scene


func get_last_child(node: Node) -> Node:
	while node.get_child_count() > 0:
		if node.get_child(0):
			node = node.get_child(0)
		else:
			push_error("node doesn't have a child")
			break
	return node

Issue

the code kinda works, it removes the nodes I don’t want to keep, but it still makes the asset so big

on top of that, this code throws 2 errors that I don’t understand, with some attempts I found out that `node.get_child(0)` causes code causes the error

can someone explain me why and give me a solution?

Look in the nested import at every Node3D and check the scale. Every one of these nodes could have a transformation. If you remove these Nodes you have to assign the transformation to the mesh or the root node. I would do it all in Blender, clean them up there and apply the scale there, so you have a nice 1,1,1 scale and a correct sized object.

Using the ‘Set Mesh Save Path’ action in the Advanced Importer lets you save the mesh as a resource to work with. If you reimport the GLB/FBX file, the resources will update automatically. Then, you won’t even need the import script.

instead of using the code just make the chair local ( o remove ereditary) and delete the nodes.

AFAIK when made local, the mesh data will get stored inside the tscn. That’s something you generally don’t want. And you loose the connection to the glb or fbx, so the mesh wouldn’t be updated on a reexported version.

it never hapened to me tho.

It happened to me more than once by accident. That’s when Godot complains, that the tscn is very large and slow to load.

thank you so much, I forgot to check if the scales were uniform in all the nodes :sob:

since I have many assets and it’s not possible to do it manually, do you know how I can change properties of the import settings by code?

I know I can change the default import settings, but I need to change a specific property (adding physics to mesh) which is not displayed in the default properties panel, Idk why

I tried with code this way but it doesn’t work because the import properties aren’t accessible from the node

how can I do that? I can’t think of any other way

func _post_import(node: Node) -> Object:
    node.set("root_scale", OBJECT_SCALE)
    node.set("gltf/embedded_image_handling", 2)
    node.set("generate/physics", true)
    node.set("physics/body_type", 0)
    return node

Sorry, that’s outside of my knowledge. My intuition is, that _post_import is too late for this. The import is already done at this moment and a node has no node.set("generate/physics", true).
That could be the moment I would ask an LLM, but I don’t want to post generated answers here.
Maybe someone else has a solution for this.
Maybe remove the “Solution” tag, so others, who are looking for unsolved questions, could join this thread.