Search code examples
godotgdscript

Creating animations through a GDScript


I need to create an animation from an array, but it just isn't created in Godot, I don't see it in remote explorer.

This list is reduced by 10 times from the original, this is just an example. I had to convert the animation from another program to this form because it is impossible to export animation in it

Help me, I don't understand what the error is:

explorer_screenshot

extends Node3D

@onready var animplayer:AnimationPlayer=$fire


var animation_table=[{'rotation':Vector3(0,0,-1),'time':0.60,'position':Vector3(0,0,0),'name':'Hammer'},
{'rotation':Vector3(0,0,-1),'time':0.60,'position':Vector3(0,0,0),'name':'Handle'},
{'rotation':Vector3(0,0,-1),'time':0.63,'position':Vector3(0,0,0),'name':'Joint'},
{'rotation':Vector3(0,0,-1),'time':0.9,'position':Vector3(0,0,0),'name':'Barrel'}]

func _ready():
    var animname='base'
    var base_library=animplayer.get_animation_library('das')
    var new_animation=Animation.new()
    var added=base_library.add_animation(animname,new_animation)
    var animation=base_library.get_animation(animname)
    
    
    for table in animation_table:
        for type in ['position','rotation']:
            var track_path=table.name+':'+type
            var track_name=track_path+'_track'
            var a:String
            var anim_type='TYPE_'+type.to_upper()+'_3D'
            var track
            var has=animation.find_track(track_path,Animation[anim_type])
            print(has)
            
            if has:
                track=has
            else:
                track=animation.add_track(Animation[anim_type])
            
            animation.track_set_path(track,track_path)
            animation.track_insert_key(track,float(table.time),table[type])

    animplayer.play(animname)

Solution

  • The Animation is being created in runtime.

    Selecting the AnimationPlayer in the Remote tab won't allow you to see Animations created in runtime in the Animation panel nor the Inspector. The best I have been able to get is to open the AnimationLibrary in the inspector, but it does not expose the Animations.

    That does not mean the animation does not exist. If the animation plays correctly and you didn't get any error output then the animation was created correctly.

    You might be able to see the Animation in the Inspector using a breakpoint and selecting it from the Debugger panel.


    Of course, the animation being created in runtime is not being saved, so it is lost when the game stops, and created again when you start the game again.


    I suspect you might want to create the animation in the editor, which you could accomplish by making a tool script. See Running code in the editor.

    You might also be interested in ResourceSaver.