[Bf-blender-cvs] [0e1a4f39ac2] functions: use generic dot export to export function networks
Jacques Lucke
noreply at git.blender.org
Sun Nov 24 15:24:53 CET 2019
Commit: 0e1a4f39ac2adad36f69898cdb12d0c90aab7bd5
Author: Jacques Lucke
Date: Sun Nov 24 15:24:46 2019 +0100
Branches: functions
https://developer.blender.org/rB0e1a4f39ac2adad36f69898cdb12d0c90aab7bd5
use generic dot export to export function networks
===================================================================
M source/blender/blenkernel/intern/inlined_node_tree.cc
M source/blender/blenlib/intern/dot_export.cc
M source/blender/functions/intern/multi_function_network.cc
===================================================================
diff --git a/source/blender/blenkernel/intern/inlined_node_tree.cc b/source/blender/blenkernel/intern/inlined_node_tree.cc
index da221282610..edfce314d35 100644
--- a/source/blender/blenkernel/intern/inlined_node_tree.cc
+++ b/source/blender/blenkernel/intern/inlined_node_tree.cc
@@ -283,22 +283,18 @@ std::string InlinedNodeTree::to_dot() const
for (const XNode *xnode : m_node_by_id) {
auto &dot_node = digraph.new_node("");
- StringRef name = xnode->m_vnode->name();
-
Vector<std::string> input_names;
for (const XInputSocket *input : xnode->m_inputs) {
- StringRef input_name = input->m_vsocket->name();
- input_names.append((input_name.size() == 0) ? "noname" : input_name);
+ input_names.append(input->m_vsocket->name());
}
Vector<std::string> output_names;
for (const XOutputSocket *output : xnode->m_outputs) {
- StringRef output_name = output->m_vsocket->name();
- output_names.append((output_name.size() == 0) ? "noname" : output_name);
+ output_names.append(output->m_vsocket->name());
}
- dot_nodes.add_new(
- xnode,
- BLI::DotExport::Utils::NodeWithSocketsWrapper(dot_node, name, input_names, output_names));
+ dot_nodes.add_new(xnode,
+ BLI::DotExport::Utils::NodeWithSocketsWrapper(
+ dot_node, xnode->m_vnode->name(), input_names, output_names));
for (const XInputSocket *input : xnode->m_inputs) {
for (const XGroupInput *group_input : input->m_linked_group_inputs) {
diff --git a/source/blender/blenlib/intern/dot_export.cc b/source/blender/blenlib/intern/dot_export.cc
index c79d7e4a010..4373c5f54f1 100644
--- a/source/blender/blenlib/intern/dot_export.cc
+++ b/source/blender/blenlib/intern/dot_export.cc
@@ -174,7 +174,7 @@ NodeWithSocketsWrapper::NodeWithSocketsWrapper(Node &node,
/* Header */
ss << "<tr><td colspan=\"3\" align=\"center\"><b>";
- ss << name;
+ ss << ((name.size() == 0) ? "No Name" : name);
ss << "</b></td></tr>";
/* Sockets */
@@ -183,6 +183,9 @@ NodeWithSocketsWrapper::NodeWithSocketsWrapper(Node &node,
ss << "<tr>";
if (i < input_names.size()) {
StringRef name = input_names[i];
+ if (name.size() == 0) {
+ name = "No Name";
+ }
ss << "<td align=\"left\" port=\"in" << i << "\">";
ss << name;
ss << "</td>";
@@ -193,6 +196,9 @@ NodeWithSocketsWrapper::NodeWithSocketsWrapper(Node &node,
ss << "<td></td>";
if (i < output_names.size()) {
StringRef name = output_names[i];
+ if (name.size() == 0) {
+ name = "No Name";
+ }
ss << "<td align=\"right\" port=\"out" << i << "\">";
ss << name;
ss << "</td>";
diff --git a/source/blender/functions/intern/multi_function_network.cc b/source/blender/functions/intern/multi_function_network.cc
index 0796faf620f..e650000fe51 100644
--- a/source/blender/functions/intern/multi_function_network.cc
+++ b/source/blender/functions/intern/multi_function_network.cc
@@ -4,6 +4,7 @@
#include "BLI_set.h"
#include "BLI_stack_cxx.h"
+#include "BLI_dot_export.h"
extern "C" {
void WM_clipboard_text_set(const char *buf, bool selection);
@@ -11,6 +12,7 @@ void WM_clipboard_text_set(const char *buf, bool selection);
namespace FN {
+using BLI::Map;
using BLI::Set;
using BLI::Stack;
@@ -146,111 +148,47 @@ void MFNetworkBuilder::add_link(MFBuilderOutputSocket &from, MFBuilderInputSocke
to.m_origin = &from;
}
-namespace DotExport {
-
-static std::string get_id(MFBuilderNode &node)
+std::string MFNetworkBuilder::to_dot()
{
- std::stringstream ss;
- ss << "\"";
- ss << (void *)&node;
- ss << "\"";
- return ss.str();
-}
+ using BLI::DotExport::Utils::NodeWithSocketsWrapper;
-static std::string get_id(MFBuilderSocket &socket)
-{
- std::stringstream ss;
- ss << "\"";
- ss << (void *)&socket;
- ss << "\"";
- return ss.str();
-}
+ BLI::DotExport::DirectedGraph digraph;
+ digraph.set_rankdir(BLI::DotExport::Attr_rankdir::LeftToRight);
+ Map<MFBuilderNode *, NodeWithSocketsWrapper> dot_nodes;
-static std::string port_id(MFBuilderSocket &socket)
-{
- return get_id(socket.node()) + ":" + get_id(socket);
-}
+ for (MFBuilderNode *node : m_node_by_id) {
+ auto &dot_node = digraph.new_node("");
-static void insert_node_table(std::stringstream &ss, MFBuilderNode &node)
-{
- ss << "<table border=\"0\" cellspacing=\"3\">";
-
- /* Header */
- ss << "<tr><td colspan=\"3\" align=\"center\"><b>";
- ss << node.name();
- ss << "</b></td></tr>";
-
- /* Sockets */
- auto inputs = node.inputs();
- auto outputs = node.outputs();
- uint socket_max_amount = std::max(inputs.size(), outputs.size());
- for (uint i = 0; i < socket_max_amount; i++) {
- ss << "<tr>";
- if (i < inputs.size()) {
- MFBuilderInputSocket &socket = *inputs[i];
- ss << "<td align=\"left\" port=" << get_id(socket) << ">";
- ss << socket.name() << " (" << socket.data_type() << ")";
- ss << "</td>";
- }
- else {
- ss << "<td></td>";
+ Vector<std::string> input_names;
+ for (MFBuilderInputSocket *socket : node->inputs()) {
+ input_names.append(socket->name());
}
- ss << "<td></td>";
- if (i < outputs.size()) {
- MFBuilderOutputSocket &socket = *outputs[i];
- ss << "<td align=\"right\" port=" << get_id(socket) << ">";
- ss << socket.name() << " (" << socket.data_type() << ")";
- ss << "</td>";
+ Vector<std::string> output_names;
+ for (MFBuilderOutputSocket *socket : node->outputs()) {
+ output_names.append(socket->name());
}
- else {
- ss << "<td></td>";
- }
- ss << "</tr>";
- }
- ss << "</table>";
-}
-
-static void insert_node(std::stringstream &ss, MFBuilderNode &node)
-{
- ss << get_id(node) << " ";
- ss << "[style=\"filled\", fillcolor=\"#FFFFFF\", shape=\"box\"";
- ss << ", label=<";
- insert_node_table(ss, node);
- ss << ">]";
-}
-
-static void insert_link(std::stringstream &ss,
- MFBuilderOutputSocket &from,
- MFBuilderInputSocket &to)
-{
- ss << port_id(from) << " -> " << port_id(to);
-}
+ dot_nodes.add_new(node,
+ NodeWithSocketsWrapper(dot_node, node->name(), input_names, output_names));
+ }
-}; // namespace DotExport
+ for (MFBuilderNode *to_node : m_node_by_id) {
+ auto to_dot_node = dot_nodes.lookup(to_node);
-std::string MFNetworkBuilder::to_dot()
-{
- std::stringstream ss;
- ss << "digraph MyGraph {" << std::endl;
- ss << "rankdir=LR" << std::endl;
+ for (MFBuilderInputSocket *to_socket : to_node->inputs()) {
+ MFBuilderOutputSocket *from_socket = to_socket->origin();
+ if (from_socket != nullptr) {
+ MFBuilderNode &from_node = from_socket->node();
- for (MFBuilderNode *node : m_node_by_id) {
- DotExport::insert_node(ss, *node);
- ss << std::endl;
- }
+ auto from_dot_node = dot_nodes.lookup(&from_node);
- for (MFBuilderNode *node : m_node_by_id) {
- for (MFBuilderInputSocket *input : node->inputs()) {
- if (input->origin() != nullptr) {
- DotExport::insert_link(ss, *input->origin(), *input);
- ss << std::endl;
+ digraph.new_edge(from_dot_node.output(from_socket->index()),
+ to_dot_node.input(to_socket->index()));
}
}
}
- ss << "}\n";
- return ss.str();
+ return digraph.to_dot_string();
}
void MFNetworkBuilder::to_dot__clipboard()
More information about the Bf-blender-cvs
mailing list