Blender  V2.93
node_ui_storage.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 
17 #include "CLG_log.h"
18 
19 #include <mutex>
20 
21 #include "BLI_map.hh"
22 #include "BLI_string_ref.hh"
23 #include "BLI_vector.hh"
24 
25 #include "DNA_node_types.h"
26 #include "DNA_object_types.h"
27 
28 #include "BKE_context.h"
29 #include "BKE_node_ui_storage.hh"
30 #include "BKE_object.h"
31 
32 static CLG_LogRef LOG = {"bke.node_ui_storage"};
33 
34 using blender::Map;
35 using blender::StringRef;
36 using blender::Vector;
37 
38 /* Use a global mutex because otherwise it would have to be stored directly in the
39  * bNodeTree struct in DNA. This could change if the node tree had a runtime struct. */
41 
43 {
44  /* As an optimization, only acquire a lock if the UI storage doesn't exist,
45  * because it only needs to be allocated once for every node tree. */
46  if (ntree.ui_storage == nullptr) {
47  std::lock_guard<std::mutex> lock(global_ui_storage_mutex);
48  /* Check again-- another thread may have allocated the storage while this one waited. */
49  if (ntree.ui_storage == nullptr) {
51  }
52  }
53 }
54 
56  const bNodeTree &ntree,
57  const bNode &node)
58 {
59  const NodeTreeUIStorage *ui_storage = ntree.ui_storage;
60  if (ui_storage == nullptr) {
61  return nullptr;
62  }
63 
64  const Object *active_object = CTX_data_active_object(C);
65  if (active_object == nullptr) {
66  return nullptr;
67  }
68 
69  const ModifierData *active_modifier = BKE_object_active_modifier(active_object);
70  if (active_modifier == nullptr) {
71  return nullptr;
72  }
73 
74  const NodeTreeEvaluationContext context(*active_object, *active_modifier);
75  const Map<std::string, NodeUIStorage> *storage = ui_storage->context_map.lookup_ptr(context);
76  if (storage == nullptr) {
77  return nullptr;
78  }
79 
80  return storage->lookup_ptr_as(StringRef(node.name));
81 }
82 
90 {
91  NodeTreeUIStorage *ui_storage = ntree.ui_storage;
92  if (ui_storage != nullptr) {
93  std::lock_guard<std::mutex> lock(ui_storage->context_map_mutex);
94  ui_storage->context_map.remove(context);
95  }
96 }
97 
99  const bNode &node,
100  const StringRef message,
101  const NodeWarningType type)
102 {
103  switch (type) {
105  CLOG_ERROR(&LOG,
106  "Node Tree: \"%s\", Node: \"%s\", %s",
107  ntree.id.name + 2,
108  node.name,
109  message.data());
110  break;
112  CLOG_WARN(&LOG,
113  "Node Tree: \"%s\", Node: \"%s\", %s",
114  ntree.id.name + 2,
115  node.name,
116  message.data());
117  break;
119  CLOG_INFO(&LOG,
120  2,
121  "Node Tree: \"%s\", Node: \"%s\", %s",
122  ntree.id.name + 2,
123  node.name,
124  message.data());
125  break;
126  }
127 }
128 
131  const bNode &node)
132 {
134  NodeTreeUIStorage &ui_storage = *ntree.ui_storage;
135 
136  std::lock_guard<std::mutex> lock(ui_storage.context_map_mutex);
137  Map<std::string, NodeUIStorage> &node_tree_ui_storage =
139 
140  NodeUIStorage &node_ui_storage = node_tree_ui_storage.lookup_or_add_default_as(
141  StringRef(node.name));
142 
143  return node_ui_storage;
144 }
145 
148  const bNode &node,
149  const NodeWarningType type,
150  std::string message)
151 {
153 
154  NodeUIStorage &node_ui_storage = node_ui_storage_ensure(ntree, context, node);
155  node_ui_storage.warnings.append({type, std::move(message)});
156 }
157 
160  const bNode &node,
161  const StringRef attribute_name,
162  const AttributeDomain domain,
163  const CustomDataType data_type)
164 {
165  NodeUIStorage &node_ui_storage = node_ui_storage_ensure(ntree, context, node);
166  node_ui_storage.attribute_hints.add_as(
167  AvailableAttributeInfo{attribute_name, domain, data_type});
168 }
AttributeDomain
Definition: BKE_attribute.h:41
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1279
NodeWarningType
General operations, lookup, etc. for blender objects.
struct ModifierData * BKE_object_active_modifier(const struct Object *ob)
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:204
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:203
#define CLOG_INFO(clg_ref, level,...)
Definition: CLG_log.h:201
ThreadMutex mutex
CustomDataType
Object is a sort of wrapper for general info.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
#define C
Definition: RandGen.cpp:39
Value & lookup_or_add_default_as(ForwardKey &&key)
Definition: BLI_map.hh:602
const Value * lookup_ptr_as(const ForwardKey &key) const
Definition: BLI_map.hh:485
Value & lookup_or_add_default(const Key &key)
Definition: BLI_map.hh:594
bool remove(const Key &key)
Definition: BLI_map.hh:337
const Value * lookup_ptr(const Key &key) const
Definition: BLI_map.hh:477
bool add_as(ForwardKey &&key)
Definition: BLI_set.hh:275
constexpr const char * data() const
void append(const T &value)
Definition: BLI_vector.hh:438
OperationNode * node
bNodeTree * ntree
static std::mutex global_ui_storage_mutex
static void node_error_message_log(bNodeTree &ntree, const bNode &node, const StringRef message, const NodeWarningType type)
void BKE_nodetree_ui_storage_free_for_context(bNodeTree &ntree, const NodeTreeEvaluationContext &context)
void BKE_nodetree_attribute_hint_add(bNodeTree &ntree, const NodeTreeEvaluationContext &context, const bNode &node, const StringRef attribute_name, const AttributeDomain domain, const CustomDataType data_type)
const NodeUIStorage * BKE_node_tree_ui_storage_get_from_context(const bContext *C, const bNodeTree &ntree, const bNode &node)
void BKE_nodetree_error_message_add(bNodeTree &ntree, const NodeTreeEvaluationContext &context, const bNode &node, const NodeWarningType type, std::string message)
static void ui_storage_ensure(bNodeTree &ntree)
static CLG_LogRef LOG
static NodeUIStorage & node_ui_storage_ensure(bNodeTree &ntree, const NodeTreeEvaluationContext &context, const bNode &node)
struct SELECTID_Context context
Definition: select_engine.c:47
char name[66]
Definition: DNA_ID.h:283
blender::Map< NodeTreeEvaluationContext, blender::Map< std::string, NodeUIStorage > > context_map
blender::Vector< NodeWarning > warnings
blender::Set< AvailableAttributeInfo > attribute_hints
struct NodeTreeUIStorage * ui_storage