v1
This commit is contained in:
103
src/main/BlenderProcess.js
Normal file
103
src/main/BlenderProcess.js
Normal file
@@ -0,0 +1,103 @@
|
||||
const { spawn } = require("child_process");
|
||||
const PathResolver = require("./PathResolver.js");
|
||||
|
||||
const BlenderProcess = {
|
||||
render_frame: (obj_params) => {
|
||||
let str_blend_path = obj_params.str_blend_path;
|
||||
let str_camera_name = obj_params.str_camera_name;
|
||||
let nb_frame = obj_params.nb_frame;
|
||||
let nb_resolution_x = obj_params.nb_resolution_x;
|
||||
let nb_resolution_y = obj_params.nb_resolution_y;
|
||||
let str_format = obj_params.str_format;
|
||||
let str_output_path = obj_params.str_output_path;
|
||||
|
||||
let str_safe_name = str_camera_name.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
||||
|
||||
let str_python_expr = [
|
||||
"import bpy",
|
||||
"scene=bpy.context.scene",
|
||||
"scene.camera=bpy.data.objects['" + str_safe_name + "']",
|
||||
"scene.render.resolution_x=" + nb_resolution_x,
|
||||
"scene.render.resolution_y=" + nb_resolution_y,
|
||||
].join(";");
|
||||
|
||||
let list_args = [
|
||||
"-b", str_blend_path,
|
||||
"--python-expr", str_python_expr,
|
||||
"-o", str_output_path,
|
||||
"-F", str_format,
|
||||
"-f", String(nb_frame),
|
||||
];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let str_stdout = "";
|
||||
let str_stderr = "";
|
||||
|
||||
let obj_process = spawn(PathResolver.get_blender_path(), list_args);
|
||||
|
||||
obj_process.stdout.on("data", (obj_data) => {
|
||||
str_stdout += obj_data.toString();
|
||||
if (obj_params.fn_on_stdout) {
|
||||
obj_params.fn_on_stdout(obj_data.toString());
|
||||
}
|
||||
});
|
||||
|
||||
obj_process.stderr.on("data", (obj_data) => {
|
||||
str_stderr += obj_data.toString();
|
||||
});
|
||||
|
||||
obj_process.on("close", (nb_code) => {
|
||||
if (nb_code !== 0) {
|
||||
reject({
|
||||
str_message: "Blender a quitte avec le code " + nb_code,
|
||||
str_stderr: str_stderr,
|
||||
str_stdout: str_stdout,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let str_rendered_file = BlenderProcess._find_rendered_file(str_stdout);
|
||||
resolve({
|
||||
str_rendered_file: str_rendered_file,
|
||||
str_stdout: str_stdout,
|
||||
});
|
||||
});
|
||||
|
||||
obj_process.on("error", (obj_err) => {
|
||||
reject({
|
||||
str_message: "Impossible de lancer Blender : " + obj_err.message,
|
||||
str_stderr: "",
|
||||
str_stdout: "",
|
||||
});
|
||||
});
|
||||
|
||||
// Store process reference for kill support
|
||||
if (obj_params.fn_on_process) {
|
||||
obj_params.fn_on_process(obj_process);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
_find_rendered_file: (str_stdout) => {
|
||||
let list_lines = str_stdout.split("\n");
|
||||
for (let str_line of list_lines) {
|
||||
let nb_index = str_line.indexOf("Saved: ");
|
||||
if (nb_index !== -1) {
|
||||
let str_path = str_line.substring(nb_index + 7).trim();
|
||||
// Remove trailing info like " Time: 00:01.23"
|
||||
let nb_time_index = str_path.indexOf(" Time:");
|
||||
if (nb_time_index !== -1) {
|
||||
str_path = str_path.substring(0, nb_time_index).trim();
|
||||
}
|
||||
// Blender 5.x wraps path in single quotes: Saved: '/path/file.png'
|
||||
if (str_path.startsWith("'") && str_path.endsWith("'")) {
|
||||
str_path = str_path.substring(1, str_path.length - 1);
|
||||
}
|
||||
return str_path;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = BlenderProcess;
|
||||
Reference in New Issue
Block a user