step 3 actually working, can't believe it first try
This commit is contained in:
@@ -132,7 +132,8 @@ function Process-DuplicateGroup {
|
||||
[string]$CommonPath,
|
||||
[hashtable]$FilesInCommon,
|
||||
[switch]$StripPrefix,
|
||||
[string[]]$ValidPrefixes = @()
|
||||
[string[]]$ValidPrefixes = @(),
|
||||
[System.Collections.ArrayList]$MoveLog = $null
|
||||
)
|
||||
|
||||
$movedCount = 0
|
||||
@@ -221,6 +222,17 @@ function Process-DuplicateGroup {
|
||||
Move-Item -Path $firstFile.FullName -Destination $destinationPath -Force
|
||||
$FilesInCommon[$fileName] = $true
|
||||
$movedCount++
|
||||
|
||||
# Log the move
|
||||
if ($null -ne $MoveLog) {
|
||||
$normalizedOriginal = [System.IO.Path]::GetFullPath($firstFile.FullName)
|
||||
$normalizedNew = [System.IO.Path]::GetFullPath($destinationPath)
|
||||
$null = $MoveLog.Add([PSCustomObject]@{
|
||||
OriginalPath = $normalizedOriginal
|
||||
NewPath = $normalizedNew
|
||||
Type = "moved"
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
Write-Warning "Failed to move first duplicate file: $($firstFile.FullName) - $($_.Exception.Message)"
|
||||
}
|
||||
@@ -229,8 +241,20 @@ function Process-DuplicateGroup {
|
||||
for ($i = 1; $i -lt $Files.Count; $i++) {
|
||||
$fileObj = $Files[$i].File
|
||||
try {
|
||||
Remove-Item -Path $fileObj.FullName -Force
|
||||
$originalPath = $fileObj.FullName
|
||||
Remove-Item -Path $originalPath -Force
|
||||
$duplicateCount++
|
||||
|
||||
# Log the deletion (replaced by the moved file)
|
||||
if ($null -ne $MoveLog) {
|
||||
$normalizedOriginal = [System.IO.Path]::GetFullPath($originalPath)
|
||||
$normalizedReplacement = [System.IO.Path]::GetFullPath($destinationPath)
|
||||
$null = $MoveLog.Add([PSCustomObject]@{
|
||||
OriginalPath = $normalizedOriginal
|
||||
ReplacedBy = $normalizedReplacement
|
||||
Type = "deleted"
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
Write-Warning "Failed to delete duplicate file: $($fileObj.FullName) - $($_.Exception.Message)"
|
||||
}
|
||||
@@ -258,6 +282,7 @@ if ($null -eq $blendfileFolders -or $blendfileFolders.Count -eq 0) {
|
||||
|
||||
$totalPass1Moved = 0
|
||||
$totalPass1Duplicates = 0
|
||||
$pass1MoveLog = [System.Collections.ArrayList]::new()
|
||||
|
||||
foreach ($blendfileFolder in $blendfileFolders) {
|
||||
Write-Host ""
|
||||
@@ -297,7 +322,7 @@ if ($null -eq $blendfileFolders -or $blendfileFolders.Count -eq 0) {
|
||||
$blendfileDuplicates = 0
|
||||
|
||||
foreach ($group in $groupedByChecksum) {
|
||||
$result = Process-DuplicateGroup -Files $group.Group -CommonPath $blendfileCommonPath -FilesInCommon $filesInBlendfileCommon
|
||||
$result = Process-DuplicateGroup -Files $group.Group -CommonPath $blendfileCommonPath -FilesInCommon $filesInBlendfileCommon -MoveLog $pass1MoveLog
|
||||
$blendfileMoved += $result.MovedCount
|
||||
$blendfileDuplicates += $result.DuplicateCount
|
||||
}
|
||||
@@ -377,6 +402,7 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
|
||||
Write-Host "Moving files to \common and deleting duplicates..." -ForegroundColor Yellow
|
||||
$pass2Moved = 0
|
||||
$pass2Duplicates = 0
|
||||
$pass2MoveLog = [System.Collections.ArrayList]::new()
|
||||
|
||||
foreach ($group in $groupedByChecksum) {
|
||||
$files = $group.Group
|
||||
@@ -388,7 +414,7 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
|
||||
|
||||
# Multiple files with same checksum (duplicates across blendfiles)
|
||||
# Use StripPrefix to remove blendfile prefixes (e.g., "Demarco_", "Chan_") when all files share the same suffix
|
||||
$result = Process-DuplicateGroup -Files $files -CommonPath $rootCommonPath -FilesInCommon $filesInRootCommon -StripPrefix -ValidPrefixes $validBlendfilePrefixes
|
||||
$result = Process-DuplicateGroup -Files $files -CommonPath $rootCommonPath -FilesInCommon $filesInRootCommon -StripPrefix -ValidPrefixes $validBlendfilePrefixes -MoveLog $pass2MoveLog
|
||||
$pass2Moved += $result.MovedCount
|
||||
$pass2Duplicates += $result.DuplicateCount
|
||||
}
|
||||
@@ -419,6 +445,7 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
|
||||
$filesInFlatColors = @{}
|
||||
$flatColorsMoved = 0
|
||||
$flatColorsDuplicates = 0
|
||||
$flatColorsMoveLog = [System.Collections.ArrayList]::new()
|
||||
|
||||
foreach ($group in $flatColorsGroupedByChecksum) {
|
||||
$files = $group.Group
|
||||
@@ -450,16 +477,26 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
|
||||
}
|
||||
|
||||
try {
|
||||
Move-Item -Path $fileObj.FullName -Destination $destinationPath -Force
|
||||
$originalPath = $fileObj.FullName
|
||||
Move-Item -Path $originalPath -Destination $destinationPath -Force
|
||||
$filesInFlatColors[$fileName] = $true
|
||||
$flatColorsMoved++
|
||||
|
||||
# Log the move
|
||||
$normalizedOriginal = [System.IO.Path]::GetFullPath($originalPath)
|
||||
$normalizedNew = [System.IO.Path]::GetFullPath($destinationPath)
|
||||
$null = $flatColorsMoveLog.Add([PSCustomObject]@{
|
||||
OriginalPath = $normalizedOriginal
|
||||
NewPath = $normalizedNew
|
||||
Type = "moved"
|
||||
})
|
||||
} catch {
|
||||
Write-Warning "Failed to move FlatColors file: $($fileObj.FullName) - $($_.Exception.Message)"
|
||||
}
|
||||
} else {
|
||||
# Multiple files with same checksum (duplicates) - merge them
|
||||
# Use Process-DuplicateGroup to handle merging
|
||||
$result = Process-DuplicateGroup -Files $files -CommonPath $flatColorsPath -FilesInCommon $filesInFlatColors -StripPrefix -ValidPrefixes $validBlendfilePrefixes
|
||||
$result = Process-DuplicateGroup -Files $files -CommonPath $flatColorsPath -FilesInCommon $filesInFlatColors -StripPrefix -ValidPrefixes $validBlendfilePrefixes -MoveLog $flatColorsMoveLog
|
||||
$flatColorsMoved += $result.MovedCount
|
||||
$flatColorsDuplicates += $result.DuplicateCount
|
||||
}
|
||||
@@ -474,6 +511,179 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
|
||||
Write-Host "Pass 2 complete: $pass2Moved file(s) moved to \common, $pass2Duplicates duplicate(s) deleted" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Save Move Log and Find Blend Files
|
||||
# ============================================================================
|
||||
Write-Host ""
|
||||
Write-Host "Saving move log and finding blend files..." -ForegroundColor Yellow
|
||||
|
||||
# Combine all move logs
|
||||
$allMoves = @()
|
||||
if ($null -ne $pass1MoveLog -and $pass1MoveLog.Count -gt 0) {
|
||||
$allMoves += $pass1MoveLog
|
||||
}
|
||||
if ($null -ne $pass2MoveLog -and $pass2MoveLog.Count -gt 0) {
|
||||
$allMoves += $pass2MoveLog
|
||||
}
|
||||
if ($null -ne $flatColorsMoveLog -and $flatColorsMoveLog.Count -gt 0) {
|
||||
$allMoves += $flatColorsMoveLog
|
||||
}
|
||||
|
||||
# Get parent directory of texture folder
|
||||
$blendFileParentDir = Split-Path -Path $textureFolderPath -Parent
|
||||
|
||||
# Find matching blend files
|
||||
$blendFileMappings = @()
|
||||
if ($null -ne $blendfileFolders -and $blendfileFolders.Count -gt 0) {
|
||||
# Get blendfile folder names
|
||||
$blendfileFolderNames = $blendfileFolders | ForEach-Object { $_.Name }
|
||||
|
||||
# Find all .blend files in parent directory
|
||||
$blendFiles = Get-ChildItem -Path $blendFileParentDir -Filter "*.blend" -File -ErrorAction SilentlyContinue
|
||||
|
||||
if ($null -ne $blendFiles -and $blendFiles.Count -gt 0) {
|
||||
foreach ($blendFile in $blendFiles) {
|
||||
# Extract prefix from blend file name (e.g., "Chan_v4.3.blend" -> "Chan")
|
||||
$blendFileName = $blendFile.BaseName
|
||||
$parts = $blendFileName.Split('_', 2)
|
||||
|
||||
if ($parts.Length -ge 1) {
|
||||
$prefix = $parts[0]
|
||||
|
||||
# Check if prefix matches any blendfile folder name
|
||||
if ($blendfileFolderNames -contains $prefix) {
|
||||
$blendFileMappings += [PSCustomObject]@{
|
||||
BlendFile = [System.IO.Path]::GetFullPath($blendFile.FullName)
|
||||
BlendfileFolder = $prefix
|
||||
}
|
||||
Write-Host "Found matching blend file: $($blendFile.Name) -> $prefix" -ForegroundColor Gray
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Create move log object
|
||||
$moveLogData = [PSCustomObject]@{
|
||||
TextureFolderPath = $textureFolderPath
|
||||
Timestamp = (Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ")
|
||||
TotalMoves = ($allMoves | Where-Object { $_.Type -eq "moved" }).Count
|
||||
TotalDeletes = ($allMoves | Where-Object { $_.Type -eq "deleted" }).Count
|
||||
Moves = $allMoves
|
||||
BlendFileMappings = $blendFileMappings
|
||||
}
|
||||
|
||||
# Save to JSON file
|
||||
$moveLogPath = Join-Path -Path $blendFileParentDir -ChildPath "texture_moves.json"
|
||||
try {
|
||||
$moveLogData | ConvertTo-Json -Depth 10 | Set-Content -Path $moveLogPath -Encoding UTF8
|
||||
Write-Host "Move log saved to: $moveLogPath" -ForegroundColor Green
|
||||
Write-Host " Total moves: $($moveLogData.TotalMoves), Total deletes: $($moveLogData.TotalDeletes)" -ForegroundColor Gray
|
||||
Write-Host " Blend files found: $($blendFileMappings.Count)" -ForegroundColor Gray
|
||||
} catch {
|
||||
Write-Warning "Failed to save move log: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Remap Texture Paths in Blend Files
|
||||
# ============================================================================
|
||||
if ($blendFileMappings.Count -gt 0 -and (Test-Path -Path $moveLogPath)) {
|
||||
Write-Host ""
|
||||
Write-Host "Remapping texture paths in blend files..." -ForegroundColor Yellow
|
||||
|
||||
# Find Blender executable from PATH
|
||||
$blenderExe = $null
|
||||
$blenderInPath = Get-Command blender -ErrorAction SilentlyContinue
|
||||
if ($null -ne $blenderInPath) {
|
||||
$blenderExe = $blenderInPath.Source
|
||||
}
|
||||
|
||||
if ($null -eq $blenderExe) {
|
||||
Write-Warning "Blender executable not found. Skipping texture path remapping."
|
||||
Write-Host " Please install Blender or add it to your PATH." -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "Found Blender: $blenderExe" -ForegroundColor Gray
|
||||
|
||||
# Get the remap script path (should be in the same directory as this script)
|
||||
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
|
||||
$remapScriptPath = Join-Path -Path $scriptDir -ChildPath "remap_texture_paths.py"
|
||||
|
||||
if (-not (Test-Path -Path $remapScriptPath)) {
|
||||
Write-Warning "Remap script not found: $remapScriptPath"
|
||||
Write-Warning "Skipping texture path remapping."
|
||||
} else {
|
||||
$processedCount = 0
|
||||
$failedCount = 0
|
||||
|
||||
Write-Host "Processing $($blendFileMappings.Count) blend file(s)..." -ForegroundColor Gray
|
||||
|
||||
foreach ($mapping in $blendFileMappings) {
|
||||
$blendFilePath = $mapping.BlendFile
|
||||
$blendFileName = Split-Path -Path $blendFilePath -Leaf
|
||||
|
||||
Write-Host " Processing: $blendFileName" -ForegroundColor Cyan
|
||||
|
||||
# Check if blend file exists
|
||||
if (-not (Test-Path -Path $blendFilePath)) {
|
||||
Write-Warning " Blend file not found: $blendFilePath"
|
||||
$failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
# Run Blender with the remapping script (one file at a time)
|
||||
# Delete old output file
|
||||
Remove-Item -Path "blender_output.txt" -ErrorAction SilentlyContinue
|
||||
|
||||
try {
|
||||
# Run Blender and capture output while displaying it (like compress_blend_files.bat)
|
||||
# Build command with proper argument array
|
||||
$blenderArgsArray = @(
|
||||
"--background",
|
||||
"--factory-startup",
|
||||
"--python", $remapScriptPath,
|
||||
"--",
|
||||
$blendFilePath,
|
||||
$moveLogPath
|
||||
)
|
||||
|
||||
# Execute and pipe output through Tee-Object to display and save
|
||||
& $blenderExe $blenderArgsArray 2>&1 | Tee-Object -FilePath "blender_output.txt" | ForEach-Object {
|
||||
Write-Host " $_"
|
||||
}
|
||||
|
||||
# Check exit code
|
||||
$exitCode = $LASTEXITCODE
|
||||
|
||||
if ($exitCode -eq 0) {
|
||||
Write-Host " Successfully remapped texture paths" -ForegroundColor Green
|
||||
$processedCount++
|
||||
} else {
|
||||
Write-Warning " Failed to remap texture paths (exit code: $exitCode)"
|
||||
$failedCount++
|
||||
}
|
||||
} catch {
|
||||
Write-Warning " Exception while processing blend file: $($_.Exception.Message)"
|
||||
$failedCount++
|
||||
}
|
||||
}
|
||||
|
||||
# Clean up temporary output files
|
||||
Remove-Item -Path "blender_output.txt" -ErrorAction SilentlyContinue
|
||||
Remove-Item -Path "blender_error.txt" -ErrorAction SilentlyContinue
|
||||
|
||||
Write-Host ""
|
||||
if ($processedCount -gt 0 -or $failedCount -gt 0) {
|
||||
Write-Host "Blend file remapping complete: $processedCount succeeded, $failedCount failed" -ForegroundColor $(if ($failedCount -eq 0) { "Green" } else { "Yellow" })
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($blendFileMappings.Count -eq 0) {
|
||||
Write-Host ""
|
||||
Write-Host "No matching blend files found. Skipping texture path remapping." -ForegroundColor Gray
|
||||
}
|
||||
}
|
||||
|
||||
# Function to remove empty folders recursively
|
||||
function Remove-EmptyFolders {
|
||||
param(
|
||||
|
||||
Reference in New Issue
Block a user