Guest User

DDS Batch Processor w Texconv

a guest
Oct 12th, 2024
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Specify the path to Texconv.exe
  2. $texconvPath = "C:\path\to\texconv.exe"
  3.  
  4. # Path to the folder with DDS files
  5. $sourceFolder = "C:\path\to\dds\files\Folder1"   # For example: C:\textures\Folder1
  6. $outputFolder = "C:\path\to\output\folder"
  7. $tempOutputFile = Join-Path -Path $outputFolder -ChildPath "temp_output.txt"  # Temporary file in the output folder
  8.  
  9. # Ensure the output folder exists
  10. if (-not (Test-Path -Path $outputFolder)) {
  11.     New-Item -Path $outputFolder -ItemType Directory
  12. }
  13.  
  14. # Get all DDS files from the folder and subfolders
  15. $ddsFiles = Get-ChildItem -Path $sourceFolder -Filter *.dds -Recurse
  16.  
  17. # File processing loop
  18. foreach ($file in $ddsFiles) {
  19.     # Get the filename without extension and convert it to lowercase
  20.     $fileBaseName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name).ToLower()
  21.  
  22.     # Check the current format using texconv and output it to a temporary file
  23.     & $texconvPath -nologo -dx10 $file.FullName 2>&1 | Out-File -FilePath $tempOutputFile
  24.  
  25.     # Read the temp output file
  26.     $formatCheckOutput = Get-Content $tempOutputFile
  27.  
  28.     # Find the line that starts with 'reading'
  29.     $readingLine = $formatCheckOutput | Where-Object { $_ -like "reading*" }
  30.  
  31.     # Check if the reading line contains 'B8G8R8A8_UNORM'
  32.     if ($readingLine -notlike "*B8G8R8A8_UNORM*") {
  33.         Write-Host "Skipping file $file.Name: not in B8G8R8A8_UNORM format"
  34.         continue  # Skip this file
  35.     }
  36.  
  37.     # Calculate the relative path from the parent folder "Folder1" to the file
  38.     $relativePath = $file.FullName.Substring((Get-Item $sourceFolder).Parent.FullName.Length).TrimStart('\')
  39.  
  40.     # Path to the output file (preserving the full folder structure)
  41.     $outputFilePath = Join-Path -Path $outputFolder -ChildPath $relativePath
  42.     $outputDir = Split-Path $outputFilePath -Parent
  43.  
  44.     # Ensure the output directory structure exists
  45.     if (-not (Test-Path -Path $outputDir)) {
  46.         New-Item -Path $outputDir -ItemType Directory -Force
  47.     }
  48.  
  49.     # Determine which compression format to use
  50.     if ($fileBaseName.EndsWith("_ma") -or $fileBaseName.EndsWith("_ct") -or $fileBaseName.EndsWith("_eg") -or $fileBaseName.EndsWith("_mb") -or $fileBaseName.EndsWith("_m")) {
  51.         $compressionFormat = "BC3_UNORM"
  52.     } else {
  53.         $compressionFormat = "BC1_UNORM"
  54.     }
  55.  
  56.     Write-Host "Processing file $file.Name with compression $compressionFormat"
  57.  
  58.     # Run texconv with the appropriate parameters and allow overwriting existing files
  59.     & $texconvPath -f $compressionFormat -y -o $outputDir $file.FullName
  60.  
  61.     # Clean up any temporary DDS files accidentally saved near texconv.exe
  62.     $tempDDSFile = Join-Path -Path (Split-Path $texconvPath -Parent) -ChildPath $file.Name
  63.     if (Test-Path -Path $tempDDSFile) {
  64.         Remove-Item -Path $tempDDSFile -Force
  65.         Write-Host "Deleted temporary file: $tempDDSFile"
  66.     }
  67. }
  68.  
  69. # Delete the temporary output file if it exists
  70. if (Test-Path -Path $tempOutputFile) {
  71.     Remove-Item -Path $tempOutputFile -Force
  72.     Write-Host "Deleted temporary output file: $tempOutputFile"
  73. }
  74.  
  75. Write-Host "All files have been processed!"
Advertisement
Add Comment
Please, Sign In to add comment