2

我需要在服务器中进行 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??
    }
}
4

1 回答 1

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 -b scene.blend -o [...]”可能需要额外注意。
于 2012-12-03T14:23:54.467 回答