├── .gitignore ├── LICENSE ├── README.md └── addons └── codeandweb.texturepacker ├── codeandweb.texturepacker_importer.gd ├── plugin.cfg └── texturepacker_import_spritesheet.gd /.gitignore: -------------------------------------------------------------------------------- 1 | # Godot 4+ specific ignores 2 | .godot/ 3 | 4 | # Godot-specific ignores 5 | .import/ 6 | export.cfg 7 | export_presets.cfg 8 | 9 | # Imported translations (automatically generated from CSV files) 10 | *.translation 11 | 12 | # Mono-specific ignores 13 | .mono/ 14 | data_*/ 15 | mono_crash.*.json 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright © 2018 Andreas Löw / CodeAndWeb GmbH 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the “Software”), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TexturePacker Importer 2 | 3 | This is a plugin for [Godot Engine](https://godotengine.org) to import sprite sheets 4 | generated with [TexturePacker](https://www.codeandweb.com/) as 5 | Godot `AtlasTexture`. 6 | 7 | **Note:** This plugin version is compatible with Godot 4.0 and newer. 8 | Use the version from the **godot-3** branch if your are using Godot 3. 9 | 10 | 11 | ## Installation 12 | 13 | Simply download it from [Godot Asset Library](https://godotengine.org/asset-library/asset/1503) 14 | 15 | Alternatively, download or clone this repository and copy the contents of the 16 | `addons` folder to your own project's `addons` folder. 17 | 18 | **Important**: Enable the plugin on the Project Settings. 19 | 20 | ## Features 21 | 22 | * Import sprite sheets as AtlasTextures 23 | * Supports trimmed sprites (margin) 24 | * Supports MultiPack 25 | 26 | ## Usage (once the plugin is enabled) 27 | 28 | 1. Create a sprite sheet in TexturePacker 29 | 2. Save the image and .tpsheet file in the godot asset folder 30 | 3. Watch Godot import it automatically. 31 | 32 | 33 | ## Known issues 34 | 35 | - TileSet import no longer supported: Godot 3 had an API where a tile could be 36 | retrieved by its name. This is no longer available in Godot 4. 37 | 38 | 39 | # Release notes 40 | 41 | ### 4.1.0 (2023-08-28) 42 | 43 | * Fixed problem when sprite sheet was updated 44 | * Improved error handling 45 | * Removed TileSet importer code 46 | 47 | ## 4.0.1 (2022-10-13) 48 | 49 | * The plugin now works with Godot 4 beta 2 50 | 51 | ## 4.0.0 (2022-10-04) 52 | 53 | * The plugin now works with Godot 4 54 | * The old version working with Godot 3 is now on the godot-3 branch 55 | 56 | ## 1.0.5 (2020-06-16) 57 | 58 | * Fixed syntax to support Godot 3.2.2 59 | * Fixed memory leak (thanks @2shady4u) 60 | * Support additional image formats: webp, pvr, tga (thanks @AntonSalazar) 61 | * Renamed **master** branch to **main** 62 | 63 | ## 1.0.4 (2018-12-11) 64 | 65 | * Fixed syntax to support Godot 3.1 66 | 67 | ## 1.0.3 (2018-10-05) 68 | 69 | * Reduced memory usage during import 70 | 71 | ## 1.0.2 (2018-04-18) 72 | 73 | * Sprite sheets can now be placed in sub folders 74 | 75 | ## 1.0.1 (2018-03-14) 76 | 77 | * Fixed order of import to prevent "No loader found on resources" error 78 | 79 | ## 1.0.0 (2018-03-12) 80 | 81 | * Initial release 82 | 83 | 84 | ## License 85 | 86 | [MIT License](LICENSE). Copyright (c) 2018 Andreas Loew / CodeAndWeb GmbH 87 | -------------------------------------------------------------------------------- /addons/codeandweb.texturepacker/codeandweb.texturepacker_importer.gd: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # 3 | # Copyright (c) 2018 Andreas Loew / CodeAndWeb GmbH www.codeandweb.com 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | @tool 24 | extends EditorPlugin 25 | 26 | var import_plugin_spritesheet = null 27 | 28 | func get_name(): 29 | return "TexturePacker Importer" 30 | 31 | 32 | func _enter_tree(): 33 | import_plugin_spritesheet = preload("res://addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd").new() 34 | add_import_plugin(import_plugin_spritesheet) 35 | 36 | 37 | func _exit_tree(): 38 | remove_import_plugin(import_plugin_spritesheet) 39 | import_plugin_spritesheet = null 40 | -------------------------------------------------------------------------------- /addons/codeandweb.texturepacker/plugin.cfg: -------------------------------------------------------------------------------- 1 | config_version=3 2 | [plugin] 3 | 4 | name="TexturePacker Importer" 5 | description="Importer for sprite sheets created using TexturePacker" 6 | version="4.1.0" 7 | author="Andreas Loew / CodeAndWeb GmbH" 8 | script="codeandweb.texturepacker_importer.gd" 9 | -------------------------------------------------------------------------------- /addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # 3 | # Copyright (c) 2018 Andreas Loew / CodeAndWeb GmbH www.codeandweb.com 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | @tool 24 | extends EditorImportPlugin 25 | 26 | 27 | enum Preset { PRESET_DEFAULT } 28 | 29 | 30 | func _get_importer_name(): 31 | return "codeandweb.texturepacker_import_spritesheet" 32 | 33 | 34 | func _get_visible_name(): 35 | return "SpriteSheet from TexturePacker" 36 | 37 | 38 | func _get_recognized_extensions(): 39 | return ["tpsheet"] 40 | 41 | 42 | func _get_save_extension(): 43 | return "res" 44 | 45 | 46 | func _get_resource_type(): 47 | return "Resource" 48 | 49 | 50 | func _get_preset_count(): 51 | return Preset.size() 52 | 53 | 54 | func _get_preset_name(preset): 55 | match preset: 56 | Preset.PRESET_DEFAULT: return "Default" 57 | 58 | 59 | func _get_import_options(path, preset_index): 60 | return [] 61 | 62 | 63 | func _get_option_visibility(path, option_name, options): 64 | return true 65 | 66 | 67 | func _get_import_order(): 68 | return 200 69 | 70 | func _get_priority(): 71 | return 1.0 72 | 73 | func _import(source_file, save_path, options, r_platform_variants, r_gen_files): 74 | print("Importing sprite sheet from "+source_file); 75 | 76 | var sheets = read_sprite_sheet(source_file) 77 | if not sheets: 78 | return ERR_PARSE_ERROR 79 | 80 | var sheetFolder = source_file.get_basename()+".sprites" 81 | create_folder(sheetFolder) 82 | 83 | for sheet in sheets.textures: 84 | var sheetFile = source_file.get_base_dir()+"/"+sheet.image 85 | var image = ResourceLoader.load(sheetFile, "ImageTexture") 86 | if not image: 87 | printerr("Failed to load image file: " + sheetFile) 88 | return ERR_FILE_NOT_FOUND 89 | 90 | create_atlas_textures(sheetFolder, sheet, image, r_gen_files) 91 | 92 | return ResourceSaver.save(Resource.new(), "%s.%s" % [save_path, _get_save_extension()]) 93 | 94 | 95 | func create_folder(folder): 96 | var dir := DirAccess.open("res://") 97 | if !dir.dir_exists(folder): 98 | if dir.make_dir_recursive(folder) != OK: 99 | printerr("Failed to create folder: " + folder) 100 | 101 | 102 | func create_atlas_textures(sheetFolder, sheet, image, r_gen_files): 103 | for sprite in sheet.sprites: 104 | if !create_atlas_texture(sheetFolder, sprite, image, r_gen_files): 105 | return false 106 | return true 107 | 108 | 109 | func create_atlas_texture(sheetFolder, sprite, image, r_gen_files): 110 | var name = sheetFolder+"/"+sprite.filename.get_basename()+".tres" 111 | var texture 112 | if ResourceLoader.exists(name, "AtlasTexture"): 113 | texture = ResourceLoader.load(name, "AtlasTexture") 114 | else: 115 | texture = AtlasTexture.new() 116 | 117 | texture.atlas = image 118 | texture.region = Rect2(sprite.region.x, sprite.region.y, sprite.region.w, sprite.region.h) 119 | texture.margin = Rect2(sprite.margin.x, sprite.margin.y, sprite.margin.w, sprite.margin.h) 120 | r_gen_files.push_back(name) 121 | return save_resource(name, texture) 122 | 123 | 124 | func save_resource(name, texture): 125 | create_folder(name.get_base_dir()) 126 | 127 | var status = ResourceSaver.save(texture, name) 128 | if status != OK: 129 | printerr("Failed to save resource "+name) 130 | return false 131 | return true 132 | 133 | 134 | func read_sprite_sheet(fileName): 135 | var file = FileAccess.open(fileName, FileAccess.READ) 136 | if not file: 137 | printerr("Failed to load "+fileName) 138 | return null 139 | 140 | var text = file.get_as_text() 141 | var dict = JSON.parse_string(text) 142 | if dict == null: 143 | printerr("Invalid json data in "+fileName) 144 | return dict 145 | --------------------------------------------------------------------------------