Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refs #8][Godot 3] Add AnimationPlayer import #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ Imports `.pxo` files from [Pixelorama](https://orama-interactive.itch.io/pixelor

## Status

The plugin provides two import types:
The plugin provides three import types:

**Single Image**: All Cels are overlaid into one image per frame as a `1xN` StreamTexture PNG with the 2D Pixel Texture preset.

**SpriteFrames**: Animation tags are used to create a SpriteFrames resource from the pxo file, which can be used with AnimatedSprite. However, due to a resource reload bug in Godot, any changes to the pxo file are not visible in the editor but by either launching the game or restarting the editor, the edited textures are visible.

**Sprite & AnimationPlayer**: Animation tags are used to create a Sprite and AnimationPlayer scene from the pxo file.

There is also an Inspector plugin which lets you open Pixelorama from within the Godot editor. When you have a .pxo file open in the Inspector, click on "Open in Pixelorama" to launch Pixelorama. It requires a one-time configuration of the location of the Pixelorama binary. Linux/BSD and Windows are supported as of now

## Usage
Expand Down
196 changes: 196 additions & 0 deletions addons/godot_pixelorama_importer/animation_player_import.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
tool
extends EditorImportPlugin

const VISIBLE_NAME := "Sprite & AnimationPlayer"

var editor: EditorInterface


func _init(editor_interface):
editor = editor_interface


func get_importer_name():
return "com.technohacker.pixelorama.animationplayer"


func get_visible_name():
return VISIBLE_NAME


func get_recognized_extensions():
return ["pxo"]


func get_save_extension():
return "tscn"


func get_resource_type():
return "PackedScene"


func get_import_options(_preset):
var default_scale: Vector2 = ProjectSettings.get_setting("pixelorama/default_scale")
var default_external_save: bool = ProjectSettings.get_setting(
"pixelorama/default_animation_external_save"
)
var default_external_save_path: String = ProjectSettings.get_setting(
"pixelorama/default_animation_external_save_path"
)

return [
{"name": "Sprite2D", "default_value": false, "usage": PROPERTY_USAGE_GROUP},
{"name": "scale", "default_value": default_scale},
{"name": "Animation", "default_value": false, "usage": PROPERTY_USAGE_GROUP},
# (1 << 16) = PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED, but not exported in GDscript.
{
"name": "external_save",
"default_value": default_external_save,
"usage": PROPERTY_USAGE_DEFAULT | (1 << 16)
},
{
"name": "external_save_path",
"default_value": default_external_save_path,
"property_hint": PROPERTY_HINT_DIR
},
]


func get_option_visibility(option, options):
if option == "external_save_path" and options.has("external_save"):
return options["external_save"]
return true


func get_preset_count():
return 0


func get_priority() -> float:
var default_import_type: String = ProjectSettings.get_setting("pixelorama/default_import_type")
if default_import_type == get_visible_name():
return 2.0
return 1.0


func import(source_file, save_path, options, _r_platform_variants, gen_files):
"""
Main import function. Reads the Pixelorama project and creates the animation player resource
"""

var spritesheet_path = "%s.spritesheet" % [save_path]

# Open the project
var load_res = preload("./util/read_pxo_file.gd").read_pxo_file(source_file, spritesheet_path)

if load_res.error != OK:
printerr("Project Load Error")
return load_res.error

var project = load_res.value

var base_animation_path: String = options.external_save_path
if base_animation_path == "" and options.external_save:
base_animation_path = source_file.get_base_dir() + source_file.get_file().get_basename()
var directory = Directory.new()
if not directory.dir_exists(base_animation_path):
var tmp = directory.make_dir(base_animation_path)
base_animation_path = base_animation_path + "/"

# Note the spritesheet
spritesheet_path = "%s.stex" % spritesheet_path
gen_files.push_back(spritesheet_path)

# Load the spritesheet as a .stex
var spritesheet_tex = StreamTexture.new()
spritesheet_tex.load(spritesheet_path)

# create the Sprite
var sprite := Sprite.new()
sprite.texture = spritesheet_tex
sprite.name = source_file.get_file().get_basename()
sprite.apply_scale(options.scale)
sprite.hframes = project.frames.size()

# create the AnimationPlayer
var animation_player := AnimationPlayer.new()
sprite.add_child(animation_player)
animation_player.name = "AnimationPlayer"
animation_player.owner = sprite # for PackedScene

# add some default animations
if project.tags.size() == 0:
# No tags, put all in default
project.tags.append({"name": "default", "from": 1, "to": project.frames.size()})
# puts a RESET track
project.tags.append({"name": "RESET", "from": 1, "to": 1})

var err: int # Error enum
# import all animations
for tag in project.tags:
var animation: Animation
var animation_path: String
if options.external_save:
# in case the animation is already save, try to load it
animation_path = "%s/%s.tres" % [base_animation_path, tag.name]
var file = File.new()
if file.file_exists(animation_path):
animation = load(animation_path)
else:
animation = Animation.new()
animation.resource_path = animation_path
else:
animation = Animation.new()
animation_player.add_animation(tag.name, animation)

