Blender V4.3
node_tree_dot_export.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_node_runtime.hh"
7
8#include "BLI_dot_export.hh"
9
10namespace blender::bke {
11
12std::string bNodeTreeToDotOptions::socket_name(const bNodeSocket &socket) const
13{
14 return socket.identifier;
15}
16
18 const bNodeSocket &socket) const
19{
20 if (!socket.is_available()) {
21 return "#999999";
22 }
23 return std::nullopt;
24}
25
27 dot::DirectedEdge &dot_edge) const
28{
29 if (!link.is_used()) {
30 dot_edge.attributes.set("color", "#999999");
31 }
32}
33
35{
36 tree.ensure_topology_cache();
37
38 dot::DirectedGraph digraph;
39 digraph.set_rankdir(dot::Attr_rankdir::LeftToRight);
40
42
43 for (const bNode *node : tree.all_nodes()) {
44 dot::Node &dot_node = digraph.new_node("");
45 dot::NodeWithSockets dot_node_with_sockets;
46 dot_node_with_sockets.node_name = node->label_or_name();
47 for (const bNodeSocket *socket : node->input_sockets()) {
48 dot::NodeWithSockets::Input &dot_input = dot_node_with_sockets.add_input(
49 options.socket_name(*socket));
50 dot_input.fontcolor = options.socket_font_color(*socket);
51 }
52 for (const bNodeSocket *socket : node->output_sockets()) {
53 dot::NodeWithSockets::Output &dot_output = dot_node_with_sockets.add_output(
54 options.socket_name(*socket));
55 dot_output.fontcolor = options.socket_font_color(*socket);
56 }
57 dot_nodes.add_new(node, dot::NodeWithSocketsRef(dot_node, dot_node_with_sockets));
58 }
59
60 for (const bNodeLink *link : tree.all_links()) {
61 const dot::NodeWithSocketsRef &from_dot_node = dot_nodes.lookup(link->fromnode);
62 const dot::NodeWithSocketsRef &to_dot_node = dot_nodes.lookup(link->tonode);
63 const dot::NodePort from_dot_port = from_dot_node.output(link->fromsock->index());
64 const dot::NodePort to_dot_port = to_dot_node.input(link->tosock->index());
65
66 dot::DirectedEdge &dot_edge = digraph.new_edge(from_dot_port, to_dot_port);
67 options.add_edge_attributes(*link, dot_edge);
68 }
69
70 return digraph.to_dot_string();
71}
72
73} // namespace blender::bke
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:241
virtual std::string socket_name(const bNodeSocket &socket) const
virtual std::optional< std::string > socket_font_color(const bNodeSocket &socket) const
virtual void add_edge_attributes(const bNodeLink &link, dot::DirectedEdge &dot_edge) const
void set(StringRef key, StringRef value)
DirectedEdge & new_edge(NodePort from, NodePort to)
Definition dot_export.cc:41
std::string to_dot_string() const
Node & new_node(StringRef label)
Definition dot_export.cc:16
void set_rankdir(Attr_rankdir rankdir)
NodePort output(int index) const
NodePort input(int index) const
CCL_NAMESPACE_BEGIN struct Options options
KDTree_3d * tree
std::string node_tree_to_dot(const bNodeTree &tree, const bNodeTreeToDotOptions &options=bNodeTreeToDotOptions())
char identifier[64]
std::optional< std::string > fontcolor
Input & add_input(std::string name)
Output & add_output(std::string name)