feat(web): add an interactive Compositor node canvas

Author Compositor graphs in the web UI with React Flow and execute
them locally. The native writer materializes RGB/Exposure/Invert/Blur/
Mix/Alpha Over/Transform/Viewer/Composite nodes into a real
CompositorNodeTree, the reader returns node locations and more node
types, and save/reopen keeps nodes, links, positions and properties.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
mes123456
2026-11-13 05:35:30 +08:00
parent 4efb4b778d
commit ef50fdd988
18 changed files with 933 additions and 19 deletions

View File

@@ -2697,6 +2697,11 @@ std::string refresh_error;
applied = web_engine_blend_main_set_geometry_node_graph(
engine->authoritative_main, command.value("meshId", "").c_str(), graph.dump().c_str(), main_error);
}
else if (type == "setCompositorNodeGraph") {
const json graph = command.value("graph", json::object());
applied = web_engine_blend_main_set_compositor_node_graph(
engine->authoritative_main, command.value("sceneId", "").c_str(), graph.dump().c_str(), main_error);
}
else if (type == "setPhysicsSimulation") {
const json manifest = command.value("manifest", json::object());
applied = web_engine_blend_main_set_physics_simulation(

View File

@@ -918,6 +918,13 @@ std::optional<json> compositor_graph_from_scene(const ParsedBlend &blend,
{"name", (node_name.empty() ? blender_type : node_name).substr(0, 256)},
{"blenderType", blender_type.empty() ? "UNKNOWN" : blender_type.substr(0, 256)},
{"properties", json::object()}};
const std::vector<float> node_location = read_float_array(*blend.sdna, node, "location", 2);
if (node_location.size() >= 2 && std::isfinite(node_location[0]) &&
std::isfinite(node_location[1]) && std::abs(node_location[0]) <= 1.0e6f &&
std::abs(node_location[1]) <= 1.0e6f)
{
node_ir["location"] = {{"x", node_location[0]}, {"y", node_location[1]}};
}
if (blender_type == "CompositorNodeComposite" || blender_type == "NodeGroupOutput") {
node_ir["type"] = "COMPOSITE";
node_ir.erase("blenderType");
@@ -973,6 +980,58 @@ std::optional<json> compositor_graph_from_scene(const ParsedBlend &blend,
node_ir.erase("blenderType");
}
}
else if (blender_type == "CompositorNodeBlur") {
const std::optional<ElementRef> size = node_socket_default_element(blend, node, "Size");
const std::vector<float> values = size ? node_socket_default_values(blend, *size) :
std::vector<float>();
if (values.size() >= 2 && std::isfinite(values[0]) && std::isfinite(values[1]) &&
values[0] == values[1] && values[0] >= 0.0f && values[0] <= 32.0f)
{
node_ir["type"] = "BLUR";
node_ir.erase("blenderType");
node_ir["properties"] = {{"radius", static_cast<int>(std::lround(values[0]))}};
}
}
else if (blender_type == "CompositorNodeAlphaOver") {
node_ir["type"] = "ALPHA_OVER";
node_ir.erase("blenderType");
}
else if (blender_type == "CompositorNodeTransform") {
const auto socket_value = [&](const char *name) -> std::optional<float> {
const std::optional<ElementRef> socket = node_socket_default_element(blend, node, name);
return socket ? read_float(*blend.sdna, *socket, "value") : std::nullopt;
};
const std::optional<float> x = socket_value("X");
const std::optional<float> y = socket_value("Y");
const std::optional<float> scale = socket_value("Scale");
if (x && y && scale && std::isfinite(*x) && std::isfinite(*y) && std::isfinite(*scale) &&
*scale > 0.0f)
{
node_ir["type"] = "TRANSFORM";
node_ir.erase("blenderType");
node_ir["properties"] = {{"translateX", *x}, {"translateY", *y},
{"scaleX", *scale}, {"scaleY", *scale}};
}
}
else if (blender_type == "ShaderNodeMix") {
const std::optional<uint64_t> storage_pointer = read_pointer(*blend.sdna, node, "storage");
const std::optional<ElementRef> storage = storage_pointer ?
element_for_pointer(blend, *storage_pointer) : std::nullopt;
const std::optional<int64_t> data_type = storage ?
read_integer(*blend.sdna, *storage, "data_type") : std::nullopt;
const std::optional<int64_t> blend_type = storage ?
read_integer(*blend.sdna, *storage, "blend_type") : std::nullopt;
const std::optional<ElementRef> factor = node_socket_default_element(blend, node, "Factor_Float");
const std::optional<float> factor_value = factor ? read_float(*blend.sdna, *factor, "value") :
std::nullopt;
if (data_type == SOCK_RGBA && blend_type == 0 && factor_value &&
std::isfinite(*factor_value) && *factor_value >= 0.0f && *factor_value <= 1.0f)
{
node_ir["type"] = "MIX";
node_ir.erase("blenderType");
node_ir["properties"] = {{"factor", *factor_value}};
}
}
nodes.push_back(std::move(node_ir));
}
if (output_node_id.empty()) output_node_id = fallback_output_node_id;
@@ -998,6 +1057,19 @@ std::optional<json> compositor_graph_from_scene(const ParsedBlend &blend,
const std::optional<ElementRef> to_node = element_for_pointer(blend, *to_node_pointer);
const std::string to_blender_type = to_node ? read_string(*blend.sdna, *to_node, "idname") :
std::string();
const std::optional<ElementRef> from_node = element_for_pointer(blend, *from_node_pointer);
const std::string from_blender_type = from_node ?
read_string(*blend.sdna, *from_node, "idname") : std::string();
if (from_blender_type == "ShaderNodeMix" && from_socket_name == "Result_Color") {
from_socket_name = "Image";
}
if (from_blender_type == "CompositorNodeRGB" && from_socket_name == "Color") {
from_socket_name = "Image";
}
if (to_blender_type == "ShaderNodeMix") {
if (to_socket_name == "A_Color") to_socket_name = "A";
else if (to_socket_name == "B_Color") to_socket_name = "B";
}
if (to_blender_type == "CompositorNodeInvert" && to_socket_name == "Color") {
to_socket_name = "Image";
}

View File

@@ -20281,6 +20281,274 @@ bool web_engine_blend_main_set_geometry_node_graph(WebBlendMainState *state,
}
}
bool web_engine_blend_main_set_compositor_node_graph(WebBlendMainState *state,
const char *scene_id,
const char *graph_json,
std::string &error)
{
if (state == nullptr || state->main == nullptr || scene_id == nullptr || graph_json == nullptr ||
find_scene(state->main, scene_id) == nullptr)
{
error = "COMPOSITOR_GRAPH_INVALID: compositor target scene is unavailable";
return false;
}
try {
json graph = json::parse(graph_json);
if (graph.value("schemaVersion", 0) != 1 || graph.value("id", std::string()).empty() ||
!graph.contains("nodes") || !graph["nodes"].is_array() ||
!graph.contains("links") || !graph["links"].is_array() ||
graph["nodes"].empty() || graph["nodes"].size() > 4096 || graph["links"].size() > 16384)
{
error = "COMPOSITOR_GRAPH_INVALID: Compositor graph schema or bounds are invalid";
return false;
}
Scene *scene = find_scene(state->main, scene_id);
static const std::unordered_map<std::string, std::string> native_types = {
{"CONSTANT_COLOR", "CompositorNodeRGB"},
{"EXPOSURE", "CompositorNodeExposure"},
{"INVERT", "CompositorNodeInvert"},
{"BLUR", "CompositorNodeBlur"},
{"MIX", "ShaderNodeMix"},
{"ALPHA_OVER", "CompositorNodeAlphaOver"},
{"TRANSFORM", "CompositorNodeTransform"},
{"VIEWER", "CompositorNodeViewer"},
{"COMPOSITE", "NodeGroupOutput"}};
std::unordered_set<std::string> node_ids;
for (const json &node : graph["nodes"]) {
const std::string node_id = node.value("id", "");
const std::string node_type = node.value("type", "");
const std::string node_name = node.value("name", "");
if (node_id.empty() || node_name.empty() || !native_types.contains(node_type)) {
error = "COMPOSITOR_NODE_UNSUPPORTED: Compositor node type cannot be persisted";
return false;
}
if (!node_ids.insert(node_id).second) {
error = "COMPOSITOR_GRAPH_INVALID: duplicate Compositor node identity";
return false;
}
const json location = node.value("location", json::object());
for (const char *axis : {"x", "y"}) {
if (location.contains(axis)) {
const json value = location.at(axis);
if (!value.is_number() || !std::isfinite(value.get<float>()) || std::abs(value.get<float>()) > 1.0e6f) {
error = "COMPOSITOR_GRAPH_INVALID: Compositor node location is invalid";
return false;
}
}
}
}
const std::string output_node_id = graph.value("outputNodeId", "");
const auto output_node = std::find_if(graph["nodes"].begin(), graph["nodes"].end(),
[&](const json &node) { return node.value("id", "") == output_node_id; });
if (output_node == graph["nodes"].end() || output_node->value("type", "") != "COMPOSITE") {
error = "COMPOSITOR_GRAPH_INVALID: Compositor output must reference a COMPOSITE node";
return false;
}
bNodeTree *tree = scene->compositing_node_group;
if (tree == nullptr) {
tree = bke::node_tree_add_tree(state->main,
graph.value("name", std::string("Compositor")),
"CompositorNodeTree");
if (tree == nullptr) {
error = "COMPOSITOR_GRAPH_INVALID: Blender could not create a CompositorNodeTree datablock";
return false;
}
scene->compositing_node_group = tree;
id_us_plus(&tree->id);
}
while (tree->nodes.first != nullptr) {
bke::node_remove_node(state->main, *tree,
*static_cast<bNode *>(tree->nodes.first), true);
}
bool has_image_output = false;
for (const bNodeTreeInterfaceItem *item : tree->tree_interface.root_panel.items()) {
if (item->item_type != NodeTreeInterfaceItemType::Socket) continue;
const auto *socket = reinterpret_cast<const bNodeTreeInterfaceSocket *>(item);
if (socket->flag & NODE_INTERFACE_SOCKET_OUTPUT) {
if (socket->socket_type != nullptr && std::string(socket->socket_type) == "NodeSocketColor") {
has_image_output = true;
}
}
}
if (!has_image_output) {
if (tree->tree_interface.add_socket("Image", "", "NodeSocketColor",
NODE_INTERFACE_SOCKET_OUTPUT, nullptr) == nullptr) {
error = "COMPOSITOR_GRAPH_INVALID: Blender could not create the Compose output socket";
return false;
}
}
const auto socket_named = [](bNode &node, const std::string &name,
const bool input) -> bNodeSocket * {
ListBase *sockets = input ? &node.inputs : &node.outputs;
for (bNodeSocket *socket = static_cast<bNodeSocket *>(sockets->first); socket != nullptr;
socket = socket->next)
{
if (name == socket->identifier || name == socket->name) return socket;
}
return nullptr;
};
const auto native_input_name = [](const std::string &type,
const std::string &name) -> std::string {
if (type == "INVERT" && name == "Image") return "Color";
if (type == "MIX") {
if (name == "A") return "A_Color";
if (name == "B") return "B_Color";
}
return name;
};
const auto native_output_name = [](const std::string &type,
const std::string &name) -> std::string {
if (type == "MIX" && name == "Image") return "Result_Color";
if (type == "CONSTANT_COLOR" && name == "Image") return "Color";
return name;
};
const auto set_default = [&](bNode &node, const bool input, const std::string &name,
const json &value) -> bool {
bNodeSocket *socket = socket_named(node, name, input);
if (socket == nullptr || socket->default_value == nullptr) return false;
switch (socket->type) {
case SOCK_FLOAT:
if (!value.is_number()) return false;
static_cast<bNodeSocketValueFloat *>(socket->default_value)->value = value.get<float>();
return true;
case SOCK_INT:
case SOCK_MENU:
if (!value.is_number()) return false;
static_cast<bNodeSocketValueInt *>(socket->default_value)->value = value.get<int>();
return true;
case SOCK_BOOLEAN:
if (!value.is_boolean()) return false;
static_cast<bNodeSocketValueBoolean *>(socket->default_value)->value = value.get<bool>();
return true;
case SOCK_RGBA: {
if (!value.is_array() || value.size() != 4) return false;
float *values = static_cast<bNodeSocketValueRGBA *>(socket->default_value)->value;
for (size_t index = 0; index < 4; index++) values[index] = value[index].get<float>();
return true;
}
case SOCK_VECTOR: {
if (!value.is_array() || value.size() < 2 || value.size() > 4) return false;
auto *vector = static_cast<bNodeSocketValueVector *>(socket->default_value);
vector->dimensions = static_cast<int>(value.size());
for (size_t index = 0; index < value.size(); index++) vector->value[index] = value[index].get<float>();
return true;
}
default:
return false;
}
};
std::unordered_map<std::string, bNode *> native_nodes;
std::unordered_map<std::string, std::string> native_node_types;
for (const json &node : graph["nodes"]) {
const std::string node_id = node.value("id", "");
const std::string node_type = node.value("type", "");
const std::string native_type = native_types.at(node_type);
bNode *native = bke::node_add_node(nullptr, *tree, UString(native_type));
if (native == nullptr) {
error = "COMPOSITOR_NODE_UNSUPPORTED: Blender does not register " + native_type;
return false;
}
STRNCPY_UTF8(native->name, node.value("name", node_id).c_str());
STRNCPY_UTF8(native->label, node.value("name", node_id).c_str());
const json location = node.value("location", json::object());
native->location[0] = location.value("x", 0.0f);
native->location[1] = location.value("y", 0.0f);
if (node_type == "MIX") {
auto *storage = static_cast<NodeShaderMix *>(native->storage);
if (storage == nullptr) {
error = "COMPOSITOR_NODE_UNSUPPORTED: Mix node storage is unavailable";
return false;
}
storage->data_type = SOCK_RGBA;
storage->blend_type = 0;
}
native_nodes.emplace(node_id, native);
native_node_types.emplace(node_id, node_type);
}
for (const json &node : graph["nodes"]) {
bNode *native = native_nodes.at(node.value("id", ""));
const std::string node_type = node.value("type", "");
const json properties = node.value("properties", json::object());
if (node_type == "CONSTANT_COLOR") {
if (!set_default(*native, false, "Color", properties.value("color", json::array({0.5, 0.5, 0.5, 1.0})))) {
error = "COMPOSITOR_GRAPH_INVALID: RGB color default has no native storage";
return false;
}
}
else if (node_type == "EXPOSURE") {
if (!set_default(*native, true, "Exposure", properties.value("exposure", 0.0))) {
error = "COMPOSITOR_GRAPH_INVALID: Exposure default has no native storage";
return false;
}
}
else if (node_type == "BLUR") {
const int radius = properties.value("radius", 0);
if (!set_default(*native, true, "Size", json::array({radius, radius}))) {
error = "COMPOSITOR_GRAPH_INVALID: Blur size default has no native storage";
return false;
}
}
else if (node_type == "MIX") {
if (!set_default(*native, true, "Factor_Float", properties.value("factor", 0.5))) {
error = "COMPOSITOR_GRAPH_INVALID: Mix factor default has no native storage";
return false;
}
}
else if (node_type == "TRANSFORM") {
if (!set_default(*native, true, "X", properties.value("translateX", 0.0)) ||
!set_default(*native, true, "Y", properties.value("translateY", 0.0)) ||
!set_default(*native, true, "Scale", properties.value("scaleX", 1.0)))
{
error = "COMPOSITOR_GRAPH_INVALID: Transform default has no native storage";
return false;
}
}
if (node.value("id", "") == output_node_id) native->flag |= NODE_DO_OUTPUT;
}
for (const json &link : graph["links"]) {
const std::string from_id = link.value("fromNodeId", "");
const std::string to_id = link.value("toNodeId", "");
if (!native_nodes.contains(from_id) || !native_nodes.contains(to_id)) {
error = "COMPOSITOR_GRAPH_INVALID: Compositor link references a missing node";
return false;
}
bNode *from_node = native_nodes.at(from_id);
bNode *to_node = native_nodes.at(to_id);
const std::string from_type = native_node_types.at(from_id);
const std::string to_type = native_node_types.at(to_id);
bNodeSocket *from = socket_named(*from_node,
native_output_name(from_type, link.value("fromSocket", "")), false);
bNodeSocket *to = socket_named(*to_node,
native_input_name(to_type, link.value("toSocket", "")), true);
if (from == nullptr || to == nullptr) {
error = "COMPOSITOR_GRAPH_INVALID: Compositor link references an unavailable socket " +
from_type + ":" + link.value("fromSocket", "") + " -> " + to_type + ":" +
link.value("toSocket", "");
return false;
}
bke::node_add_link(*tree, *from_node, *from, *to_node, *to);
}
BKE_ntree_update_tag_all(tree);
BKE_ntree_update_after_single_tree_change(*state->main, *tree);
tree->id.recalc |= ID_RECALC_NTREE_OUTPUT;
scene->r.scemode |= R_DOCOMP;
return true;
}
catch (const std::exception &exception) {
error = std::string("COMPOSITOR_GRAPH_INVALID: Compositor graph JSON is invalid: ") + exception.what();
return false;
}
}
bool web_engine_blend_main_physics_simulation_json(WebBlendMainState *state,
std::string &physics_json,
std::string &error)

View File

@@ -752,6 +752,10 @@ bool web_engine_blend_main_set_geometry_node_graph(WebBlendMainState *state,
const char *mesh_id,
const char *graph_json,
std::string &error);
bool web_engine_blend_main_set_compositor_node_graph(WebBlendMainState *state,
const char *scene_id,
const char *graph_json,
std::string &error);
bool web_engine_blend_main_physics_simulation_json(WebBlendMainState *state,
std::string &physics_json,
std::string &error);