var track_index := animation.find_track(".:frame")
if track_index != -1:
# track exist, remove it to add a fresh one
animation.remove_track(track_index)

# add the track for the frame
track_index = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(track_index, ".:frame")
animation.track_set_interpolation_loop_wrap(track_index, false)

# insert the new animation keys
var time := 0.0
for frame in range(tag.from - 1, tag.to):
animation.track_insert_key(track_index, time, frame)
time += 1.0 / (project.fps / project.frames[frame].duration)

# loop handling
if (
tag.name.begins_with("loop")
or tag.name.begins_with("cycle")
or tag.name.ends_with("loop")
or tag.name.ends_with("cycle")
):
animation.loop = true

# update/set the length
animation.length = time

if options.external_save:
err = ResourceSaver.save(animation_path, animation)
if err != OK:
printerr("Error saving Animation %s: error %s" % [tag.name, err])
return err
gen_files.push_back(animation_path)

var scene = PackedScene.new()
err = scene.pack(sprite)
if err != OK:
printerr("Error creating PackedScene")
return err

var packed_scene_path = "%s.%s" % [save_path, get_save_extension()]
err = ResourceSaver.save(packed_scene_path, scene)
if err != OK:
printerr("Error saving PackedScene")
return err
gen_files.push_back(packed_scene_path)

editor.get_inspector().refresh()
return OK
51 changes: 50 additions & 1 deletion addons/godot_pixelorama_importer/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,57 @@ func _enter_tree() -> void:

import_plugins = [
preload("single_image_import.gd").new(),
preload("spriteframes_import.gd").new(get_editor_interface())
preload("spriteframes_import.gd").new(get_editor_interface()),
preload("animation_player_import.gd").new(get_editor_interface())
]

var hint_string := []
for plugin in import_plugins:
hint_string.append(plugin.VISIBLE_NAME)

var property_infos = [
{
"default": "Single Image",
"property_info":
{
"name": "pixelorama/default_import_type",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_ENUM,
"hint_string": ",".join(hint_string) # "Single Image,SpriteFrames,Sprite & AnimationPlayer"
}
},
{
"default": Vector2.ONE,
"property_info":
{
"name": "pixelorama/default_scale",
"type": TYPE_VECTOR2,
}
},
{
"default": false,
"property_info":
{
"name": "pixelorama/default_animation_external_save",
"type": TYPE_BOOL,
}
},
{
"default": "",
"property_info":
{
"name": "pixelorama/default_animation_external_save_path",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_DIR
}
}
]

for pi in property_infos:
if !ProjectSettings.has_setting(pi.property_info.name):
ProjectSettings.set_setting(pi.property_info.name, pi.default)
ProjectSettings.add_property_info(pi.property_info)

for plugin in import_plugins:
add_import_plugin(plugin)

Expand Down
11 changes: 10 additions & 1 deletion addons/godot_pixelorama_importer/single_image_import.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
tool
extends EditorImportPlugin

const VISIBLE_NAME := "Single Image"


func get_importer_name():
return "com.technohacker.pixelorama"


func get_visible_name():
return "Single Image"
return VISIBLE_NAME


func get_recognized_extensions():
Expand Down Expand Up @@ -35,6 +37,13 @@ func get_preset_count():
return 0


func get_priority() -> float:
var default_import_type: String = ProjectSettings.get_setting("pixelorama/default_import_type")
if default_import_type == get_visible_name():
return 2.0
return 1.0


func import(source_file, save_path, _options, _r_platform_variants, _r_gen_files):
"""
Main import function. Reads the Pixelorama project and extracts the PNG image from it
Expand Down
17 changes: 13 additions & 4 deletions addons/godot_pixelorama_importer/spriteframes_import.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
tool
extends EditorImportPlugin

const VISIBLE_NAME := "SpriteFrames"

var editor: EditorInterface


Expand All @@ -13,7 +15,7 @@ func get_importer_name():


func get_visible_name():
return "SpriteFrames"
return VISIBLE_NAME


func get_recognized_extensions():
Expand All @@ -30,7 +32,7 @@ func get_resource_type():


func get_import_options(_preset):
return [{"name": "animation_fps", "default_value": 6}]
return []


func get_option_visibility(_option, _options):
Expand All @@ -41,7 +43,14 @@ func get_preset_count():
return 0


func import(source_file, save_path, options, _r_platform_variants, r_gen_files):
func get_priority() -> float:
var default_import_type: String = ProjectSettings.get_setting("pixelorama/default_import_type")
if default_import_type == get_visible_name():
return 2.0
return 1.0


func import(source_file, save_path, _options, _r_platform_variants, r_gen_files):
"""
Main import function. Reads the Pixelorama project and creates the SpriteFrames resource
"""
Expand Down Expand Up @@ -78,7 +87,7 @@ func import(source_file, save_path, options, _r_platform_variants, r_gen_files):

for tag in project.tags:
frames.add_animation(tag.name)
frames.set_animation_speed(tag.name, options.animation_fps)
frames.set_animation_speed(tag.name, project.fps)

for frame in range(tag.from, tag.to + 1):
var image_rect := Rect2(Vector2((frame - 1) * frame_size.x, 0), frame_size)
Expand Down