Blender 3D渲染Java servlet接口

时间:2012-12-03 13:13:51

标签: java servlets render blender

我需要在服务器中制作3D渲染静止图像。我被告知Java servlet会这样做。我正在研究它以获得快速原型并且卡住了。

我应该如何在Java中调用Blender?

import java.io.*;
import javax.servlet.http.*;

@WebServlet("/HelloworldInServlet")
public class HelloworldInServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Run Blender on server (localhost for now)
        // I need something here??
    }
}

1 个答案:

答案 0 :(得分:3)

我能想到的最简单的解决方案如下:

public class RenderSceneServlet extends HttpServlet {
    protected void doGet(HttServletRequest request, HttpServletResponse response) {
        String outputFile = "/path/to/output-file";

        // execute command to render a frame from scene.blend
        Runtime runtime = Runtime.getRuntime();
        Process exec = runtime.exec("blender -b scene.blend -o " + outputFile + " -F JPEG -x 1 -f 1");

        // wait for blender to finish
        exec.waitFor();

        // serve the rendered file
        File file = new File(outputFile);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        response.setContentLength(bytes.length);
        response.setContentType("image/jpeg");
        response.getOutputStream().write(bytes);
    }
}

你应该记住一些事情:

  • 使用outputFile的交替名称,否则并发请求将每次覆盖该文件。
  • FileUtils是来自apache commons-io
  • 的课程
  • 我对blender并不熟悉,所以命令行“blender -b scene.blend -o [...]”可能需要额外注意。