strip blendfile prefixes from common imgs
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -89,13 +89,51 @@ function Move-FilesToCommon {
|
||||
}
|
||||
}
|
||||
|
||||
# Function to extract suffix after blendfile prefix (e.g., "Demarco_Std_Teeth_ao.jpg" -> "Std_Teeth_ao.jpg")
|
||||
# Only strips prefixes that are in the $ValidPrefixes list
|
||||
function Get-FileNameWithoutPrefix {
|
||||
param(
|
||||
[string]$FileName,
|
||||
[string[]]$ValidPrefixes
|
||||
)
|
||||
|
||||
# Validate input
|
||||
if ([string]::IsNullOrWhiteSpace($FileName)) {
|
||||
return $FileName
|
||||
}
|
||||
|
||||
# If no valid prefixes provided, return original filename
|
||||
if ($null -eq $ValidPrefixes -or $ValidPrefixes.Count -eq 0) {
|
||||
return $FileName
|
||||
}
|
||||
|
||||
# Use Split instead of regex for robustness (avoids $matches variable issues in loops)
|
||||
# Split by the first underscore only (limit to 2 parts)
|
||||
$parts = $FileName.Split('_', 2)
|
||||
|
||||
if ($parts.Length -eq 2) {
|
||||
$prefix = $parts[0]
|
||||
$suffix = $parts[1]
|
||||
|
||||
# Only strip if the prefix is in the valid prefixes list
|
||||
if ($ValidPrefixes -contains $prefix -and -not [string]::IsNullOrWhiteSpace($suffix)) {
|
||||
return $suffix
|
||||
}
|
||||
}
|
||||
|
||||
# No valid prefix found or suffix is empty, return original filename
|
||||
return $FileName
|
||||
}
|
||||
|
||||
# Function to process duplicate group
|
||||
function Process-DuplicateGroup {
|
||||
param(
|
||||
[array]$Files,
|
||||
[string]$CommonPath,
|
||||
[string]$DuplicatesPath,
|
||||
[hashtable]$FilesInCommon
|
||||
[hashtable]$FilesInCommon,
|
||||
[switch]$StripPrefix,
|
||||
[string[]]$ValidPrefixes = @()
|
||||
)
|
||||
|
||||
$movedCount = 0
|
||||
@@ -109,10 +147,72 @@ function Process-DuplicateGroup {
|
||||
}
|
||||
}
|
||||
|
||||
# Validate files array
|
||||
if ($null -eq $Files -or $Files.Count -eq 0) {
|
||||
return @{
|
||||
MovedCount = 0
|
||||
DuplicateCount = 0
|
||||
}
|
||||
}
|
||||
|
||||
# Multiple files with same checksum (duplicates)
|
||||
# Move first file to \common
|
||||
# Determine the filename to use
|
||||
$firstFile = $Files[0].File
|
||||
if ($null -eq $firstFile -or [string]::IsNullOrWhiteSpace($firstFile.Name)) {
|
||||
Write-Warning "Invalid file object in duplicate group"
|
||||
return @{
|
||||
MovedCount = 0
|
||||
DuplicateCount = 0
|
||||
}
|
||||
}
|
||||
|
||||
$fileName = $firstFile.Name
|
||||
|
||||
# If StripPrefix is enabled, always strip prefix from the filename
|
||||
if ($StripPrefix) {
|
||||
Write-Host " [DEBUG] StripPrefix enabled for group with $($Files.Count) file(s)" -ForegroundColor Magenta
|
||||
# Always try to strip prefix - if file has a prefix, it will be removed
|
||||
$strippedName = Get-FileNameWithoutPrefix -FileName $firstFile.Name -ValidPrefixes $ValidPrefixes
|
||||
|
||||
Write-Host " [DEBUG] Original: '$($firstFile.Name)' -> Stripped: '$strippedName'" -ForegroundColor Gray
|
||||
|
||||
# Use stripped name if it's different from original (prefix was removed)
|
||||
# Otherwise keep original (file had no prefix to strip)
|
||||
if ($strippedName -ne $firstFile.Name) {
|
||||
$fileName = $strippedName
|
||||
Write-Host " [DEBUG] Using stripped name: '$fileName'" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " [DEBUG] No prefix found, keeping original: '$fileName'" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# If we have multiple files with same checksum, try to find common suffix
|
||||
# This handles cases where files from different blendfiles share the same texture
|
||||
if ($Files.Count -gt 1) {
|
||||
$allSuffixes = @()
|
||||
foreach ($fileObj in $Files) {
|
||||
# $fileObj is a PSCustomObject with .File property, so access .File.Name
|
||||
$suffix = Get-FileNameWithoutPrefix -FileName $fileObj.File.Name -ValidPrefixes $ValidPrefixes
|
||||
$allSuffixes += $suffix
|
||||
}
|
||||
|
||||
Write-Host " [DEBUG] All suffixes: $($allSuffixes -join ', ')" -ForegroundColor Gray
|
||||
|
||||
# If all files strip to the same suffix, use that
|
||||
$uniqueSuffixes = @($allSuffixes | Select-Object -Unique)
|
||||
if ($uniqueSuffixes.Count -eq 1 -and $uniqueSuffixes[0] -ne $firstFile.Name) {
|
||||
$fileName = [string]$uniqueSuffixes[0]
|
||||
Write-Host " [DEBUG] All files share common suffix, using: '$fileName'" -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host " [DEBUG] Final filename: '$fileName'" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
# Ensure fileName is never null or empty
|
||||
if ([string]::IsNullOrWhiteSpace($fileName)) {
|
||||
$fileName = $firstFile.Name
|
||||
}
|
||||
|
||||
$destinationPath = Join-Path -Path $CommonPath -ChildPath $fileName
|
||||
|
||||
# Handle name conflicts for the first file
|
||||
@@ -247,6 +347,14 @@ if ($null -eq $blendfileFolders -or $blendfileFolders.Count -eq 0) {
|
||||
Write-Host ""
|
||||
Write-Host "=== PASS 2: Inter-Blendfile Processing ===" -ForegroundColor Cyan
|
||||
|
||||
# Build list of valid blendfile prefixes from folder names
|
||||
$validBlendfilePrefixes = @()
|
||||
$blendfileFoldersForPrefixes = Get-ChildItem -Path $textureFolderPath -Directory | Where-Object { $_.Name -ne "common" }
|
||||
if ($null -ne $blendfileFoldersForPrefixes) {
|
||||
$validBlendfilePrefixes = $blendfileFoldersForPrefixes | ForEach-Object { $_.Name }
|
||||
Write-Host "Valid blendfile prefixes: $($validBlendfilePrefixes -join ', ')" -ForegroundColor Gray
|
||||
}
|
||||
|
||||
# Get all remaining files (excluding all \common folders)
|
||||
Write-Host "Collecting remaining files..." -ForegroundColor Yellow
|
||||
$remainingFiles = Get-ChildItem -Path $textureFolderPath -Recurse -File | Where-Object { $_.FullName -notlike "*\common\*" }
|
||||
@@ -299,7 +407,8 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
|
||||
}
|
||||
|
||||
# Multiple files with same checksum (duplicates across blendfiles)
|
||||
$result = Process-DuplicateGroup -Files $files -CommonPath $rootCommonPath -DuplicatesPath $rootDuplicatesPath -FilesInCommon $filesInRootCommon
|
||||
# Use StripPrefix to remove blendfile prefixes (e.g., "Demarco_", "Chan_") when all files share the same suffix
|
||||
$result = Process-DuplicateGroup -Files $files -CommonPath $rootCommonPath -DuplicatesPath $rootDuplicatesPath -FilesInCommon $filesInRootCommon -StripPrefix -ValidPrefixes $validBlendfilePrefixes
|
||||
$pass2Moved += $result.MovedCount
|
||||
$pass2Duplicates += $result.DuplicateCount
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user