Created
May 15, 2024 06:33
-
-
Save thayol/9c9df81f85b776adc7246ca58971ddf7 to your computer and use it in GitHub Desktop.
Godot glTF import script: extra root node fix (reparent/remove)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tool | |
extends EditorScenePostImport | |
func _post_import(scene): | |
var fake_root = scene.get_child(0) # glTF's root "scene" node | |
# Remove the wrongly imported fake root node | |
reparent_children(fake_root, scene) | |
fake_root.queue_free() | |
# Rename "Node" based on the file name | |
scene.name = basename().replace("-", "_").to_pascal_case() | |
return scene | |
func reparent_children(node, new_parent): | |
for child in node.get_children(): | |
reparent(child, new_parent) | |
func reparent(node, new_parent): | |
var old_parent = node.get_parent() | |
node.owner = null | |
old_parent.remove_child(node) | |
new_parent.add_child(node) | |
node.owner = new_parent | |
func basename(): | |
return get_source_file().get_file().get_basename() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment