Programatically create a SpriteFrames resources from multiple spritesheets

Godot Version

v4.4.1.stable.official [49a5bc7b6]

Question

I can’t share too much info because of NDA.

I currently have a SpriteFrames resource which is being played by an AnimatedSprite2D node.
However, because I only need ONE animation per game (because the game is different every time you play it), and because we want to keep the size of the game as minimal as possible, I need to dynamically load the Spritesheets necessary, then using those, create the SpriteFrames resource when the game starts.

Each spritesheet file looks like this (this would therefore be loaded from an external URL)

I know that every single spritesheet is 3200x2520 in resolution, I know that every full animation requires exactly 12 spritesheets, and I know that each spritesheet contains 10 frames, I know that each frame should be exactly 800x840 in resolution, and I know that each final SpriteFrames resource should contain a total of 120 frames.

Question is, how could I do this? I know how to load an image from a URL, but how do I “cut it up” into individual frames programatically, and add each frame to a newly created SpriteFrames resource?

Thank you for your help.

SpriteFrames has an add_animation(name), and a add_animation_frame(name, texture, duration), so I imagine it would look something like:

# Untested, unoptimized, probably buggy...

func populate_anim(name: String, sheets: Array) -> SpriteFrames:
    var sf: SpriteFrames = SpriteFrames.new()
    if sheets.size() != 12: scream_and_die("Wrong number of textures!")

    sf.new_animation(name)
    for i in 12:
        for y in 3:
            for x in 4:
                var atlas: AtlasTexture = AtlasTexture.new()
                atlas.atlas = sheets[i]
                atlas.region = Rect2(800 * x, 840 * y, 800, 840)
                sf.new_animation_frame(name, atlas, 1.0)

    return sf
1 Like