Advertisement
AssoAndrea

MaterialScript

May 6th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. TArray<UObject*> FMyPluginModule::ImportTexture(const TArray<FString>& Paths)
  2. {
  3.     for (FString Path : Paths)
  4.     {
  5.         Path = Path.Replace(TEXT("\\"), TEXT("/"));
  6.     }
  7.     /*FAssetToolsModule& AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools");
  8.     return AssetToolsModule.Get().ImportAssets(Paths, TEXT("/Game/Imported"));*/
  9.  
  10.    
  11.     UAutomatedAssetImportData* importData = NewObject<UAutomatedAssetImportData>();
  12.     importData->bReplaceExisting = true;
  13.     importData->DestinationPath = TEXT("/Game/Imported");
  14.     importData->Filenames = Paths;
  15.    
  16.  
  17.     FAssetToolsModule& AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools");
  18.     TArray<UObject*> importedAssets = AssetToolsModule.Get().ImportAssetsAutomated(importData);
  19.     return importedAssets;
  20. }
  21.  
  22. UMaterial* TryAddMaterial(TMap<FString, UMaterial*>& Materials,const FString Name, UMaterialFactoryNew* MaterialFactory)
  23. {
  24.     if (!Materials.Contains(Name))
  25.     {
  26.         UPackage* Package = CreatePackage(*(FString::Printf(TEXT("/Game/ImportedMaterial/%s"), *Name)));
  27.         UObject* NewMat = MaterialFactory->FactoryCreateNew(MaterialFactory->SupportedClass, Package, *Name, EObjectFlags::RF_Standalone | EObjectFlags::RF_Public, nullptr, GWarn);
  28.        
  29.         /*UMaterialInstanceConstantFactoryNew* InstanceFactory = NewObject<UMaterialInstanceConstantFactoryNew>();
  30.         FString InstanceName = Name + TEXT("_Instance");
  31.         UObject* Instance = InstanceFactory->FactoryCreateNew(InstanceFactory->SupportedClass, NewMat, *InstanceName, EObjectFlags::RF_Standalone | EObjectFlags::RF_Public, nullptr, GWarn);
  32.         UMaterialInstanceConstant* InstanceMat = Cast<UMaterialInstanceConstant>(Instance);*/
  33.        
  34.        
  35.         UMaterial* Material = Cast<UMaterial>(NewMat);
  36.        
  37.         Materials.Add(Name, Material);
  38.         FAssetRegistryModule::AssetCreated(Material);
  39.        
  40.         return Material;
  41.     }
  42.     return Materials[Name];
  43. }
  44.  
  45.  
  46. bool FMyPluginModule::Exec(UWorld* InWorld, const TCHAR* Cmd, FOutputDevice& Ar)
  47. {
  48.     if (FParse::Command(&Cmd, TEXT("MakeMaterial")))
  49.     {
  50.         FString Param;
  51.         if (FParse::Value(Cmd, TEXT("Path"), Param))
  52.         {
  53.             TArray<FString> FoundFiles = TArray<FString>();
  54.             FFileManagerGeneric FileManager = FFileManagerGeneric();
  55.             FileManager.FindFilesRecursive(FoundFiles, *Param, TEXT("*.png"), true, true, true);
  56.             FString BaseColor = "_BaseColor";
  57.             FString Normal = "_Normal";
  58.             FString ORM = "_OcclusionRoughnessMetallic";
  59.             FString Emissive = "_Emissive";
  60.             FString FileName;
  61.             TArray<UObject*> Imported = ImportTexture(FoundFiles);
  62.             TMap<FString,UMaterial*> Materials;
  63.             UMaterialFactoryNew* MatFactory = NewObject<UMaterialFactoryNew>();
  64.             for (UObject* Texture : Imported)
  65.             {
  66.                 FileName = Texture->GetName();
  67.                 //UE_LOG(LogTemp, Warning, TEXT("%s"), *Path);
  68.  
  69.                 if (FileName.Contains(BaseColor))
  70.                 {
  71.                     FString Name;
  72.                     FileName.Split(BaseColor, &Name, nullptr);
  73.                     UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
  74.                    
  75.                     Material->Modify(); //Start modify material
  76.                     UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
  77.                     TextureExpression->Texture  = Cast<UTexture>(Texture);
  78.                     Material->Expressions.Add(TextureExpression);
  79.                     Material->BaseColor.Expression = TextureExpression;
  80.                     Material->PostEditChange();
  81.                     Material->MarkPackageDirty();
  82.                 }
  83.                 if (FileName.Contains(Normal))
  84.                 {
  85.                     FString Name;
  86.                     FileName.Split(Normal, &Name, nullptr);
  87.                     UE_LOG(LogTemp, Error, TEXT("%s"), *Name);
  88.                    
  89.                     UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
  90.                    
  91.                     Material->Modify(); //Start modify material
  92.                     UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
  93.                     TextureExpression->SamplerType = EMaterialSamplerType::SAMPLERTYPE_Normal;
  94.                     TextureExpression->Texture = Cast<UTexture>(Texture);
  95.                     Material->Expressions.Add(TextureExpression);
  96.                     Material->Normal.Expression = TextureExpression;
  97.                     Material->PostEditChange();
  98.                     Material->MarkPackageDirty();
  99.                 }
  100.                 if (FileName.Contains(ORM))
  101.                 {
  102.                     UTexture* MatTex = Cast<UTexture>(Texture);
  103.                     MatTex->CompressionSettings = TextureCompressionSettings::TC_Default;
  104.                     MatTex->SRGB = false;
  105.                     //capire come collegare i parametri separati
  106.                     FString Name;
  107.                     FileName.Split(ORM, &Name, nullptr);
  108.                     UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
  109.  
  110.                     Material->Modify(); //Start modify material
  111.                     UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
  112.                     TextureExpression->Texture = MatTex;
  113.                     TextureExpression->SamplerType = EMaterialSamplerType::SAMPLERTYPE_LinearColor;
  114.                     TArray<FExpressionOutput> Out = TextureExpression->GetOutputs();
  115.  
  116.                    
  117.                     Material->Expressions.Add(TextureExpression);
  118.                     Material->AmbientOcclusion.Connect(1, TextureExpression);
  119.                     Material->Roughness.Connect(2, TextureExpression);
  120.                     Material->Metallic.Connect(3, TextureExpression);
  121.                    
  122.                     Material->PostEditChange();
  123.                     Material->MarkPackageDirty();
  124.                 }
  125.                 if (FileName.Contains(Emissive))
  126.                 {
  127.                     FString Name;
  128.                     FileName.Split(Emissive, &Name, nullptr);
  129.                     UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
  130.  
  131.                     Material->Modify(); //Start modify material
  132.                     UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
  133.                     TextureExpression->Texture = Cast<UTexture>(Texture);
  134.                     Material->Expressions.Add(TextureExpression);
  135.                     Material->EmissiveColor.Expression = TextureExpression;
  136.                     Material->PostEditChange();
  137.                     Material->MarkPackageDirty();
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     if (FParse::Command(&Cmd, TEXT("MakeMaterial")))
  143.     {
  144.         MakeMaterialExample();
  145.         return true;
  146.     }
  147.     return false;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement