Godot导入FBX自动生成Mesh资源文件

前言

godot的3D粒子特效需要用到Mesh资源,但是导入OBJ会遇到无法保存材质的问题,我用的Godot3.5

所以使用FBX导入,但是godot会直接把fbx文件变成场景文件,如果粒子系统需要使用的话还得手动从场景中找到模型并手动保存。

这样感觉很麻烦,所以我就写了一个小插件,自动从fbx场景中提取第一个mesh,如果有需要也可以自行修改,主要是这个方法和思路。

代码

tool
extends EditorPlugin

static func get_children_by_type(node:Node, type)->Array:
	var res:=[]
	for n in node.get_children():
		if n is type:
			res.push_back(n)
	return res

func _enter_tree():
	var fs_dock: = get_editor_interface().get_file_system_dock()
	var popup_menus =  get_children_by_type(fs_dock, PopupMenu)
	popup_menus[-1].connect("about_to_show", self, "on_context_menu_show",[popup_menus[-1]])
	popup_menus[-1].connect("id_pressed", self, "on_context_menu_id_pressed",[popup_menus[-1]])


func on_context_menu_show(menu:PopupMenu):
	var cur_path:String = get_editor_interface().get_current_path()
	if not cur_path: return
	
	var ext = cur_path.get_extension()
	if ext == "fbx":
		menu.add_separator()
		menu.add_item("生成Mesh")


func on_context_menu_id_pressed(id:int, menu:PopupMenu):
	var cur_path:String = get_editor_interface().get_current_path()
	#获取fbx文件的绝对路径(仅编辑器状态下可用)
	var path = ProjectSettings.globalize_path(cur_path)
	print("读取文件路径:" + cur_path)
	var mesh_tree = load(cur_path).instance()
	var root_node = mesh_tree.get_children()[0]
	var mesh_instance:MeshInstance = root_node.get_children()[0]
	var mesh_instance_mesh:ArrayMesh = mesh_instance.mesh
	
	var out_path:String = path.split(".fbx")[0] + ".tres"
	print("文件保存路径:" + out_path)
	var err = ResourceSaver.save(out_path, mesh_instance_mesh)
	print(err)
	
	


func _exit_tree():
	pass

这样鼠标右键fbx文件就能看到最下面多出来一个按钮,点击一下就能提取并生成文件了。

目前缺陷

只能提取第一个模型,最好是做一个编辑器界面能自由选择导出的哪个模型,有些模型是嵌套关系的,因为我目前只用到粒子模型,比较简单,所以暂时能胜任。

猜你喜欢

转载自blog.csdn.net/u012863565/article/details/128885045