UE5 程序化批量导出staticmesh为FBX

// Fill out your copyright notice in the Description page of Project Settings.


#include "UpdateAssetVersionLibrary.h"
#include "Runtime\Core\Public\Misc\FileHelper.h"
#include "Runtime\Core\Public\Misc\Paths.h"
#include "Misc\Paths.h"
#include "EdGraphUtilities.h"
#include "MaterialGraph/MaterialGraphNode.h"
#include "Windows/WindowsPlatformApplicationMisc.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "HAL\FileManagerGeneric.h"
#include "GenericPlatform/GenericPlatformApplicationMisc.h"
#include "MaterialGraph/MaterialGraphSchema.h"
#include "Runtime/Engine/Public/ImageUtils.h"
#include "Kismet\KismetSystemLibrary.h"
#include "Developer\DesktopPlatform\Public\IDesktopPlatform.h"
#include "Developer\DesktopPlatform\Public\DesktopPlatformModule.h"
#include "MaterialGraph/MaterialGraphNode_Comment.h"

FString UUpdateAssetVersionLibrary::OpenFileDialog(FString Dialog, FString OpenPath) 
{
	IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
	FString OutFolderName;
	FString DefaultFile;
	FString FileType = FString("*.uasset");
	if (DesktopPlatform != nullptr) {
		if (DesktopPlatform->OpenDirectoryDialog(nullptr, Dialog, OpenPath, OutFolderName)) {
			OutFolderName.Append("/");
			return OutFolderName;
		}
	}
	return FString();
}

FString UUpdateAssetVersionLibrary::ConvertObsolutePathToRelativePath(FString ObPath, FString TailPath) 
{
	FString HeadPath = UKismetSystemLibrary::GetProjectContentDirectory();
	for (uint16 Count = 0; Count < HeadPath.Len(); Count++) {
		ObPath.RemoveAt(0);
	}
	FString HeadDir = "/Script/Engine.StaticMesh'/Game/";
	HeadDir.Append(ObPath);
	for (uint8 Count = 0; Count < 7; Count++) {
		HeadDir.RemoveAt(HeadDir.Len() - 1);
		TailPath.RemoveAt(TailPath.Len() - 1);
	}
	HeadDir.Append("." + TailPath + "'");
	return HeadDir;
}

void UUpdateAssetVersionLibrary::ExportStaticMeshToFBX(FString Path) 
{
	//UStaticMesh* StaticMesh = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Game/FPWeapon/Mesh/FirstPersonProjectileMesh.FirstPersonProjectileMesh'"));
	TArray<UObject*> ExportMesh;
	//ExportMesh.Add(StaticMesh);
	IAssetTools& assetTools = IAssetTools::Get();
	IFileManager& FileManager = IFileManager::Get();
	FFileManagerGeneric* FileManagerGeneric = new FFileManagerGeneric();
	TArray<FString> AssetNames;
	TArray<FString> AllStaticMeshUassetPath;
	TArray<FString> IdentifyPath;
	if(FileManagerGeneric != nullptr)
		FileManagerGeneric->FindFiles(AssetNames, *Path, TEXT(""));
	if (AssetNames.Num() != 0) {
		for (auto AssetIter : AssetNames) {
			FString RelativePath = ConvertObsolutePathToRelativePath(Path + AssetIter, AssetIter);

			AllStaticMeshUassetPath.AddUnique(RelativePath);
		}
		for (auto PathIter : AllStaticMeshUassetPath) {
			//if (FileManager.FileExists(*PathIter)) {
			UStaticMesh* StaticMesh = LoadObject<UStaticMesh>(nullptr, *PathIter);
			if (StaticMesh->IsValidLowLevel()) {
				ExportMesh.Add(StaticMesh);
			}
		}
	}
	else {
		return;
	}
	//FString("D:/ExportAssets")
	assetTools.ExportAssetsWithDialog(ExportMesh, true);
}

bool UUpdateAssetVersionLibrary::WriteToTXT(FString Information, FString MaterialName)
{
	uint16 Number = 0;
	FString Dialog;
	FString OutFolderName;
	FString OpenPath = UKismetSystemLibrary::GetProjectContentDirectory();
	IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
	DesktopPlatform->OpenDirectoryDialog(nullptr, Dialog, OpenPath, OutFolderName);
	OutFolderName += ("/" + MaterialName);
	while (1) {
		FString PathTemp = OutFolderName + "_" + FString::FromInt(Number) + ".txt";
		if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*PathTemp)) {
			Number++;
		}
		else {
			OutFolderName = PathTemp;
			break;
		}
	}
	bool isSuccess = FFileHelper::SaveStringToFile(Information, *OutFolderName);

	return isSuccess;
}


vo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值