Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.78 KB | None | 0 0
  1. /**
  2.  *Project: Loki Render - A distributed job queue manager.
  3.  *Version 0.7.2
  4.  *Copyright (C) 2014 Daniel Petersen
  5.  *Created on Sep 12, 2009
  6.  */
  7. /**
  8.  *This program is free software: you can redistribute it and/or modify
  9.  *it under the terms of the GNU General Public License as published by
  10.  *the Free Software Foundation, either version 3 of the License, or
  11.  *(at your option) any later version.
  12.  *
  13.  *This program is distributed in the hope that it will be useful,
  14.  *but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *GNU General Public License for more details.
  17.  *
  18.  *You should have received a copy of the GNU General Public License
  19.  *along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  */
  21. package net.whn.loki.CL;
  22.  
  23. import net.whn.loki.common.*;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.logging.Logger;
  27. import javax.swing.JFileChooser;
  28. import javax.swing.JOptionPane;
  29. import net.whn.loki.IO.GruntIOHelper;
  30.  
  31. /**
  32.  *
  33.  * @author daniel
  34.  */
  35. public class CLHelper implements ICommon {
  36.  
  37.     public static String generateIsSupportedCL(JobType type, String binPath) {
  38.         String isSupportedCL = null;    //TODO
  39.         if (type == JobType.BLENDER) {
  40.             if (binPath == null) {
  41.                 return "blender -v";
  42.             } else {
  43.                 return binPath + " -v";
  44.             }
  45.         } else {
  46.             log.severe("received unknown job type!");
  47.         }
  48.         return null;
  49.     }
  50.  
  51.     /**
  52.      * generates command line based on type and values in task, auto file transfer
  53.      * @param task
  54.      * @return command line to pass to shell, or null if unknown type
  55.      */
  56.     public static String[] generateFileTaskCL(String bin, File lBaseDir, Task task)
  57.             throws IOException {
  58.         String[] taskCL = null;
  59.         JobType type = task.getType();
  60.         if (type == JobType.BLENDER) {
  61.             taskCL = blender_generateCL(bin, lBaseDir, task);
  62.         } else {
  63.             log.severe("received unknown job type!");
  64.         }
  65.         return taskCL;
  66.     }
  67.  
  68.     public static TaskStatus determineTaskReturn(JobType type, String stdout,
  69.             String errout) {
  70.         if (type == JobType.BLENDER) {
  71.             return blender_determineTaskReturn(stdout);
  72.         } else {
  73.             log.severe("received unknown job type!");
  74.             return null;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      *
  80.      * @param stdout
  81.      * @return full path
  82.      */
  83.     public static String blender_getRenderedFileName(String stdout) {
  84.         //example "Saved: /home/daniel/.loki/tmp/0001.png Time: 00:00.31"
  85.         String[] tokens = stdout.split("\\n");
  86.         for (int i = 0; i < tokens.length; i++) {
  87.             if (tokens[i].contains("Saved:")) {
  88.                 int last;
  89.                 if (tokens[i].contains("Time:")) {
  90.                     last = tokens[i].lastIndexOf("Time:") - 1;
  91.                 }
  92.                 else {
  93.                     last = tokens[i].length();
  94.                 }
  95.                 // Function breaks with quote in filename
  96.                 if (tokens[i].contains("'")){
  97.                     last = last - 1;
  98.                     return tokens[i].substring(8, last);
  99.                    
  100.                 }
  101.                
  102.                 return tokens[i].substring(7, last);
  103.             }
  104.         }
  105.         return null;
  106.     }
  107.  
  108.     public static String extractBlenderRenderTime(String stdout) {
  109.         String[] tokens = stdout.split(" |\\n");
  110.         for (int i = 0; i < tokens.length; i++) {
  111.             if (tokens[i].contains("Time:")) {
  112.                 return tokens[i + 1];
  113.             }
  114.         }
  115.  
  116.         return null;
  117.     }
  118.  
  119.     public static boolean determineBlenderBin(Config cfg)
  120.              {
  121.         boolean exeOK = true;
  122.  
  123.         String blenderBinStr = cfg.getBlenderBin();
  124.         if (blenderBinStr == null) {
  125.             blenderBinStr = "blender";
  126.         }
  127.  
  128.         if (!isBlenderExe(blenderBinStr)) { //no good, ask user to find it
  129.             JFileChooser fileChooser = new JFileChooser();
  130.             fileChooser.setDialogTitle("Please select the Blender executable");
  131.             while (true) {
  132.                 if (fileChooser.showDialog(null, "Select") ==
  133.                         JFileChooser.APPROVE_OPTION) {
  134.                     blenderBinStr = fileChooser.getSelectedFile().getPath();
  135.  
  136.                     if (isBlenderExe(blenderBinStr)) {
  137.                         break;
  138.                     } else {
  139.                         String msg = "Loki can't validate\n'" +
  140.                                 blenderBinStr + "'\n" +
  141.                                 "as a Blender executable. Use it anyway?";
  142.                         int result = JOptionPane.showConfirmDialog(null, msg,
  143.                                 "Valid executable?", JOptionPane.YES_NO_OPTION);
  144.  
  145.                         log.info("can't validate blender executable: " +
  146.                                 blenderBinStr);
  147.                         if(result == 0)
  148.                             break;
  149.                     }
  150.                 } else {
  151.                     log.info("loki didn't get a blender exe path; exiting.");
  152.                     return false;
  153.                 }
  154.             }
  155.         }
  156.         cfg.setBlenderBin(blenderBinStr);
  157.         return exeOK;
  158.     }
  159.  
  160.     public static boolean isBlenderExe(String blenderBinStr)
  161.              {
  162.  
  163.         String[] cl = {blenderBinStr, "-v"};
  164.         ProcessHelper pHelper = new ProcessHelper(cl);
  165.  
  166.         String[] result = pHelper.runProcess();
  167.         if (result[0].contains("Blender")) {
  168.             return true;
  169.         } else {
  170.             log.info("not a valid blender executable: " + blenderBinStr);
  171.             return false;
  172.         }
  173.     }
  174.  
  175.     /*BEGIN PRIVATE*/
  176.     //logging
  177.     private static final String className = "net.whn.loki.grunt.TaskCLHelper";
  178.     private static final Logger log = Logger.getLogger(className);
  179.  
  180.     private static String[] blender_generateCL(String blenderBin,
  181.             File lokiBaseDir, Task t) throws IOException {
  182.        
  183.         File blendFile;
  184.         File outputDirFile;
  185.        
  186.         if(t.isAutoFileTranfer()) {
  187.             blendFile = new File(lokiBaseDir, "fileCache" +
  188.                 File.separator + t.getProjectFileMD5() + ".blend");
  189.             outputDirFile = new File(lokiBaseDir, "tmp");
  190.         } else {    //manual    
  191.             blendFile = t.getOrigProjFile();
  192.             outputDirFile = new File(t.getOutputDir());
  193.         }
  194.  
  195.        
  196.         String[] blenderCL = null;
  197.  
  198.         if (blendFile.canRead() && outputDirFile.isDirectory()) {
  199.            
  200.             if (t.isTile()) {
  201.                 File script = GruntIOHelper.blender_setupTileScript(
  202.                         outputDirFile, t);
  203.                    
  204.                 blenderCL = new String[11];
  205.                 blenderCL[0] = blenderBin;
  206.                 blenderCL[1] = "-noaudio";
  207.                 blenderCL[2] = "-nojoystick";
  208.                 blenderCL[3] = "-b";
  209.                 blenderCL[4] = blendFile.getCanonicalPath();
  210.                 blenderCL[5] = "-P";
  211.                 blenderCL[6] = script.getCanonicalPath();
  212.                 blenderCL[7] = "-o";
  213.                 blenderCL[8] = outputDirFile.getCanonicalPath() + File.separator;
  214.                 blenderCL[9] = "-f";
  215.                 blenderCL[10] = Integer.toString(t.getFrame());
  216.                
  217.  
  218.  
  219.             } else {    //render the entire frame
  220.                 //example 'blender -b file.blend -o render_# -f 1
  221.  
  222.                 blenderCL = new String[9];
  223.                 blenderCL[0] = blenderBin;
  224.                 blenderCL[1] = "-noaudio";
  225.                 blenderCL[2] = "-nojoystick";
  226.                 blenderCL[3] = "-b";
  227.                 blenderCL[4] = blendFile.getCanonicalPath();
  228.                 blenderCL[5] = "-o";
  229.                 if(t.isAutoFileTranfer()) {
  230.                     blenderCL[6] = outputDirFile.getCanonicalPath()
  231.                             + File.separator;
  232.                 } else { //manual
  233.                     blenderCL[6] = outputDirFile.getCanonicalPath()
  234.                             + File.separator + t.getOutputFilePrefix();
  235.                 }
  236.                
  237.                 blenderCL[7] = "-f";
  238.                 blenderCL[8] = Integer.toString(t.getFrame());
  239.                
  240.  
  241.             }
  242.         } else {
  243.             log.severe("problems generating blender CL: " +
  244.                     blendFile.getAbsolutePath() + " " +
  245.                     outputDirFile.getAbsolutePath());
  246.         }
  247.  
  248.         return blenderCL;
  249.     }
  250.  
  251.     private static TaskStatus blender_determineTaskReturn(String stdout) {
  252.         TaskStatus status = null;
  253.         if (stdout.contains("Saved:")) {
  254.             status = TaskStatus.DONE;
  255.         } else {
  256.             status = TaskStatus.FAILED;
  257.         }
  258.         return status;
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement