Amakesh

photoshop .jsx

Jul 12th, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #target photoshop
  2.  
  3. // the directory with displacement maps
  4. var folderDisp = new Folder("D:/test");
  5.  
  6. // the directory with normal maps
  7. var folderNorm = new Folder("D:/test");
  8.  
  9. // A file for color matching (color source)
  10. var fileColorSource = new File("D:/test/disp_color_base/c.png");
  11.  
  12. // the directory for saving the results
  13. var folderSave = new Folder("D:/test/output");
  14.  
  15. if (!folderDisp.exists || !folderNorm.exists || !fileColorSource.exists || !folderSave.exists) {
  16.     alert("Verify that all paths are correct.");
  17.     throw "Path error!";
  18. }
  19.  
  20. // Prepare the log file
  21. var logFile = new File(folderSave + "/log2.txt");
  22. logFile.open("w");
  23. logFile.writeln("=== LOG start: " + new Date().toString() + " ===");
  24.  
  25.  
  26. // Find all displacement maps in the directory
  27. var dispFiles = folderDisp.getFiles(function(f) {
  28.     return f instanceof File && f.name.match(/_disp\.dds$/i);
  29. });
  30.  
  31. if (dispFiles.length == 0) {
  32.     alert("No _disp.dds files found.");
  33.     throw "Cancelled";
  34. }
  35.  
  36. // Processing
  37. for (var i = 0; i < dispFiles.length; i++) {
  38.     var fileDisp = dispFiles[i];
  39.  
  40.     try {
  41.         // Extract the base name, e.g. a from a_disp.png
  42.         var baseName = fileDisp.name.replace(/_disp\.dds$/i, "");
  43.  
  44.         // Find the normal map named e.g. a_n.dds
  45.         var fileNorm = new File(folderNorm + "/" + baseName + "_n.dds");
  46.         if (!fileNorm.exists) {
  47.             logFile.writeln("Normal map not found: " + fileNorm.name);
  48.             continue;
  49.         }
  50.  
  51.         // Open color source
  52.         var docColor = app.open(fileColorSource);
  53.         // Open displacement map
  54.         var docDisp = app.open(fileDisp);
  55.      
  56.         app.activeDocument = docDisp;
  57.  
  58.         // Do "Match colors": Image > Adjustments > Match Color
  59.         var idmatchColor = stringIDToTypeID( "matchColor" );
  60.         var desc = new ActionDescriptor();
  61.    
  62.         var src = new ActionReference();
  63.         src.putIdentifier( charIDToTypeID( "Dcmn" ), docColor.id );
  64.         desc.putReference( charIDToTypeID( "Srce" ), src );
  65.  
  66.         // luminance: default 100
  67.         desc.putInteger(charIDToTypeID("LwCl"), 100);
  68.         // color intensity: default 100
  69.         desc.putInteger(charIDToTypeID("Clrs"), 100);
  70.         // fade: default 0
  71.         desc.putInteger(charIDToTypeID("Fzns"), 0);
  72.         desc.putBoolean(charIDToTypeID("Ntrl"), false);
  73.  
  74.         executeAction( idmatchColor, desc, DialogModes.NO );
  75.  
  76.         // Switch to the displacement map and select all
  77.         app.activeDocument = docDisp;
  78.         docDisp.selection.selectAll();
  79.         docDisp.selection.copy();
  80.  
  81.         // Open normal map
  82.         var docNorm = app.open(fileNorm);
  83.  
  84.         // Switch to the normal map
  85.         app.activeDocument = docNorm;
  86.  
  87.         // Go to the channels and select alpha
  88.         var alphaChannel;
  89.         try {
  90.             alphaChannel = docNorm.channels.getByName("Alpha 1");
  91.         } catch(e) {
  92.             alphaChannel = docNorm.channels.add();
  93.         }
  94.  
  95.         docNorm.activeChannels = [alphaChannel];
  96.         docNorm.paste();
  97.  
  98.         // Save using the SaveAsDDS action (recorded before)
  99.         app.doAction("SaveAsDDS", "DDS Actions");
  100.  
  101.         var tempSaveFile = new File(folderSave + "/TEMP.dds");
  102.         // rename TEMP.dds → a_nh.dds
  103.         var finalSaveFile = new File(folderSave + "/" + baseName + "_nh.dds");
  104.  
  105.         $.sleep(300);
  106.         app.refresh();
  107.  
  108.         if (tempSaveFile.exists) {
  109.             tempSaveFile.copy(finalSaveFile);
  110.             tempSaveFile.remove(); // delete the temporary file
  111.             logFile.writeln("Saved: " + finalSaveFile.name);
  112.         } else {
  113.         logFile.writeln("Error: TEMP.dds did not exist.");
  114.         }
  115.        
  116.         //Close dolor source without saving (originals unchanged)
  117.         docColor.close(SaveOptions.DONOTSAVECHANGES);
  118.         // Close both maps without saving (originals unchanged)
  119.         docDisp.close(SaveOptions.DONOTSAVECHANGES);
  120.         docNorm.close(SaveOptions.DONOTSAVECHANGES);
  121.  
  122.     } catch(e) {
  123.         logFile.writeln("Error in file: " + fileDisp.name + " - " + e.toString());
  124.     }
  125. }
  126.  
  127. logFile.writeln("=== LOG end: " + new Date().toString() + " ===");
  128. logFile.close();
  129.  
  130. alert("Done! See the log: log2.txt");
  131.  
Tags: photoshop
Advertisement
Add Comment
Please, Sign In to add comment