godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.48k stars 21.07k forks source link

The speed and loop properties did not take effect when converting the sprite image to SpriteFrames object and saving it #97802

Open deft663 opened 2 weeks ago

deft663 commented 2 weeks ago

Tested versions

Godot_v4.3-stable_win64

System information

window10

Issue description

The speed and loop properties did not take effect when converting the sprite image to SpriteFrames object and saving it

Steps to reproduce

func generate_animations(model_dictionary, save_path, animation_name):
    var groupDic: Dictionary = {}

    for filename in model_dictionary:
        var sp = filename.split("_")
        groupDic[sp[1]] = 1

    var sprite_frames = SpriteFrames.new()
    sprite_frames.remove_animation("default")

    for direction in groupDic.keys():
        var animation_direction = animation_name + "_" + direction

        var texture_arr = file_helper.get_atlas_texture_by_prefix(animation_direction, model_dictionary)

        sprite_frames.add_animation(animation_direction)

        if texture_arr.size() > 0:
            sprite_frames.set_animation_speed(animation_direction, texture_arr.size()/3 )   
        else:
            print("No frames found for animation direction: " + animation_direction)

        var loop:bool = false
        if animation_name == "stand" || animation_name == "walk":
            loop = true
        sprite_frames.set_animation_loop(animation_direction,loop)
        for texture in texture_arr:
            sprite_frames.add_frame(animation_direction, texture)

    # save
    var result = ResourceSaver.save(sprite_frames, save_path + "/" + animation_name + ".tres", ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS)
    print(str(result))

    # debug
    for anim_name in sprite_frames.get_animation_names():
        print("Animation: " + anim_name + ", Speed: " + str(sprite_frames.get_animation_speed(anim_name)) + ", Loop: " + str(sprite_frames.get_animation_loop(anim_name)))

Minimal reproduction project (MRP)

func generate_animations(model_dictionary, save_path, animation_name):
    var groupDic: Dictionary = {}

    for filename in model_dictionary:
        var sp = filename.split("_")
        groupDic[sp[1]] = 1

    var sprite_frames = SpriteFrames.new()
    sprite_frames.remove_animation("default")

    for direction in groupDic.keys():
        var animation_direction = animation_name + "_" + direction

        var texture_arr = file_helper.get_atlas_texture_by_prefix(animation_direction, model_dictionary)

        sprite_frames.add_animation(animation_direction)

        if texture_arr.size() > 0:
            sprite_frames.set_animation_speed(animation_direction, texture_arr.size()/3 )   
        else:
            print("No frames found for animation direction: " + animation_direction)

        var loop:bool = false
        if animation_name == "stand" || animation_name == "walk":
            loop = true
        sprite_frames.set_animation_loop(animation_direction,loop)
        for texture in texture_arr:
            sprite_frames.add_frame(animation_direction, texture)

    # save
    var result = ResourceSaver.save(sprite_frames, save_path + "/" + animation_name + ".tres", ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS)
    print(str(result))

    # debug
    for anim_name in sprite_frames.get_animation_names():
        print("Animation: " + anim_name + ", Speed: " + str(sprite_frames.get_animation_speed(anim_name)) + ", Loop: " + str(sprite_frames.get_animation_loop(anim_name)))

Bugsquad edit: Fix codeblock formatting.

SourceOfHTML commented 2 weeks ago

Hello, I'd like to help, though I do have a request, and in general, a suggestion for issues after this one:

The steps to reproduce need to be more descriptive than one script (Where do I put this script, how do I configure the sprite image, etc.)

The MRP is a project wherein you follow The Steps Reproduce, but in such a way that the project has exactly what is needed to reproduce the problem, no more and no less.

So far, with the information given, I can't do much except evaluate your GDScript by eye.

Note: I'm a new contributor, I might be somewhat off with these descriptions of how issues generally work, but i'm working off of other issues I've seen.

deft663 commented 2 weeks ago

Hello, what is written at the top of this code @tool extends EditorScript As mentioned above, before saving the SpriteFrames object, I set the speed attribute and loop attribute for each animation, and the final printed value was also what I expected. However, after using the load function to load the saved tres file, I printed the speed and loop attributes with default values, such as the loop attribute always being true