Created
December 10, 2022 21:59
-
-
Save QueenOfSquiggles/8f9ec3e866a84bb0893742364e6c6507 to your computer and use it in GitHub Desktop.
Godot 4 Quick Asset Extraction
This file contains 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 EditorPlugin | |
func _enter_tree(): | |
add_tool_menu_item("Extract Asset Pack", _menu_extract_asset_pack) | |
func _exit_tree(): | |
pass | |
func _menu_extract_asset_pack() -> void: | |
print("Extracting asset pack!") | |
var cur_file := get_editor_interface().get_current_path() | |
var asset_pack_scene := load(cur_file) as PackedScene | |
if asset_pack_scene == null: | |
printerr("Currently selected file is not able to be loaded as a PackedScene, which is required to extract assets: %s" % cur_file) | |
return | |
var editable_scene :Node= asset_pack_scene.instantiate() | |
var children := editable_scene.get_children() | |
var parts_path := cur_file.get_base_dir().path_join("asset_pack") | |
DirAccess.make_dir_recursive_absolute(parts_path) | |
for c in children: | |
var data := PackedScene.new() | |
data.pack(_process_asset(c)) | |
var err := ResourceSaver.save(data, parts_path.path_join(str(c.name) + ".scn")) | |
if err == OK: | |
print("Extracted asset %s from pack %s" % [str(c.name), cur_file.get_file()]) | |
else: | |
printerr("Failed to extract asset %s from pack %s" % [str(c.name), cur_file.get_file()]) | |
func _process_asset(asset : Node) -> Node: | |
var spat := asset as Node3D | |
if spat == null: | |
printerr("Failed to process asset %s, only 3D meshes currently recognized") | |
return asset | |
spat.transform.origin = Vector3.ZERO | |
return spat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want a licensing term on this: CC0, use it for whatever, in whatever way you want, with or without credit to me. Honestly this kind of functionality feels like a public service. I might look into the actual Godot code to see if I could add this with more nuance to the engine