Blender V5.3
node_context_path.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <algorithm>
11
12#include "BLI_listbase.hh"
13#include "BLI_string.hh"
14#include "BLI_vector.hh"
15
16#include "DNA_node_types.h"
17
18#include "BKE_compositor.hh"
19#include "BKE_context.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_material.hh"
22#include "BKE_node.hh"
23#include "BKE_node_runtime.hh"
24#include "BKE_object.hh"
25
26#include "RNA_access.hh"
27#include "RNA_prototypes.hh"
28
29#include "ED_node_c.hh"
30#include "ED_screen.hh"
31
32#include "SEQ_modifier.hh"
33#include "SEQ_select.hh"
34#include "SEQ_sequencer.hh"
35
36#include "WM_api.hh"
37
38#include "UI_interface.hh"
39#include "UI_resources.hh"
40
41#include "node_intern.hh"
42
43namespace blender {
44
45struct Material;
46
47namespace ed::space_node {
48
50{
51 if (!object.data) {
52 return;
53 }
54 if (object.type == OB_MESH) {
55 ui::context_path_add_generic(path, *RNA_Mesh, object.data);
56 }
57 else if (object.type == OB_CURVES) {
58 ui::context_path_add_generic(path, *RNA_Curves, object.data);
59 }
60 else if (object.type == OB_LAMP) {
61 ui::context_path_add_generic(path, *RNA_Light, object.data);
62 }
63 else if (ELEM(object.type, OB_CURVES_LEGACY, OB_FONT, OB_SURF)) {
64 ui::context_path_add_generic(path, *RNA_Curve, object.data);
65 }
66}
67
68static std::function<void(bContext &)> tree_path_handle_func(int i)
69{
70 return [i](bContext &C) {
71 wmOperatorType *ot = WM_operatortype_find("NODE_OT_tree_path_parent", false);
73 RNA_int_set(&op_props, "parent_tree_index", i);
76 };
77}
78
79static BIFIconID node_tree_icon(const ID &tree_id)
80{
81 if (ID_IS_PACKED(&tree_id)) {
82 return ICON_PACKAGE;
83 }
84 if (ID_IS_LINKED(&tree_id)) {
85 return ICON_LINKED;
86 }
87 if (ID_IS_ASSET(&tree_id)) {
88 return ICON_ASSET_MANAGER;
89 }
90 return ICON_NODETREE;
91}
92
94{
95 return node.is_group() && !node.is_custom_group() && node.id && !ID_MISSING(node.id);
96}
97
99{
100 if (ntree == nullptr) {
101 return false;
102 }
103 ntree->ensure_topology_cache();
104 for (const bNode *group_node : ntree->group_nodes()) {
105 if (node_group_is_enterable(*group_node)) {
106 return true;
107 }
108 }
109 return false;
110}
111
112static void navigate_menu_create(bContext *C, ui::Layout *layout, void *arg)
113{
114 SpaceNode *snode = CTX_wm_space_node(C);
115 bNodeTree *tree = static_cast<bNodeTree *>(arg);
116 if (!snode || !tree) {
117 return;
118 }
119
120 bNodeTree *child_tree_in_path = nullptr;
121 for (bNodeTreePath &path_item : snode->treepath) {
122 if (path_item.nodetree == tree && path_item.next) {
123 child_tree_in_path = path_item.next->nodetree;
124 break;
125 }
126 }
127
128 tree->ensure_topology_cache();
129 Set<bNodeTree *> visited_groups;
130 Vector<bNode *> group_nodes;
131 for (bNode *group_node : tree->group_nodes()) {
132 if (!node_group_is_enterable(*group_node)) {
133 continue;
134 }
135 /* When a group is used multiple times, the menu enters the first node found. */
136 if (visited_groups.add(id_cast<bNodeTree *>(group_node->id))) {
137 group_nodes.append(group_node);
138 }
139 }
140 std::ranges::sort(group_nodes, [](const bNode *a, const bNode *b) {
141 return BLI_strcasecmp_natural(BKE_id_name(*a->id), BKE_id_name(*b->id)) < 0;
142 });
143
144 for (bNode *group_node : group_nodes) {
145 bNodeTree *group_tree = id_cast<bNodeTree *>(group_node->id);
146
147 auto enter_group_fn = [tree, group_node, group_tree](bContext &C) {
148 SpaceNode *snode = CTX_wm_space_node(&C);
149 ARegion *region = CTX_wm_region(&C);
150
151 while (snode->edittree != tree) {
152 ED_node_tree_pop(region, snode);
153 }
154 bke::node_set_active(*tree, *group_node);
155 ED_node_tree_push(region, snode, group_tree, group_node);
156 };
157
158 ui::Button *but = layout->button(
159 BKE_id_name(*group_node->id), node_tree_icon(*group_node->id), enter_group_fn);
160 if (group_tree == child_tree_in_path) {
161 /* Highlight the current path item and it becomes the default item for Enter. */
163 }
164 }
165}
166
169 StructRNA &rna_type,
170 void *ptr,
171 bNodeTree *tree)
172{
173 const bool in_root_tree = snode.nodetree == snode.edittree;
174 const bool has_group = in_root_tree ? node_tree_has_enterable_group(tree) : true;
176 rna_type,
177 ptr,
178 ICON_NONE,
179 in_root_tree ? nullptr : tree_path_handle_func(0),
180 has_group ? navigate_menu_create : nullptr,
181 tree);
182}
183
186 const bool skip_base = false)
187{
188
189 for (const auto [i, path_item] : snode.treepath.enumerate()) {
190 if (skip_base && &path_item == snode.treepath.first_) {
191 continue;
192 }
193 if (path_item.nodetree == nullptr) {
194 continue;
195 }
196
197 const bool is_last_path = &path_item == snode.treepath.last();
198 const bool has_group = is_last_path ? node_tree_has_enterable_group(path_item.nodetree) : true;
200 *RNA_NodeTree,
201 path_item.nodetree,
202 node_tree_icon(path_item.nodetree->id),
203 is_last_path ? nullptr : tree_path_handle_func(i),
204 has_group ? navigate_menu_create : nullptr,
205 path_item.nodetree);
206 }
207}
208
210 SpaceNode &snode,
212{
213 if (snode.flag & SNODE_PIN) {
214 if (snode.shaderfrom == SNODE_SHADER_WORLD) {
215 Scene *scene = CTX_data_scene(&C);
216 ui::context_path_add_generic(path, *RNA_Scene, scene);
217 if (scene != nullptr) {
219 snode, path, *RNA_World, scene->world, snode.nodetree);
220 }
221 /* Skip the base node tree here, because the world contains a node tree already. */
223 }
224 else {
226 }
227 }
228 else {
229 Object *object = CTX_data_active_object(&C);
230 if (snode.shaderfrom == SNODE_SHADER_OBJECT && object != nullptr) {
231 ui::context_path_add_generic(path, *RNA_Object, object);
232 if (!(object->matbits && object->matbits[object->actcol - 1])) {
233 context_path_add_object_data(path, *object);
234 }
235 Material *material = BKE_object_material_get(object, object->actcol);
237 snode, path, *RNA_Material, material, snode.nodetree);
238 }
239 else if (snode.shaderfrom == SNODE_SHADER_WORLD) {
240 Scene *scene = CTX_data_scene(&C);
241 ui::context_path_add_generic(path, *RNA_Scene, scene);
242 if (scene != nullptr) {
244 snode, path, *RNA_World, scene->world, snode.nodetree);
245 }
246 }
247#ifdef WITH_FREESTYLE
248 else if (snode.shaderfrom == SNODE_SHADER_LINESTYLE) {
249 ViewLayer *viewlayer = CTX_data_view_layer(&C);
251 ui::context_path_add_generic(path, *RNA_ViewLayer, viewlayer);
252 Material *mat = BKE_object_material_get(object, object->actcol);
253 ui::context_path_add_generic(path, *RNA_Material, mat);
254 }
255#endif
257 }
258}
259
261 SpaceNode &snode,
263{
264 if (snode.flag & SNODE_PIN) {
266 return;
267 }
268
270 bool skip_base = false;
271 Scene *sequencer_scene = CTX_data_sequencer_scene(&C);
272 if (sequencer_scene) {
273 ui::context_path_add_generic(path, *RNA_Scene, sequencer_scene, ICON_SCENE);
274 Strip *strip = seq::select_active_get(sequencer_scene);
275 if (strip) {
276 ui::context_path_add_generic(path, *RNA_Strip, strip, ICON_SEQ_STRIP);
277 bNodeTree *node_group = nullptr;
278 if (strip->type == STRIP_TYPE_COMPOSITOR && strip->effectdata) {
279 CompositorEffectVars *comp_data = static_cast<CompositorEffectVars *>(strip->effectdata);
280 node_group = comp_data->node_group;
281 }
282 else {
284 if (smd && smd->type == eSeqModifierType_Compositor) {
286 reinterpret_cast<SequencerCompositorModifierData *>(smd);
287 node_group = scmd->node_group;
288 }
289 }
290
291 if (node_group != nullptr) {
293 snode, path, *RNA_NodeTree, node_group, node_group);
294 skip_base = true;
295 }
296 }
297 }
298 context_path_add_node_tree_and_node_groups(snode, path, skip_base);
299 return;
300 }
301
302 Scene *scene = CTX_data_scene(&C);
303 ui::context_path_add_generic(path, *RNA_Scene, scene);
305 if (!effect) {
307 return;
308 }
309
310 ui::context_path_add_generic(path, *RNA_SceneCompositorEffect, effect, ICON_NODE_COMPOSITING);
312}
313
315 SpaceNode &snode,
317{
318 if (snode.flag & SNODE_PIN || snode.node_tree_sub_type == SNODE_GEOMETRY_TOOL) {
320 }
321 else {
322 Object *object = CTX_data_active_object(&C);
323 if (!object) {
325 return;
326 }
327 ui::context_path_add_generic(path, *RNA_Object, object);
329 if (!modifier) {
331 return;
332 }
333 ui::context_path_add_generic(path, *RNA_Modifier, modifier, ICON_GEOMETRY_NODES);
335 }
336}
337
339{
340 SpaceNode *snode = CTX_wm_space_node(&C);
341 if (snode == nullptr) {
342 return {};
343 }
344
345 Vector<ui::ContextPathItem> context_path;
346
347 if (ED_node_is_geometry(snode)) {
348 get_context_path_node_geometry(C, *snode, context_path);
349 }
350 else if (ED_node_is_shader(snode)) {
351 get_context_path_node_shader(C, *snode, context_path);
352 }
353 else if (ED_node_is_compositor(snode)) {
354 get_context_path_node_compositor(C, *snode, context_path);
355 }
356
357 return context_path;
358}
359
360} // namespace ed::space_node
361
362} // namespace blender
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_ASSET(_id)
Definition DNA_ID.h:743
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define C
Definition RandGen.cpp:29
#define ELEM(...)
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
SceneCompositorEffect * get_active_effect(const Scene &scene)
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
static std::function< void(bContext &)> tree_path_handle_func(int i)
static void get_context_path_node_shader(const bContext &C, SpaceNode &snode, Vector< ui::ContextPathItem > &path)
static void get_context_path_node_geometry(const bContext &C, SpaceNode &snode, Vector< ui::ContextPathItem > &path)
static void context_path_add_object_data(Vector< ui::ContextPathItem > &path, Object &object)
static void navigate_menu_create(bContext *C, ui::Layout *layout, void *arg)
static bool node_group_is_enterable(const bNode &node)
static void get_context_path_node_compositor(const bContext &C, SpaceNode &snode, Vector< ui::ContextPathItem > &path)
static bool node_tree_has_enterable_group(const bNodeTree *ntree)
Vector< ui::ContextPathItem > context_path_for_space_node(const bContext &C)
static void context_path_add_top_level_shader_node_tree(const SpaceNode &snode, Vector< ui::ContextPathItem > &path, StructRNA &rna_type, void *ptr, bNodeTree *tree)
static BIFIconID node_tree_icon(const ID &tree_id)
static void context_path_add_node_tree_and_node_groups(const SpaceNode &snode, Vector< ui::ContextPathItem > &path, const bool skip_base=false)
Strip * select_active_get(const Scene *scene)
StripModifierData * modifier_get_active(const Strip *strip)
void button_flag_enable(Button *but, int64_t flag)
void context_path_add_generic(Vector< ContextPathItem > &path, StructRNA &rna_type, void *ptr, const BIFIconID icon_override=ICON_NONE, std::function< void(bContext &)> handle_func=nullptr, MenuCreateFunc menu_func=nullptr, void *menu_arg=nullptr)
BMesh Iterators.
bool ED_node_is_compositor(const SpaceNode *snode)
Definition node_edit.cc:135
void ED_node_tree_push(ARegion *region, SpaceNode *snode, bNodeTree *ntree, bNode *gnode)
@ SNODE_SHADER_LINESTYLE
bool ED_node_is_geometry(const SpaceNode *snode)
Definition node_edit.cc:150
Scene * CTX_data_sequencer_scene(const bContext *C)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void WM_operator_properties_free(PointerRNA *ptr)
void ED_node_tree_pop(ARegion *region, SpaceNode *snode)
Material * BKE_object_material_get(Object *ob, short act)
bool ED_node_is_shader(SpaceNode *snode)
Definition node_edit.cc:140
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
const char * BKE_id_name(const ID &id)
ViewLayer * CTX_data_view_layer(const bContext *C)
ModifierData * BKE_object_active_modifier(const Object *ob)
Definition object.cc:1427
@ SNODE_COMPOSITOR_SEQUENCER
BMesh const char void * data
SpaceNode * CTX_wm_space_node(const bContext *C)
int BIFIconID
Definition ED_asset.hh:30
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
FreestyleLineStyle * BKE_linestyle_active_from_view_layer(struct ViewLayer *view_layer)
Definition linestyle.cc:773
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
Scene * CTX_data_scene(const bContext *C)
@ eSeqModifierType_Compositor
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
struct World * world
eSpaceNode_ShaderFrom shaderfrom
struct bNodeTree * edittree
struct bNodeTree * nodetree
ListBaseT< bNodeTreePath > treepath
eSpaceNode_Flag flag
struct bNodeTreePath * next
struct bNodeTree * nodetree
Button * button(StringRef name, int icon, std::function< void(bContext &)> func, std::optional< StringRef > tooltip=std::nullopt)