Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #target photoshop
- // the directory with displacement maps
- var folderDisp = new Folder("D:/test");
- // the directory with normal maps
- var folderNorm = new Folder("D:/test");
- // A file for color matching (color source)
- var fileColorSource = new File("D:/test/disp_color_base/c.png");
- // the directory for saving the results
- var folderSave = new Folder("D:/test/output");
- if (!folderDisp.exists || !folderNorm.exists || !fileColorSource.exists || !folderSave.exists) {
- alert("Verify that all paths are correct.");
- throw "Path error!";
- }
- // Prepare the log file
- var logFile = new File(folderSave + "/log2.txt");
- logFile.open("w");
- logFile.writeln("=== LOG start: " + new Date().toString() + " ===");
- // Find all displacement maps in the directory
- var dispFiles = folderDisp.getFiles(function(f) {
- return f instanceof File && f.name.match(/_disp\.dds$/i);
- });
- if (dispFiles.length == 0) {
- alert("No _disp.dds files found.");
- throw "Cancelled";
- }
- // Processing
- for (var i = 0; i < dispFiles.length; i++) {
- var fileDisp = dispFiles[i];
- try {
- // Extract the base name, e.g. a from a_disp.png
- var baseName = fileDisp.name.replace(/_disp\.dds$/i, "");
- // Find the normal map named e.g. a_n.dds
- var fileNorm = new File(folderNorm + "/" + baseName + "_n.dds");
- if (!fileNorm.exists) {
- logFile.writeln("Normal map not found: " + fileNorm.name);
- continue;
- }
- // Open color source
- var docColor = app.open(fileColorSource);
- // Open displacement map
- var docDisp = app.open(fileDisp);
- app.activeDocument = docDisp;
- // Do "Match colors": Image > Adjustments > Match Color
- var idmatchColor = stringIDToTypeID( "matchColor" );
- var desc = new ActionDescriptor();
- var src = new ActionReference();
- src.putIdentifier( charIDToTypeID( "Dcmn" ), docColor.id );
- desc.putReference( charIDToTypeID( "Srce" ), src );
- // luminance: default 100
- desc.putInteger(charIDToTypeID("LwCl"), 100);
- // color intensity: default 100
- desc.putInteger(charIDToTypeID("Clrs"), 100);
- // fade: default 0
- desc.putInteger(charIDToTypeID("Fzns"), 0);
- desc.putBoolean(charIDToTypeID("Ntrl"), false);
- executeAction( idmatchColor, desc, DialogModes.NO );
- // Switch to the displacement map and select all
- app.activeDocument = docDisp;
- docDisp.selection.selectAll();
- docDisp.selection.copy();
- // Open normal map
- var docNorm = app.open(fileNorm);
- // Switch to the normal map
- app.activeDocument = docNorm;
- // Go to the channels and select alpha
- var alphaChannel;
- try {
- alphaChannel = docNorm.channels.getByName("Alpha 1");
- } catch(e) {
- alphaChannel = docNorm.channels.add();
- }
- docNorm.activeChannels = [alphaChannel];
- docNorm.paste();
- // Save using the SaveAsDDS action (recorded before)
- app.doAction("SaveAsDDS", "DDS Actions");
- var tempSaveFile = new File(folderSave + "/TEMP.dds");
- // rename TEMP.dds → a_nh.dds
- var finalSaveFile = new File(folderSave + "/" + baseName + "_nh.dds");
- $.sleep(300);
- app.refresh();
- if (tempSaveFile.exists) {
- tempSaveFile.copy(finalSaveFile);
- tempSaveFile.remove(); // delete the temporary file
- logFile.writeln("Saved: " + finalSaveFile.name);
- } else {
- logFile.writeln("Error: TEMP.dds did not exist.");
- }
- //Close dolor source without saving (originals unchanged)
- docColor.close(SaveOptions.DONOTSAVECHANGES);
- // Close both maps without saving (originals unchanged)
- docDisp.close(SaveOptions.DONOTSAVECHANGES);
- docNorm.close(SaveOptions.DONOTSAVECHANGES);
- } catch(e) {
- logFile.writeln("Error in file: " + fileDisp.name + " - " + e.toString());
- }
- }
- logFile.writeln("=== LOG end: " + new Date().toString() + " ===");
- logFile.close();
- alert("Done! See the log: log2.txt");
Advertisement
Add Comment
Please, Sign In to add comment