通过GDScript创建动画

huangapple go评论62阅读模式
英文:

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:

通过GDScript创建动画

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)
英文:

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:

通过GDScript创建动画

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)

答案1

得分: 2

Animation 在运行时被创建。

在远程选项卡中选择 AnimationPlayer 不会让你在动画面板或检查器中看到在运行时创建的 Animation。我能做到的最好的是在检查器中打开 AnimationLibrary,但它并不会暴露 Animations

这并不意味着动画不存在。如果动画正常播放且你没有收到任何错误输出,那么动画已经被正确创建。

你也许可以通过在调试器面板中设置断点并从中选择来在检查器中看到 Animation

当然,运行时创建的动画不会被保存,所以当游戏停止时它会丢失,并在你重新启动游戏时重新创建。

我怀疑你可能想在编辑器中创建动画,你可以通过创建一个 tool 脚本来实现。请参考在编辑器中运行代码

你也可能对ResourceSaver感兴趣。

英文:

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.

答案2

得分: 0

以下是您提供的代码的翻译:

感谢,"工具" 起作用了。有了它的帮助,我只意识到错误确切地在帧中,因为动画被创建了。但它只能运行一次,然后你必须重新启动项目 通过GDScript创建动画 我还看到动画名称不同,名称没有包含库的名称(不是 "das/base",而只是 "base")

(我不知道如何正常回复)

(以下是更改后的代码)

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)

请注意,我已根据您的请求删除了代码的注释部分。如果您需要进一步的翻译或解释,请随时提出。

英文:

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 通过GDScript创建动画 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)

huangapple
  • 本文由 发表于 2023年5月10日 19:28:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定