Guest User

Untitled

a guest
Nov 7th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 6.59 KB | None | 0 0
  1. module main;
  2.  
  3. import std.stdio;
  4. import std.file;
  5. import std.path;
  6. import std.range : array,take,chunks;
  7. import std.algorithm : find, canFind, map, joiner;
  8. import std.format : format;
  9. import std.conv : to;
  10. import std.process : executeShell;
  11. import std.string : replace;
  12. import std.algorithm : filter;  
  13.  
  14. enum export_version = "2016";
  15. enum import_version = "2016";
  16.  
  17. enum installer_path = "E:\\Max Resources\\001 Max Plugins\\000 Octane\\Max\\";
  18. enum successlist_file = ".\\success_list.txt";
  19. enum faillist_file = ".\\fail_list.txt";
  20.  
  21. void main(string[] args)
  22. {
  23.     string export_exe;
  24.     string import_exe;
  25.  
  26.     version (linux) string autodesk_path = "/media/Windows7/Program Files/Autodesk/";
  27.     else version (Windows) string autodesk_path = "C:\\Program Files\\Autodesk\\";
  28.     else assert(0,"unsupported platform");
  29.  
  30.     auto max_executables = dirEntries(autodesk_path,"*3dsmax.exe",SpanMode.depth);
  31.     foreach (exe; max_executables)
  32.     {
  33.         writeln(exe);
  34.         if (exe.name.canFind(import_version)) import_exe = exe.name;
  35.         if (exe.name.canFind(export_version)) export_exe = exe.name;
  36.     }
  37.     if (import_exe == "" || export_exe == "")
  38.     {
  39.         max_executables = dirEntries("D:\\Program Files\\Autodesk\\","*3dsmax.exe",SpanMode.depth);
  40.         foreach (exe; max_executables)
  41.         {
  42.             writeln(exe);
  43.             if (exe.name.canFind(import_version)) import_exe = exe.name;
  44.             if (exe.name.canFind(export_version)) export_exe = exe.name;
  45.         }
  46.     }
  47.     assert(import_exe != "" && export_exe != "","Couldn't find the max versions we are using on this system\n");
  48.  
  49.     version(linux)
  50.     {  
  51.         auto import_script = "/media/danni/network/resources/Temp/danni/OctaneImporter/importer_c.ms";
  52.         auto export_script = "/media/danni/network/resources/Temp/danni/OctaneImporter/exporter.ms";   
  53.         auto filedir = "/media/danni/network/resources/Temp/danni/maxfiles/";
  54.     }
  55.     else version(Windows)
  56.     {
  57. //      auto import_script = "E:\\Temp\\Danni\\OctaneImporter\\importer.ms";
  58.         auto import_script = "E:\\Temp\\Danni\\OctaneImporter\\importer_c.ms";
  59.         auto export_script = "E:\\Temp\\Danni\\OctaneImporter\\exporter.ms";
  60.         auto filedir = ".";
  61.     }
  62.     else assert(0);
  63.  
  64.     if (export_version == import_version)
  65.     {
  66.         auto installer_script = installer_path ~ "OctaneRender for 3ds Max 2.23.2 - 1.9.exe";
  67.         executeShell(installer_script);
  68.     }
  69.  
  70.     writeln("\n starting on export step\n");
  71.     auto max_files = dirEntries(filedir,"*.max",SpanMode.depth).
  72.         map!(a => a.name).
  73.         filter!(a =>
  74.                 !a.canFind("Octane2.") &&
  75.                 !a.canFind("\\ssd\\") &&
  76.                 !a.canFind("\\ssed\\") &&
  77.                 !a.canFind("\\quaratined\\") &&
  78.                 !a.canFind("\\octane1\\") &&
  79.                 !a.replace(".max",".txt").exists()).map!(a => a.replace("\\","\\\\")).array;
  80. //  writeln(filedir.dirEntries("*.max",SpanMode.depth).map!(a => a.name).filter!(a => !a.canFind("Octane2.") && !a.canFind("\\ssd\\")) );
  81.     enum chunk_size = 8;
  82.  
  83.     writefln("exporting %d files",max_files.length);
  84.     if (max_files.length > 0)
  85.     {
  86.         int o = 1;
  87.         foreach (chunk; max_files.chunks(chunk_size))
  88.         {
  89.             writeln("exporting chunk ", o.to!string, " of ", (max_files.length/chunk_size).to!string);
  90.             o++;
  91.             auto tmp_script = "tmp_exporter.ms";
  92.  
  93.             std.file.write(
  94.                 tmp_script,
  95.                 format(
  96.                     "include \"%s\"\r\nai3d_octane_convert_export_files = #(%s)\r\nfor export_file in ai3d_octane_convert_export_files do export_material_file export_file\r\nquitMax() #noprompt\r\n",
  97.                     export_script,  chunk.map!(a => "\"" ~ a ~ "\"").joiner(",").to!string()
  98.                 )
  99.             );
  100.  
  101.             auto export_cmd = format("\"%s\" -mip -silent -U MAXScript \"%s\" ",export_exe, tmp_script);
  102.             writeln(export_cmd);
  103.             executeShell(export_cmd);
  104.         }
  105.     }
  106.  
  107.     if (export_version == import_version)
  108.     {
  109.         auto installer_script = installer_path ~ "NEW ERA Testing Only\\OctaneRender_for_3ds_Max_2.25_-_2.23.exe";
  110.         executeShell(installer_script);
  111.     }
  112.  
  113.     if (!successlist_file.exists) std.file.write(successlist_file,"");
  114.     if (!faillist_file.exists) std.file.write(faillist_file,"");
  115.     writeln("\n done exporting work; lets import...\n");
  116.     max_files = dirEntries(filedir,"*.max",SpanMode.depth).
  117.         map!(a => a.name).
  118.         filter!(a =>
  119.             !a.canFind("Octane2.") &&                                       // DON'T use old octane converter attempts
  120.             !a.canFind("\\ssd\\") &&                                        // or files in superseeded folders
  121.             !a.canFind("\\ssed\\") &&
  122.             !a.canFind("\\octane1\\") &&                                    // or files that have already been converted
  123.             !a.canFind("\\quarantined\\") &&                                // or files that failed to convert
  124.  
  125.             a.replace(".max",".txt").exists &&                              // MAKE SURE that a material text file has been generated
  126.             std.file.read(a.replace(".max",".txt"),8) != "END_FILE" &&       // that isn't essentially an empty file
  127.             !(a.dirName~"\\\\octane1\\\\"~a.baseName).exists &&             // and that the file hasn't already been converted
  128.             !a.replace(".max","_Octane2.max"
  129.         ).exists).map!(a => a.replace("\\","\\\\")).array;
  130. //  writeln (max_files);
  131.     // check for files that have an exported material txt file but not an imported and saved max file
  132.     int i=1;
  133.     foreach (max_file;  max_files)
  134.     {
  135.         writeln("\nimporting ", max_file, " ( ", i.to!string, " of ", max_files.length.to!string , " )\n");
  136.         i++;
  137. //      writeln("old should be %s", (max_file.dirName~"\\\\ssd\\\\"~max_file.baseName));
  138.         auto tmp_script = "tmp_importer.ms";
  139.  
  140.         std.file.write
  141.         (
  142.             tmp_script,
  143.             format("include \"%s\"\r\nimport_material_file \"%s\"\r\nquitMax() #noprompt\r\n", import_script, max_file)
  144.         );
  145.  
  146.         string import_cmd = format("\"%s\" -mip -silent -U MAXScript \"%s\"", import_exe, tmp_script);
  147.         //writeln(import_cmd);
  148.         executeShell(import_cmd);
  149.  
  150.         auto backup_dir = max_file.dirName() ~ "\\octane1\\";
  151.         mkdirRecurse(backup_dir);
  152.  
  153.         string result_file = max_file.replace(".max","_Octane2.max").replace("\\\\","\\");
  154.         if (result_file.exists && result_file.isFile)
  155.         {
  156.             writefln(result_file);
  157.             max_file.rename (backup_dir~max_file.baseName);
  158.             max_file.replace(".max","_Octane2.max").rename(max_file);
  159.  
  160.             auto ff = max_file.replace(".\\\\","").replace("\\\\","\\").asAbsolutePath();
  161.             std.file.append(successlist_file,format("%s\n",ff));  
  162.         }
  163.         else
  164.         {
  165.             auto badfiledir = max_file.dirName() ~ "\\quarantined\\";
  166.             mkdirRecurse(badfiledir);
  167.             auto badfile =badfiledir~max_file.baseName();
  168.             max_file.rename(badfile);
  169.             max_file.replace(".max",".txt").rename(badfile.replace(".max",".txt"));
  170. //          max_file.replace(".max",".log").rename(badfile.replace(".max",".log"));
  171.  
  172.             auto ff = badfile.replace(".\\\\","").replace("\\\\","\\").asAbsolutePath();
  173.             std.file.append(faillist_file,format("%s\n",ff));
  174.         }
  175.     }
  176.  
  177.     writeln("\ndone\n");
  178.     // Lets the user press <Return> before program returns
  179.     stdin.readln();
  180. }
Add Comment
Please, Sign In to add comment