2

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)
display
  • 35
  • 3

2 Answers2

2

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.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • thanks, "tool" works. With the help of it, I realized only that the error is exactly in the frames, since the animation was created. But it works only once, then you have to restart the project :( I also saw that the animation names are different, the name was without the name of the library (not "das / BASE", but simply "BASE") – display May 10 '23 at 17:15
  • @display You might want to move the code away from `_ready`. If you want to be able to trigger it multiple times. A common pattern among Godot developers is to export a `bool` variable, so it shows in the Inspector, and use the setter to trigger whatever code we want to run. We would also set the property back to false when the process is done (you can call `notify_property_list_changed` to have the inspector refresh). For more complex stuff you might want to look into making an `EditorPlugin`. – Theraot May 10 '23 at 17:20
0

thanks, "tool" works. With the help of it, I realized only that the error is exactly in the frames, since the animation was created. But it works only once, then you have to restart the project :( I also saw that the animation names are different, the name was without the name of the library (not "das/base", but simply "base")

(i dont know how to reply normally)

(Here is changed code)

func _ready():
    var animname='base'
    var library_name='das'
    var base_library=animplayer.get_animation_library(library_name)
    var new_animation=Animation.new()
    base_library.add_animation(animname,new_animation)
    var animation=base_library.get_animation(animname)

    for table in animation_table:
        if find_child(table.name):
            for type in['position','rotation']:
                var track_path=table.name+':'+type
                var anim_type='TYPE_'+type.to_upper()+'_3D'
                var track
                var has=animation.find_track(track_path,Animation[anim_type])

                if has!=-1:
                    track=has
                else:
                    track=animation.add_track(Animation[anim_type])

                print(track,table[type])

                animation.track_set_path(track,track_path)
                animation.track_insert_key(track,float(table.time),table[type])

    animplayer.play(library_name+'/'+animname)
display
  • 35
  • 3