Blender  V2.93
COM_Node.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2011, Blender Foundation.
17  */
18 
19 #include <cstring>
20 
21 #include "BKE_node.h"
22 
23 #include "RNA_access.h"
24 
25 #include "COM_ExecutionSystem.h"
26 #include "COM_NodeOperation.h"
27 #include "COM_TranslateOperation.h"
28 
29 #include "COM_SocketProxyNode.h"
30 
31 #include "COM_defines.h"
32 
33 #include "COM_Node.h" /* own include */
34 
35 namespace blender::compositor {
36 
37 /**************
38  **** Node ****
39  **************/
40 
41 Node::Node(bNode *editorNode, bool create_sockets)
42  : m_editorNodeTree(nullptr),
43  m_editorNode(editorNode),
44  m_inActiveGroup(false),
45  m_instanceKey(NODE_INSTANCE_KEY_NONE)
46 {
47  if (create_sockets) {
48  bNodeSocket *input = (bNodeSocket *)editorNode->inputs.first;
49  while (input != nullptr) {
51  if (input->type == SOCK_RGBA) {
52  dt = DataType::Color;
53  }
54  if (input->type == SOCK_VECTOR) {
55  dt = DataType::Vector;
56  }
57 
58  this->addInputSocket(dt, input);
59  input = input->next;
60  }
61  bNodeSocket *output = (bNodeSocket *)editorNode->outputs.first;
62  while (output != nullptr) {
64  if (output->type == SOCK_RGBA) {
65  dt = DataType::Color;
66  }
67  if (output->type == SOCK_VECTOR) {
68  dt = DataType::Vector;
69  }
70 
71  this->addOutputSocket(dt, output);
72  output = output->next;
73  }
74  }
75 }
76 
78 {
79  while (!this->outputs.is_empty()) {
80  delete (this->outputs.pop_last());
81  }
82  while (!this->inputs.is_empty()) {
83  delete (this->inputs.pop_last());
84  }
85 }
86 
88 {
89  this->addInputSocket(datatype, nullptr);
90 }
91 
92 void Node::addInputSocket(DataType datatype, bNodeSocket *bSocket)
93 {
94  NodeInput *socket = new NodeInput(this, bSocket, datatype);
95  this->inputs.append(socket);
96 }
97 
99 {
100  this->addOutputSocket(datatype, nullptr);
101 }
103 {
104  NodeOutput *socket = new NodeOutput(this, bSocket, datatype);
105  outputs.append(socket);
106 }
107 
108 NodeOutput *Node::getOutputSocket(unsigned int index) const
109 {
110  return outputs[index];
111 }
112 
113 NodeInput *Node::getInputSocket(unsigned int index) const
114 {
115  return inputs[index];
116 }
117 
118 bNodeSocket *Node::getEditorInputSocket(int editorNodeInputSocketIndex)
119 {
120  bNodeSocket *bSock = (bNodeSocket *)this->getbNode()->inputs.first;
121  int index = 0;
122  while (bSock != nullptr) {
123  if (index == editorNodeInputSocketIndex) {
124  return bSock;
125  }
126  index++;
127  bSock = bSock->next;
128  }
129  return nullptr;
130 }
131 bNodeSocket *Node::getEditorOutputSocket(int editorNodeOutputSocketIndex)
132 {
133  bNodeSocket *bSock = (bNodeSocket *)this->getbNode()->outputs.first;
134  int index = 0;
135  while (bSock != nullptr) {
136  if (index == editorNodeOutputSocketIndex) {
137  return bSock;
138  }
139  index++;
140  bSock = bSock->next;
141  }
142  return nullptr;
143 }
144 
145 /*******************
146  **** NodeInput ****
147  *******************/
148 
150  : m_node(node), m_editorSocket(b_socket), m_datatype(datatype), m_link(nullptr)
151 {
152 }
153 
155 {
156  m_link = link;
157 }
158 
160 {
161  PointerRNA ptr;
162  RNA_pointer_create((ID *)getNode()->getbNodeTree(), &RNA_NodeSocket, getbNodeSocket(), &ptr);
163  return RNA_float_get(&ptr, "default_value");
164 }
165 
166 void NodeInput::getEditorValueColor(float *value) const
167 {
168  PointerRNA ptr;
169  RNA_pointer_create((ID *)getNode()->getbNodeTree(), &RNA_NodeSocket, getbNodeSocket(), &ptr);
170  return RNA_float_get_array(&ptr, "default_value", value);
171 }
172 
173 void NodeInput::getEditorValueVector(float *value) const
174 {
175  PointerRNA ptr;
176  RNA_pointer_create((ID *)getNode()->getbNodeTree(), &RNA_NodeSocket, getbNodeSocket(), &ptr);
177  return RNA_float_get_array(&ptr, "default_value", value);
178 }
179 
180 /********************
181  **** NodeOutput ****
182  ********************/
183 
185  : m_node(node), m_editorSocket(b_socket), m_datatype(datatype)
186 {
187 }
188 
190 {
191  PointerRNA ptr;
192  RNA_pointer_create((ID *)getNode()->getbNodeTree(), &RNA_NodeSocket, getbNodeSocket(), &ptr);
193  return RNA_float_get(&ptr, "default_value");
194 }
195 
197 {
198  PointerRNA ptr;
199  RNA_pointer_create((ID *)getNode()->getbNodeTree(), &RNA_NodeSocket, getbNodeSocket(), &ptr);
200  return RNA_float_get_array(&ptr, "default_value", value);
201 }
202 
204 {
205  PointerRNA ptr;
206  RNA_pointer_create((ID *)getNode()->getbNodeTree(), &RNA_NodeSocket, getbNodeSocket(), &ptr);
207  return RNA_float_get_array(&ptr, "default_value", value);
208 }
209 
210 } // namespace blender::compositor
const bNodeInstanceKey NODE_INSTANCE_KEY_NONE
Definition: node.cc:3909
@ SOCK_VECTOR
@ SOCK_RGBA
StructRNA RNA_NodeSocket
struct btDbvtNode * m_node
#define output
NodeInput are sockets that can receive data/input.
Definition: COM_Node.h:210
NodeInput(Node *node, bNodeSocket *b_socket, DataType datatype)
Definition: COM_Node.cc:149
void getEditorValueColor(float *value) const
Definition: COM_Node.cc:166
void getEditorValueVector(float *value) const
Definition: COM_Node.cc:173
void setLink(NodeOutput *link)
Definition: COM_Node.cc:154
bNodeSocket * getbNodeSocket() const
Definition: COM_Node.h:234
float getEditorValueFloat() const
Definition: COM_Node.cc:159
NodeOutput are sockets that can send data/input.
Definition: COM_Node.h:258
bNodeSocket * getbNodeSocket() const
Definition: COM_Node.h:276
void getEditorValueColor(float *value)
Definition: COM_Node.cc:196
NodeOutput(Node *node, bNodeSocket *b_socket, DataType datatype)
Definition: COM_Node.cc:184
void getEditorValueVector(float *value)
Definition: COM_Node.cc:203
Vector< NodeInput * > inputs
the list of actual input-sockets
Definition: COM_Node.h:68
Vector< NodeOutput * > outputs
the list of actual output-sockets
Definition: COM_Node.h:73
NodeOutput * getOutputSocket(const unsigned int index=0) const
Definition: COM_Node.cc:108
bNodeSocket * getEditorInputSocket(int editorNodeInputSocketIndex)
Definition: COM_Node.cc:118
bNode * getbNode() const
get the reference to the SDNA bNode struct
Definition: COM_Node.h:82
Node(bNode *editorNode, bool create_sockets=true)
Definition: COM_Node.cc:41
NodeInput * getInputSocket(const unsigned int index) const
Definition: COM_Node.cc:113
bNodeSocket * getEditorOutputSocket(int editorNodeOutputSocketIndex)
Definition: COM_Node.cc:131
void addOutputSocket(DataType datatype)
add an NodeOutput to the collection of output-sockets
Definition: COM_Node.cc:98
void addInputSocket(DataType datatype)
add an NodeInput to the collection of input-sockets
Definition: COM_Node.cc:87
OperationNode * node
DataType
possible data types for sockets
Definition: COM_defines.h:27
@ Vector
Vector data type.
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
Definition: rna_access.c:6378
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6355
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
struct bNodeSocket * next
ListBase inputs
ListBase outputs
PointerRNA * ptr
Definition: wm_files.c:3157