Files
test2/source/blender/editors/space_node/node_templates.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1044 lines
31 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup edinterface
*/
#include <cstdlib>
#include <cstring>
#include <optional>
#include "MEM_guardedalloc.h"
#include "DNA_node_types.h"
#include "DNA_screen_types.h"
#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_vector.hh"
#include "BLT_translation.h"
#include "BKE_context.hh"
2024-01-15 12:44:04 -05:00
#include "BKE_lib_id.hh"
#include "BKE_main.hh"
Nodes: Panels integration with blend files and UI Part 3/3 of #109135, #110272 Switch to new node group interfaces and deprecate old DNA and API. This completes support for panels in node drawing and in node group interface declarations in particular. The new node group interface DNA and RNA code has been added in parts 1 and 2 (#110885, #110952) but has not be enabled yet. This commit completes the integration by * enabling the new RNA API * using the new API in UI * read/write new interfaces from blend files * add versioning for backward compatibility * add forward-compatible writing code to reconstruct old interfaces All places accessing node group interface declarations should now be using the new API. A runtime cache has been added that allows simple linear access to socket inputs and outputs even when a panel hierarchy is used. Old DNA has been deprecated and should only be accessed for versioning (inputs/outputs renamed to inputs_legacy/outputs_legacy to catch errors). Versioning code ensures both backward and forward compatibility of existing files. The API for old interfaces is removed. The new API is very similar but is defined on the `ntree.interface` instead of the `ntree` directly. Breaking change notifications and detailed instructions for migrating will be added. A python test has been added for the node group API functions. This includes new functionality such as creating panels and moving items between different levels. This patch does not yet contain panel representations in the modifier UI. This has been tested in a separate branch and will be added with a later PR (#108565). Pull Request: https://projects.blender.org/blender/blender/pulls/111348
2023-08-30 12:37:21 +02:00
#include "BKE_node_runtime.hh"
#include "BKE_node_tree_interface.hh"
#include "BKE_node_tree_update.hh"
#include "RNA_access.hh"
#include "RNA_prototypes.h"
#include "NOD_node_declaration.hh"
#include "NOD_socket.hh"
#include "NOD_socket_declarations.hh"
#include "../interface/interface_intern.hh" /* XXX bad level */
#include "UI_interface.hh"
2012-08-02 23:03:16 +00:00
#include "ED_node.hh" /* own include */
#include "node_intern.hh"
#include "ED_undo.hh"
#include "WM_api.hh"
using blender::nodes::NodeDeclaration;
namespace blender::ed::space_node {
/************************* Node Socket Manipulation **************************/
/* describes an instance of a node type and a specific socket to link */
struct NodeLinkItem {
int socket_index = -1; /* index for linking */
int socket_type = SOCK_CUSTOM; /* socket type for compatibility check */
const char *socket_name = nullptr; /* ui label of the socket */
const char *node_name = nullptr; /* ui label of the node */
/* extra settings */
bNodeTree *ngroup = nullptr; /* group node tree */
};
/* Compare an existing node to a link item to see if it can be reused.
* item must be for the same node type!
* XXX should become a node type callback
*/
static bool node_link_item_compare(bNode *node, NodeLinkItem *item)
{
if (ELEM(node->type, NODE_GROUP, NODE_CUSTOM_GROUP)) {
return (node->id == (ID *)item->ngroup);
}
return true;
}
static void node_link_item_apply(bNodeTree *ntree, bNode *node, NodeLinkItem *item)
{
if (ELEM(node->type, NODE_GROUP, NODE_CUSTOM_GROUP)) {
node->id = (ID *)item->ngroup;
BKE_ntree_update_tag_node_property(ntree, node);
}
else {
/* nothing to do for now */
}
if (node->id) {
id_us_plus(node->id);
}
}
static void node_tag_recursive(bNode *node)
{
if (!node || (node->flag & NODE_TEST)) {
2012-07-01 09:54:44 +00:00
return; /* in case of cycles */
}
2012-07-01 09:54:44 +00:00
node->flag |= NODE_TEST;
LISTBASE_FOREACH (bNodeSocket *, input, &node->inputs) {
if (input->link) {
node_tag_recursive(input->link->fromnode);
}
}
}
static void node_clear_recursive(bNode *node)
{
if (!node || !(node->flag & NODE_TEST)) {
2012-07-01 09:54:44 +00:00
return; /* in case of cycles */
}
2012-07-01 09:54:44 +00:00
node->flag &= ~NODE_TEST;
LISTBASE_FOREACH (bNodeSocket *, input, &node->inputs) {
if (input->link) {
node_clear_recursive(input->link->fromnode);
}
}
}
static void node_remove_linked(Main *bmain, bNodeTree *ntree, bNode *rem_node)
{
bNode *node, *next;
if (!rem_node) {
return;
}
/* tag linked nodes to be removed */
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
node->flag &= ~NODE_TEST;
}
node_tag_recursive(rem_node);
/* clear tags on nodes that are still used by other nodes */
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
if (!(node->flag & NODE_TEST)) {
LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
if (sock->link && sock->link->fromnode != rem_node) {
node_clear_recursive(sock->link->fromnode);
}
}
}
}
/* remove nodes */
for (node = (bNode *)ntree->nodes.first; node; node = next) {
next = node->next;
if (node->flag & NODE_TEST) {
nodeRemoveNode(bmain, ntree, node, true);
}
}
}
/* disconnect socket from the node it is connected to */
static void node_socket_disconnect(Main *bmain,
bNodeTree *ntree,
bNode *node_to,
bNodeSocket *sock_to)
{
if (!sock_to->link) {
return;
}
nodeRemLink(ntree, sock_to->link);
sock_to->flag |= SOCK_COLLAPSED;
Nodes: refactor node tree update handling Goals of this refactor: * More unified approach to updating everything that needs to be updated after a change in a node tree. * The updates should happen in the correct order and quadratic or worse algorithms should be avoided. * Improve detection of changes to the output to avoid tagging the depsgraph when it's not necessary. * Move towards a more declarative style of defining nodes by having a more centralized update procedure. The refactor consists of two main parts: * Node tree tagging and update refactor. * Generally, when changes are done to a node tree, it is tagged dirty until a global update function is called that updates everything in the correct order. * The tagging is more fine-grained compared to before, to allow for more precise depsgraph update tagging. * Depsgraph changes. * The shading specific depsgraph node for node trees as been removed. * Instead, there is a new `NTREE_OUTPUT` depsgrap node, which is only tagged when the output of the node tree changed (e.g. the Group Output or Material Output node). * The copy-on-write relation from node trees to the data block they are embedded in is now non-flushing. This avoids e.g. triggering a material update after the shader node tree changed in unrelated ways. Instead the material has a flushing relation to the new `NTREE_OUTPUT` node now. * The depsgraph no longer reports data block changes through to cycles through `Depsgraph.updates` when only the node tree changed in ways that do not affect the output. Avoiding unnecessary updates seems to work well for geometry nodes and cycles. The situation is a bit worse when there are drivers on the node tree, but that could potentially be improved separately in the future. Avoiding updates in eevee and the compositor is more tricky, but also less urgent. * Eevee updates are triggered by calling `DRW_notify_view_update` in `ED_render_view3d_update` indirectly from `DEG_editors_update`. * Compositor updates are triggered by `ED_node_composite_job` in `node_area_refresh`. This is triggered by calling `ED_area_tag_refresh` in `node_area_listener`. Removing updates always has the risk of breaking some dependency that no one was aware of. It's not unlikely that this will happen here as well. Adding back missing updates should be quite a bit easier than getting rid of unnecessary updates though. Differential Revision: https://developer.blender.org/D13246
2021-12-21 15:18:56 +01:00
BKE_ntree_update_tag_node_property(ntree, node_to);
ED_node_tree_propagate_change(nullptr, bmain, ntree);
}
/* remove all nodes connected to this socket, if they aren't connected to other nodes */
static void node_socket_remove(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to)
{
if (!sock_to->link) {
return;
}
node_remove_linked(bmain, ntree, sock_to->link->fromnode);
sock_to->flag |= SOCK_COLLAPSED;
Nodes: refactor node tree update handling Goals of this refactor: * More unified approach to updating everything that needs to be updated after a change in a node tree. * The updates should happen in the correct order and quadratic or worse algorithms should be avoided. * Improve detection of changes to the output to avoid tagging the depsgraph when it's not necessary. * Move towards a more declarative style of defining nodes by having a more centralized update procedure. The refactor consists of two main parts: * Node tree tagging and update refactor. * Generally, when changes are done to a node tree, it is tagged dirty until a global update function is called that updates everything in the correct order. * The tagging is more fine-grained compared to before, to allow for more precise depsgraph update tagging. * Depsgraph changes. * The shading specific depsgraph node for node trees as been removed. * Instead, there is a new `NTREE_OUTPUT` depsgrap node, which is only tagged when the output of the node tree changed (e.g. the Group Output or Material Output node). * The copy-on-write relation from node trees to the data block they are embedded in is now non-flushing. This avoids e.g. triggering a material update after the shader node tree changed in unrelated ways. Instead the material has a flushing relation to the new `NTREE_OUTPUT` node now. * The depsgraph no longer reports data block changes through to cycles through `Depsgraph.updates` when only the node tree changed in ways that do not affect the output. Avoiding unnecessary updates seems to work well for geometry nodes and cycles. The situation is a bit worse when there are drivers on the node tree, but that could potentially be improved separately in the future. Avoiding updates in eevee and the compositor is more tricky, but also less urgent. * Eevee updates are triggered by calling `DRW_notify_view_update` in `ED_render_view3d_update` indirectly from `DEG_editors_update`. * Compositor updates are triggered by `ED_node_composite_job` in `node_area_refresh`. This is triggered by calling `ED_area_tag_refresh` in `node_area_listener`. Removing updates always has the risk of breaking some dependency that no one was aware of. It's not unlikely that this will happen here as well. Adding back missing updates should be quite a bit easier than getting rid of unnecessary updates though. Differential Revision: https://developer.blender.org/D13246
2021-12-21 15:18:56 +01:00
BKE_ntree_update_tag_node_property(ntree, node_to);
ED_node_tree_propagate_change(nullptr, bmain, ntree);
}
/* add new node connected to this socket, or replace an existing one */
static void node_socket_add_replace(const bContext *C,
bNodeTree *ntree,
bNode *node_to,
bNodeSocket *sock_to,
int type,
NodeLinkItem *item)
{
2018-06-09 15:16:44 +02:00
Main *bmain = CTX_data_main(C);
bNode *node_from;
2012-10-12 14:35:10 +00:00
bNodeSocket *sock_from_tmp;
bNode *node_prev = nullptr;
/* unlink existing node */
if (sock_to->link) {
node_prev = sock_to->link->fromnode;
nodeRemLink(ntree, sock_to->link);
}
/* find existing node that we can use */
for (node_from = (bNode *)ntree->nodes.first; node_from; node_from = node_from->next) {
if (node_from->type == type) {
break;
}
}
if (node_from) {
if (node_from->inputs.first || node_from->typeinfo->draw_buttons ||
node_from->typeinfo->draw_buttons_ex)
{
node_from = nullptr;
}
}
if (node_prev && node_prev->type == type && node_link_item_compare(node_prev, item)) {
/* keep the previous node if it's the same type */
node_from = node_prev;
}
else if (!node_from) {
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
node_from = nodeAddStaticNode(C, ntree, type);
if (node_prev != nullptr) {
/* If we're replacing existing node, use its location. */
node_from->locx = node_prev->locx;
node_from->locy = node_prev->locy;
node_from->offsetx = node_prev->offsetx;
node_from->offsety = node_prev->offsety;
}
else {
sock_from_tmp = (bNodeSocket *)BLI_findlink(&node_from->outputs, item->socket_index);
bke::nodePositionRelative(node_from, node_to, sock_from_tmp, sock_to);
}
node_link_item_apply(ntree, node_from, item);
ED_node_tree_propagate_change(C, bmain, ntree);
}
nodeSetActive(ntree, node_from);
/* add link */
sock_from_tmp = (bNodeSocket *)BLI_findlink(&node_from->outputs, item->socket_index);
2012-10-12 14:35:10 +00:00
nodeAddLink(ntree, node_from, sock_from_tmp, node_to, sock_to);
sock_to->flag &= ~SOCK_COLLAPSED;
/* copy input sockets from previous node */
if (node_prev && node_from != node_prev) {
LISTBASE_FOREACH (bNodeSocket *, sock_prev, &node_prev->inputs) {
LISTBASE_FOREACH (bNodeSocket *, sock_from, &node_from->inputs) {
Nodes: Support storing socket link limits in bNodeSocketType Currently the link limit of sockets is stored in bNodeSocket->limit. This allows for a lot of flexibility, but is also very redundant. In every case I've had to deal with so far, it would have "more correct" to set the link limit per socket type and not per socket. I did not enforce this constraint yet, because the link limit is exposed in the Python API, which I did not want to break here. In the future it might even make sense to only support only three kinds of link limits: a) no links, b) at most one link, c) an arbitrary number links links. The other link limits usually don't work well with tools (e.g. which link should be removed when a new one is connected?) and is not used in practice. However, that is for another day. Eventually, I would like to get rid of bNodeSocket->limit completely and replace it either with fixed link limits or a callback in bNodeSocketType. This patch consists of three parts: **1. Support defining link limit in socket type** This introduces a new `nodeSocketLinkLimit` function that serves as an indirection to hide where the link limit of a socket is defined. **2. Define link limits for builtin sockets on socket type** Data sockets: one input, many outputs Virtual sockets: one input, one output Undefined sockets: many inputs, many outputs (to avoid that links are removed when the type of the socket is not known) **3. Remove `bNodeSocketTemplate->limit`** This wasn't used anymore after the second commit. Removing it simplifies socket definitions in hundreds of places and removes a lot of redundancy. Differential Revision: https://developer.blender.org/D7038 Reviewers: brecht
2020-03-06 12:20:05 +01:00
if (nodeCountSocketLinks(ntree, sock_from) >= nodeSocketLinkLimit(sock_from)) {
continue;
}
if (STREQ(sock_prev->identifier, sock_from->identifier) &&
sock_prev->type == sock_from->type)
{
bNodeLink *link = sock_prev->link;
if (link && link->fromnode) {
nodeAddLink(ntree, link->fromnode, link->fromsock, node_from, sock_from);
nodeRemLink(ntree, link);
}
node_socket_copy_default_value(sock_from, sock_prev);
}
}
}
/* also preserve mapping for texture nodes */
if (node_from->typeinfo->nclass == NODE_CLASS_TEXTURE &&
node_prev->typeinfo->nclass == NODE_CLASS_TEXTURE &&
/* White noise texture node does not have NodeTexBase. */
node_from->storage != nullptr && node_prev->storage != nullptr)
{
memcpy(node_from->storage, node_prev->storage, sizeof(NodeTexBase));
}
/* remove node */
node_remove_linked(bmain, ntree, node_prev);
}
Nodes: refactor node tree update handling Goals of this refactor: * More unified approach to updating everything that needs to be updated after a change in a node tree. * The updates should happen in the correct order and quadratic or worse algorithms should be avoided. * Improve detection of changes to the output to avoid tagging the depsgraph when it's not necessary. * Move towards a more declarative style of defining nodes by having a more centralized update procedure. The refactor consists of two main parts: * Node tree tagging and update refactor. * Generally, when changes are done to a node tree, it is tagged dirty until a global update function is called that updates everything in the correct order. * The tagging is more fine-grained compared to before, to allow for more precise depsgraph update tagging. * Depsgraph changes. * The shading specific depsgraph node for node trees as been removed. * Instead, there is a new `NTREE_OUTPUT` depsgrap node, which is only tagged when the output of the node tree changed (e.g. the Group Output or Material Output node). * The copy-on-write relation from node trees to the data block they are embedded in is now non-flushing. This avoids e.g. triggering a material update after the shader node tree changed in unrelated ways. Instead the material has a flushing relation to the new `NTREE_OUTPUT` node now. * The depsgraph no longer reports data block changes through to cycles through `Depsgraph.updates` when only the node tree changed in ways that do not affect the output. Avoiding unnecessary updates seems to work well for geometry nodes and cycles. The situation is a bit worse when there are drivers on the node tree, but that could potentially be improved separately in the future. Avoiding updates in eevee and the compositor is more tricky, but also less urgent. * Eevee updates are triggered by calling `DRW_notify_view_update` in `ED_render_view3d_update` indirectly from `DEG_editors_update`. * Compositor updates are triggered by `ED_node_composite_job` in `node_area_refresh`. This is triggered by calling `ED_area_tag_refresh` in `node_area_listener`. Removing updates always has the risk of breaking some dependency that no one was aware of. It's not unlikely that this will happen here as well. Adding back missing updates should be quite a bit easier than getting rid of unnecessary updates though. Differential Revision: https://developer.blender.org/D13246
2021-12-21 15:18:56 +01:00
BKE_ntree_update_tag_node_property(ntree, node_from);
BKE_ntree_update_tag_node_property(ntree, node_to);
ED_node_tree_propagate_change(nullptr, bmain, ntree);
}
/****************************** Node Link Menu *******************************/
// #define UI_NODE_LINK_ADD 0
2012-07-01 09:54:44 +00:00
#define UI_NODE_LINK_DISCONNECT -1
#define UI_NODE_LINK_REMOVE -2
struct NodeLinkArg {
Main *bmain;
Scene *scene;
bNodeTree *ntree;
bNode *node;
bNodeSocket *sock;
bNodeType *node_type;
NodeLinkItem item;
uiLayout *layout;
};
static Vector<NodeLinkItem> ui_node_link_items(NodeLinkArg *arg,
int in_out,
std::optional<NodeDeclaration> &r_node_decl)
{
Vector<NodeLinkItem> items;
/* XXX this should become a callback for node types! */
if (arg->node_type->type == NODE_GROUP) {
bNodeTree *ngroup;
for (ngroup = (bNodeTree *)arg->bmain->nodetrees.first; ngroup;
ngroup = (bNodeTree *)ngroup->id.next)
{
const char *disabled_hint;
if ((ngroup->type != arg->ntree->type) || !nodeGroupPoll(arg->ntree, ngroup, &disabled_hint))
{
continue;
}
}
for (ngroup = (bNodeTree *)arg->bmain->nodetrees.first; ngroup;
ngroup = (bNodeTree *)ngroup->id.next)
{
const char *disabled_hint;
if ((ngroup->type != arg->ntree->type) || !nodeGroupPoll(arg->ntree, ngroup, &disabled_hint))
{
continue;
}
ngroup->ensure_interface_cache();
Nodes: Panels integration with blend files and UI Part 3/3 of #109135, #110272 Switch to new node group interfaces and deprecate old DNA and API. This completes support for panels in node drawing and in node group interface declarations in particular. The new node group interface DNA and RNA code has been added in parts 1 and 2 (#110885, #110952) but has not be enabled yet. This commit completes the integration by * enabling the new RNA API * using the new API in UI * read/write new interfaces from blend files * add versioning for backward compatibility * add forward-compatible writing code to reconstruct old interfaces All places accessing node group interface declarations should now be using the new API. A runtime cache has been added that allows simple linear access to socket inputs and outputs even when a panel hierarchy is used. Old DNA has been deprecated and should only be accessed for versioning (inputs/outputs renamed to inputs_legacy/outputs_legacy to catch errors). Versioning code ensures both backward and forward compatibility of existing files. The API for old interfaces is removed. The new API is very similar but is defined on the `ntree.interface` instead of the `ntree` directly. Breaking change notifications and detailed instructions for migrating will be added. A python test has been added for the node group API functions. This includes new functionality such as creating panels and moving items between different levels. This patch does not yet contain panel representations in the modifier UI. This has been tested in a separate branch and will be added with a later PR (#108565). Pull Request: https://projects.blender.org/blender/blender/pulls/111348
2023-08-30 12:37:21 +02:00
Span<bNodeTreeInterfaceSocket *> iosockets = (in_out == SOCK_IN ?
ngroup->interface_inputs() :
ngroup->interface_outputs());
for (const int index : iosockets.index_range()) {
bNodeTreeInterfaceSocket *iosock = iosockets[index];
NodeLinkItem item;
item.socket_index = index;
/* NOTE: int stemp->type is not fully reliable, not used for node group
* interface sockets. use the typeinfo->type instead.
*/
Nodes: Panels integration with blend files and UI Part 3/3 of #109135, #110272 Switch to new node group interfaces and deprecate old DNA and API. This completes support for panels in node drawing and in node group interface declarations in particular. The new node group interface DNA and RNA code has been added in parts 1 and 2 (#110885, #110952) but has not be enabled yet. This commit completes the integration by * enabling the new RNA API * using the new API in UI * read/write new interfaces from blend files * add versioning for backward compatibility * add forward-compatible writing code to reconstruct old interfaces All places accessing node group interface declarations should now be using the new API. A runtime cache has been added that allows simple linear access to socket inputs and outputs even when a panel hierarchy is used. Old DNA has been deprecated and should only be accessed for versioning (inputs/outputs renamed to inputs_legacy/outputs_legacy to catch errors). Versioning code ensures both backward and forward compatibility of existing files. The API for old interfaces is removed. The new API is very similar but is defined on the `ntree.interface` instead of the `ntree` directly. Breaking change notifications and detailed instructions for migrating will be added. A python test has been added for the node group API functions. This includes new functionality such as creating panels and moving items between different levels. This patch does not yet contain panel representations in the modifier UI. This has been tested in a separate branch and will be added with a later PR (#108565). Pull Request: https://projects.blender.org/blender/blender/pulls/111348
2023-08-30 12:37:21 +02:00
const bNodeSocketType *typeinfo = iosock->socket_typeinfo();
item.socket_type = typeinfo->type;
item.socket_name = iosock->name;
item.node_name = ngroup->id.name + 2;
item.ngroup = ngroup;
items.append(item);
}
}
}
else if (arg->node_type->declare != nullptr) {
using namespace blender;
using namespace blender::nodes;
r_node_decl.emplace(NodeDeclaration());
Nodes: unify static and dynamic declarations This helps solving the problem encountered in #113553. The problem is that we currently can't support link-drag-search for nodes which have a dynamic declaration. With this patch, there is only a single `declare` function per node type, instead of the separate `declare` and `declare_dynamic` functions. The new `declare` function has access to the node and tree. However, both are allowed to be null. The final node declaration has a flag for whether it depends on the node context or not. Nodes that previously had a dynamic declaration should now create as much of the declaration as possible that does not depend on the node. This allows code like for link-drag-search to take those sockets into account even if the other sockets are dynamic. For node declarations that have dynamic types (e.g. Switch node), we can also add extra information to the static node declaration, like the identifier of the socket with the dynamic type. This is not part of this patch though. I can think of two main alternatives to the approach implemented here: * Define two separate functions for dynamic nodes. One that creates the "static declaration" without node context, and on that creates the actual declaration with node context. * Have a single declare function that generates "build instructions" for the actual node declaration. So instead of building the final declaration directly, one can for example add a socket whose type depends on a specific rna path in the node. The actual node declaration is then automatically generated based on the build instructions. This becomes quite a bit more tricky with dynamic amounts of sockets and introduces another indirection between declarations and what sockets the node actually has. I found the approach implemented in this patch to lead to the least amount of boilerplate (doesn't require a seperate "build instructions" data structure) and code duplication (socket properties are still only defined in one place). At the same time, it offers more flexibility to how nodes can be dynamic. Pull Request: https://projects.blender.org/blender/blender/pulls/113742
2023-10-15 20:28:23 +02:00
blender::nodes::build_node_declaration(*arg->node_type, *r_node_decl, nullptr, nullptr);
Nodes: Panels integration with blend files and UI Part 3/3 of #109135, #110272 Switch to new node group interfaces and deprecate old DNA and API. This completes support for panels in node drawing and in node group interface declarations in particular. The new node group interface DNA and RNA code has been added in parts 1 and 2 (#110885, #110952) but has not be enabled yet. This commit completes the integration by * enabling the new RNA API * using the new API in UI * read/write new interfaces from blend files * add versioning for backward compatibility * add forward-compatible writing code to reconstruct old interfaces All places accessing node group interface declarations should now be using the new API. A runtime cache has been added that allows simple linear access to socket inputs and outputs even when a panel hierarchy is used. Old DNA has been deprecated and should only be accessed for versioning (inputs/outputs renamed to inputs_legacy/outputs_legacy to catch errors). Versioning code ensures both backward and forward compatibility of existing files. The API for old interfaces is removed. The new API is very similar but is defined on the `ntree.interface` instead of the `ntree` directly. Breaking change notifications and detailed instructions for migrating will be added. A python test has been added for the node group API functions. This includes new functionality such as creating panels and moving items between different levels. This patch does not yet contain panel representations in the modifier UI. This has been tested in a separate branch and will be added with a later PR (#108565). Pull Request: https://projects.blender.org/blender/blender/pulls/111348
2023-08-30 12:37:21 +02:00
Span<SocketDeclaration *> socket_decls = (in_out == SOCK_IN) ? r_node_decl->inputs :
r_node_decl->outputs;
int index = 0;
Nodes: Panels integration with blend files and UI Part 3/3 of #109135, #110272 Switch to new node group interfaces and deprecate old DNA and API. This completes support for panels in node drawing and in node group interface declarations in particular. The new node group interface DNA and RNA code has been added in parts 1 and 2 (#110885, #110952) but has not be enabled yet. This commit completes the integration by * enabling the new RNA API * using the new API in UI * read/write new interfaces from blend files * add versioning for backward compatibility * add forward-compatible writing code to reconstruct old interfaces All places accessing node group interface declarations should now be using the new API. A runtime cache has been added that allows simple linear access to socket inputs and outputs even when a panel hierarchy is used. Old DNA has been deprecated and should only be accessed for versioning (inputs/outputs renamed to inputs_legacy/outputs_legacy to catch errors). Versioning code ensures both backward and forward compatibility of existing files. The API for old interfaces is removed. The new API is very similar but is defined on the `ntree.interface` instead of the `ntree` directly. Breaking change notifications and detailed instructions for migrating will be added. A python test has been added for the node group API functions. This includes new functionality such as creating panels and moving items between different levels. This patch does not yet contain panel representations in the modifier UI. This has been tested in a separate branch and will be added with a later PR (#108565). Pull Request: https://projects.blender.org/blender/blender/pulls/111348
2023-08-30 12:37:21 +02:00
for (const SocketDeclaration *socket_decl_ptr : socket_decls) {
const SocketDeclaration &socket_decl = *socket_decl_ptr;
NodeLinkItem item;
item.socket_index = index++;
if (dynamic_cast<const decl::Float *>(&socket_decl)) {
item.socket_type = SOCK_FLOAT;
}
else if (dynamic_cast<const decl::Int *>(&socket_decl)) {
item.socket_type = SOCK_INT;
}
else if (dynamic_cast<const decl::Bool *>(&socket_decl)) {
item.socket_type = SOCK_BOOLEAN;
}
else if (dynamic_cast<const decl::Vector *>(&socket_decl)) {
item.socket_type = SOCK_VECTOR;
}
else if (dynamic_cast<const decl::Color *>(&socket_decl)) {
item.socket_type = SOCK_RGBA;
}
else if (dynamic_cast<const decl::Rotation *>(&socket_decl)) {
item.socket_type = SOCK_ROTATION;
}
else if (dynamic_cast<const decl::String *>(&socket_decl)) {
item.socket_type = SOCK_STRING;
}
Geometry Nodes: Menu Switch Node This patch adds support for _Menu Switch_ nodes and enum definitions in node trees more generally. The design is based on the outcome of the [2022 Nodes Workshop](https://code.blender.org/2022/11/geometry-nodes-workshop-2022/#menu-switch). The _Menu Switch_ node is an advanced version of the _Switch_ node which has a customizable **menu input socket** instead of a simple boolean. The _items_ of this menu are owned by the node itself. Each item has a name and description and unique identifier that is used internally. A menu _socket_ represents a concrete value out of the list of items. To enable selection of an enum value for unconnected sockets the menu is presented as a dropdown list like built-in enums. When the socket is connected a shared pointer to the enum definition is propagated along links and stored in socket default values. This allows node groups to expose a menu from an internal menu switch as a parameter. The enum definition is a runtime copy of the enum items in DNA that allows sharing. A menu socket can have multiple connections, which can lead to ambiguity. If two or more different menu source nodes are connected to a socket it gets marked as _undefined_. Any connection to an undefined menu socket is invalid as a hint to users that there is a problem. A warning/error is also shown on nodes with undefined menu sockets. At runtime the value of a menu socket is the simple integer identifier. This can also be a field in geometry nodes. The identifier is unique within each enum definition, and it is persistent even when items are added, removed, or changed. Changing the name of an item does not affect the internal identifier, so users can rename enum items without breaking existing input values. This also persists if, for example, a linked node group is temporarily unavailable. Pull Request: https://projects.blender.org/blender/blender/pulls/113445
2024-01-26 12:40:01 +01:00
else if (dynamic_cast<const decl::Menu *>(&socket_decl)) {
item.socket_type = SOCK_MENU;
}
else if (dynamic_cast<const decl::Image *>(&socket_decl)) {
item.socket_type = SOCK_IMAGE;
}
else if (dynamic_cast<const decl::Texture *>(&socket_decl)) {
item.socket_type = SOCK_TEXTURE;
}
else if (dynamic_cast<const decl::Material *>(&socket_decl)) {
item.socket_type = SOCK_MATERIAL;
}
else if (dynamic_cast<const decl::Shader *>(&socket_decl)) {
item.socket_type = SOCK_SHADER;
}
else if (dynamic_cast<const decl::Collection *>(&socket_decl)) {
item.socket_type = SOCK_COLLECTION;
}
else if (dynamic_cast<const decl::Object *>(&socket_decl)) {
item.socket_type = SOCK_OBJECT;
}
else {
item.socket_type = SOCK_CUSTOM;
}
item.socket_name = socket_decl.name.c_str();
item.node_name = arg->node_type->ui_name;
items.append(item);
}
}
else {
bNodeSocketTemplate *socket_templates = (in_out == SOCK_IN ? arg->node_type->inputs :
arg->node_type->outputs);
bNodeSocketTemplate *stemp;
int i;
i = 0;
for (stemp = socket_templates; stemp && stemp->type != -1; stemp++, i++) {
NodeLinkItem item;
item.socket_index = i;
item.socket_type = stemp->type;
item.socket_name = stemp->name;
item.node_name = arg->node_type->ui_name;
items.append(item);
}
}
return items;
}
static void ui_node_link(bContext *C, void *arg_p, void *event_p)
{
2012-07-01 09:54:44 +00:00
NodeLinkArg *arg = (NodeLinkArg *)arg_p;
Main *bmain = arg->bmain;
bNode *node_to = arg->node;
bNodeSocket *sock_to = arg->sock;
bNodeTree *ntree = arg->ntree;
int event = POINTER_AS_INT(event_p);
if (event == UI_NODE_LINK_DISCONNECT) {
node_socket_disconnect(bmain, ntree, node_to, sock_to);
}
else if (event == UI_NODE_LINK_REMOVE) {
node_socket_remove(bmain, ntree, node_to, sock_to);
}
else {
node_socket_add_replace(C, ntree, node_to, sock_to, arg->node_type->type, &arg->item);
}
ED_undo_push(C, "Node input modify");
}
2021-12-13 16:22:21 +11:00
static void ui_node_sock_name(const bNodeTree *ntree,
bNodeSocket *sock,
char name[UI_MAX_NAME_STR])
{
if (sock->link && sock->link->fromnode) {
bNode *node = sock->link->fromnode;
char node_name[UI_MAX_NAME_STR];
bke::nodeLabel(ntree, node, node_name, sizeof(node_name));
if (BLI_listbase_is_empty(&node->inputs) && node->outputs.first != node->outputs.last) {
BLI_snprintf(
name, UI_MAX_NAME_STR, "%s | %s", IFACE_(node_name), IFACE_(sock->link->fromsock->name));
}
else {
BLI_strncpy_utf8(name, IFACE_(node_name), UI_MAX_NAME_STR);
}
}
else if (sock->type == SOCK_SHADER) {
BLI_strncpy_utf8(name, IFACE_("None"), UI_MAX_NAME_STR);
}
else {
BLI_strncpy_utf8(name, IFACE_("Default"), UI_MAX_NAME_STR);
}
}
static int ui_compatible_sockets(int typeA, int typeB)
{
return (typeA == typeB);
}
static int ui_node_item_name_compare(const void *a, const void *b)
{
2016-05-06 06:29:39 +10:00
const bNodeType *type_a = *(const bNodeType **)a;
const bNodeType *type_b = *(const bNodeType **)b;
return BLI_strcasecmp_natural(type_a->ui_name, type_b->ui_name);
}
static bool ui_node_item_special_poll(const bNodeTree * /*ntree*/, const bNodeType *ntype)
{
if (STREQ(ntype->idname, "ShaderNodeUVAlongStroke")) {
/* TODO(sergey): Currently we don't have Freestyle nodes edited from
* the buttons context, so can ignore its nodes completely.
*
* However, we might want to do some extra checks here later.
*/
return false;
}
return true;
}
static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
{
bNodeTree *ntree = arg->ntree;
bNodeSocket *sock = arg->sock;
uiLayout *layout = arg->layout;
uiLayout *column = nullptr;
uiBlock *block = uiLayoutGetBlock(layout);
uiBut *but;
NodeLinkArg *argN;
int first = 1;
/* generate array of node types sorted by UI name */
blender::Vector<bNodeType *> sorted_ntypes;
NODE_TYPES_BEGIN (ntype) {
const char *disabled_hint;
if (!(ntype->poll && ntype->poll(ntype, ntree, &disabled_hint))) {
continue;
}
if (ntype->nclass != nclass) {
continue;
}
if (!ui_node_item_special_poll(ntree, ntype)) {
continue;
}
sorted_ntypes.append(ntype);
}
NODE_TYPES_END;
qsort(
sorted_ntypes.data(), sorted_ntypes.size(), sizeof(bNodeType *), ui_node_item_name_compare);
/* generate UI */
for (int j = 0; j < sorted_ntypes.size(); j++) {
bNodeType *ntype = sorted_ntypes[j];
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
char name[UI_MAX_NAME_STR];
const char *cur_node_name = nullptr;
2020-09-09 18:41:07 +02:00
int num = 0;
int icon = ICON_NONE;
arg->node_type = ntype;
std::optional<blender::nodes::NodeDeclaration> node_decl;
Vector<NodeLinkItem> items = ui_node_link_items(arg, SOCK_OUT, node_decl);
for (const NodeLinkItem &item : items) {
if (ui_compatible_sockets(item.socket_type, sock->type)) {
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
num++;
}
}
for (const NodeLinkItem &item : items) {
if (!ui_compatible_sockets(item.socket_type, sock->type)) {
continue;
}
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
if (first) {
column = uiLayoutColumn(layout, false);
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
UI_block_layout_set_current(block, column);
uiItemL(column, IFACE_(cname), ICON_NODE);
but = (uiBut *)block->buttons.last;
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
first = 0;
}
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
if (num > 1) {
if (!cur_node_name || !STREQ(cur_node_name, item.node_name)) {
cur_node_name = item.node_name;
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
/* XXX Do not use uiItemL here,
* it would add an empty icon as we are in a menu! */
uiDefBut(block,
UI_BTYPE_LABEL,
0,
IFACE_(cur_node_name),
0,
0,
UI_UNIT_X * 4,
UI_UNIT_Y,
nullptr,
0.0,
0.0,
0.0,
0.0,
"");
}
2023-05-09 12:50:37 +10:00
SNPRINTF(name, "%s", IFACE_(item.socket_name));
icon = ICON_BLANK1;
}
else {
STRNCPY_UTF8(name, IFACE_(item.node_name));
icon = ICON_NONE;
}
but = uiDefIconTextBut(block,
UI_BTYPE_BUT,
0,
icon,
name,
0,
0,
UI_UNIT_X * 4,
UI_UNIT_Y,
nullptr,
0.0,
0.0,
0.0,
0.0,
TIP_("Add node to input"));
argN = (NodeLinkArg *)MEM_dupallocN(arg);
argN->item = item;
UI_but_funcN_set(but, ui_node_link, argN, nullptr);
}
}
}
static void node_menu_column_foreach_cb(void *calldata, int nclass, const char *name)
{
2012-07-01 09:54:44 +00:00
NodeLinkArg *arg = (NodeLinkArg *)calldata;
if (!ELEM(nclass, NODE_CLASS_GROUP, NODE_CLASS_LAYOUT)) {
ui_node_menu_column(arg, nclass, name);
}
}
static void ui_template_node_link_menu(bContext *C, uiLayout *layout, void *but_p)
{
2012-07-01 09:54:44 +00:00
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
uiBlock *block = uiLayoutGetBlock(layout);
2012-07-01 09:54:44 +00:00
uiBut *but = (uiBut *)but_p;
uiLayout *split, *column;
2012-07-01 09:54:44 +00:00
NodeLinkArg *arg = (NodeLinkArg *)but->func_argN;
bNodeSocket *sock = arg->sock;
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
bNodeTreeType *ntreetype = arg->ntree->typeinfo;
UI_block_layout_set_current(block, layout);
split = uiLayoutSplit(layout, 0.0f, false);
2012-07-01 09:54:44 +00:00
arg->bmain = bmain;
arg->scene = scene;
arg->layout = split;
if (ntreetype && ntreetype->foreach_nodeclass) {
ntreetype->foreach_nodeclass(scene, arg, node_menu_column_foreach_cb);
}
column = uiLayoutColumn(split, false);
UI_block_layout_set_current(block, column);
if (sock->link) {
uiItemL(column, IFACE_("Link"), ICON_NONE);
but = (uiBut *)block->buttons.last;
but->drawflag = UI_BUT_TEXT_LEFT;
but = uiDefBut(block,
UI_BTYPE_BUT,
0,
IFACE_("Remove"),
0,
0,
UI_UNIT_X * 4,
UI_UNIT_Y,
nullptr,
2012-07-01 09:54:44 +00:00
0.0,
0.0,
0.0,
0.0,
TIP_("Remove nodes connected to the input"));
UI_but_funcN_set(but, ui_node_link, MEM_dupallocN(arg), POINTER_FROM_INT(UI_NODE_LINK_REMOVE));
but = uiDefBut(block,
UI_BTYPE_BUT,
0,
IFACE_("Disconnect"),
0,
0,
UI_UNIT_X * 4,
UI_UNIT_Y,
nullptr,
2012-07-01 09:54:44 +00:00
0.0,
0.0,
0.0,
0.0,
TIP_("Disconnect nodes connected to the input"));
UI_but_funcN_set(
but, ui_node_link, MEM_dupallocN(arg), POINTER_FROM_INT(UI_NODE_LINK_DISCONNECT));
}
ui_node_menu_column(arg, NODE_CLASS_GROUP, N_("Group"));
}
} // namespace blender::ed::space_node
void uiTemplateNodeLink(
uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
{
using namespace blender::ed::space_node;
uiBlock *block = uiLayoutGetBlock(layout);
NodeLinkArg *arg;
uiBut *but;
float socket_col[4];
arg = MEM_new<NodeLinkArg>("NodeLinkArg");
arg->ntree = ntree;
arg->node = node;
arg->sock = input;
PointerRNA node_ptr = RNA_pointer_create(&ntree->id, &RNA_Node, node);
node_socket_color_get(*C, *ntree, node_ptr, *input, socket_col);
UI_block_layout_set_current(block, layout);
if (input->link || input->type == SOCK_SHADER || (input->flag & SOCK_HIDE_VALUE)) {
char name[UI_MAX_NAME_STR];
ui_node_sock_name(ntree, input, name);
2012-07-01 09:54:44 +00:00
but = uiDefMenuBut(
block, ui_template_node_link_menu, nullptr, name, 0, 0, UI_UNIT_X * 4, UI_UNIT_Y, "");
}
else {
but = uiDefIconMenuBut(
block, ui_template_node_link_menu, nullptr, ICON_NONE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "");
}
UI_but_type_set_menu_from_pulldown(but);
UI_but_node_link_set(but, input, socket_col);
UI_but_drawflag_enable(but, UI_BUT_ICON_LEFT);
2012-07-01 09:54:44 +00:00
but->poin = (char *)but;
but->func_argN = arg;
if (input->link && input->link->fromnode) {
if (input->link->fromnode->flag & NODE_ACTIVE_TEXTURE) {
but->flag |= UI_BUT_NODE_ACTIVE;
}
}
}
namespace blender::ed::space_node {
/**************************** Node Tree Layout *******************************/
static void ui_node_draw_input(uiLayout &layout,
bContext &C,
bNodeTree &ntree,
bNode &node,
bNodeSocket &input,
int depth,
const char *panel_label);
static void node_panel_toggle_button_cb(bContext *C, void *panel_state_argv, void *ntree_argv)
{
Main *bmain = CTX_data_main(C);
bNodePanelState *panel_state = static_cast<bNodePanelState *>(panel_state_argv);
bNodeTree *ntree = static_cast<bNodeTree *>(ntree_argv);
panel_state->flag ^= NODE_PANEL_COLLAPSED;
ED_node_tree_propagate_change(C, bmain, ntree);
/* Make sure panel state updates from the Properties Editor, too. */
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_NODE_VIEW, nullptr);
}
static void ui_node_draw_panel(uiLayout &layout,
bContext &C,
bNodeTree &ntree,
const nodes::PanelDeclaration &panel_decl,
bNodePanelState &panel_state,
PointerRNA nodeptr)
{
uiLayout *row = uiLayoutRow(&layout, true);
uiLayoutSetPropDecorate(row, false);
/* Panel header with collapse icon */
uiBlock *block = uiLayoutGetBlock(row);
UI_block_emboss_set(block, UI_EMBOSS_NONE);
uiBut *but = uiDefIconTextBut(block,
UI_BTYPE_BUT,
0,
panel_state.is_collapsed() ? ICON_RIGHTARROW : ICON_DOWNARROW_HLT,
IFACE_(panel_decl.name.c_str()),
0,
0,
UI_UNIT_X * 4,
UI_UNIT_Y,
nullptr,
0.0,
0.0,
0.0,
0.0,
"");
UI_but_drawflag_enable(but, UI_BUT_TEXT_LEFT | UI_BUT_NO_TOOLTIP);
UI_but_func_set(but, node_panel_toggle_button_cb, &panel_state, &ntree);
UI_block_emboss_set(block, UI_EMBOSS);
/* Panel buttons. */
if (!panel_state.is_collapsed() && panel_decl.draw_buttons) {
uiLayoutSetPropSep(&layout, true);
panel_decl.draw_buttons(&layout, &C, &nodeptr);
}
}
static void ui_node_draw_node(
uiLayout &layout, bContext &C, bNodeTree &ntree, bNode &node, int depth)
{
PointerRNA nodeptr = RNA_pointer_create(&ntree.id, &RNA_Node, &node);
if (node.typeinfo->draw_buttons) {
if (node.type != NODE_GROUP) {
uiLayoutSetPropSep(&layout, true);
node.typeinfo->draw_buttons(&layout, &C, &nodeptr);
}
}
if (node.declaration() && node.declaration()->use_custom_socket_order) {
/* Node with panels. */
namespace nodes = blender::nodes;
using ItemDeclIterator = blender::Span<nodes::ItemDeclarationPtr>::iterator;
using SocketIterator = blender::Span<bNodeSocket *>::iterator;
using PanelStateIterator = blender::MutableSpan<bNodePanelState>::iterator;
ItemDeclIterator item_decl = node.declaration()->items.begin();
SocketIterator input = node.input_sockets().begin();
PanelStateIterator panel_state = node.panel_states().begin();
const ItemDeclIterator item_decl_end = node.declaration()->items.end();
bool panel_collapsed = false;
const char *panel_label = nullptr;
for (; item_decl != item_decl_end; ++item_decl) {
if (const nodes::SocketDeclaration *socket_decl =
dynamic_cast<const nodes::SocketDeclaration *>(item_decl->get()))
{
if (socket_decl->in_out == SOCK_IN) {
if (!panel_collapsed) {
ui_node_draw_input(layout, C, ntree, node, **input, depth + 1, panel_label);
}
++input;
}
}
else if (const nodes::PanelDeclaration *panel_decl =
dynamic_cast<const nodes::PanelDeclaration *>(item_decl->get()))
{
panel_collapsed = panel_state->is_collapsed();
panel_label = panel_decl->name.c_str();
ui_node_draw_panel(layout, C, ntree, *panel_decl, *panel_state, nodeptr);
++panel_state;
}
}
}
else {
/* Node without panels. */
LISTBASE_FOREACH (bNodeSocket *, input, &node.inputs) {
ui_node_draw_input(layout, C, ntree, node, *input, depth + 1, nullptr);
}
}
}
static void ui_node_draw_input(uiLayout &layout,
bContext &C,
bNodeTree &ntree,
bNode &node,
bNodeSocket &input,
int depth,
const char *panel_label)
{
uiBlock *block = uiLayoutGetBlock(&layout);
uiLayout *row = nullptr;
bool dependency_loop;
if (input.flag & SOCK_UNAVAIL) {
return;
}
/* to avoid eternal loops on cyclic dependencies */
node.flag |= NODE_TEST;
bNode *lnode = (input.link) ? input.link->fromnode : nullptr;
dependency_loop = (lnode && (lnode->flag & NODE_TEST));
if (dependency_loop) {
lnode = nullptr;
}
/* socket RNA pointer */
PointerRNA inputptr = RNA_pointer_create(&ntree.id, &RNA_NodeSocket, &input);
PointerRNA nodeptr = RNA_pointer_create(&ntree.id, &RNA_Node, &node);
row = uiLayoutRow(&layout, true);
/* Decorations are added manually here. */
uiLayoutSetPropDecorate(row, false);
uiPropertySplitWrapper split_wrapper = uiItemPropertySplitWrapperCreate(row);
/* Empty decorator item for alignment. */
bool add_dummy_decorator = false;
{
uiLayout *sub = uiLayoutRow(split_wrapper.label_column, true);
if (depth > 0) {
UI_block_emboss_set(block, UI_EMBOSS_NONE);
if (lnode &&
(lnode->inputs.first || (lnode->typeinfo->draw_buttons && lnode->type != NODE_GROUP)))
{
int icon = (input.flag & SOCK_COLLAPSED) ? ICON_RIGHTARROW : ICON_DOWNARROW_HLT;
uiItemR(sub, &inputptr, "show_expanded", UI_ITEM_R_ICON_ONLY, "", icon);
}
UI_block_emboss_set(block, UI_EMBOSS);
}
sub = uiLayoutRow(sub, true);
uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_RIGHT);
uiItemL(sub, node_socket_get_label(&input, panel_label), ICON_NONE);
}
if (dependency_loop) {
uiItemL(row, RPT_("Dependency Loop"), ICON_ERROR);
add_dummy_decorator = true;
}
else if (lnode) {
/* input linked to a node */
uiTemplateNodeLink(row, &C, &ntree, &node, &input);
add_dummy_decorator = true;
if (depth == 0 || !(input.flag & SOCK_COLLAPSED)) {
if (depth == 0) {
uiItemS(&layout);
}
ui_node_draw_node(layout, C, ntree, *lnode, depth);
}
}
else {
UI: Add support for showing socket descriptions in tooltips Currently, hovering over a socket itself shows no tooltip at all, while hovering over its value field shows "Default value", which is not helpful. This patch therefore implements socket tooltips following the proposal at https://blender.community/c/rightclickselect/2Qgbbc/. A lot of the basic functionality was already implemented for Geometry Nodes, where hovering over the socket itself shows introspection info. This patch extends this by: - Supporting dynamic tooltips on labels, which is important for good tooltip coverage in a socket's region of the node. - Adding a function to setting a dynamic tooltip for an entire uiLayout, which avoids needing to set it manually for a wide variety of socket types. - Hiding the property label field in a tooltip when dynamic tooltip is also provided. If really needed, this label can be restored through the dynamic tooltip, but in all current cases the label is actually pointless anyways since the dynamic tooltip gives more accurate and specific information. - Adding dynamic tooltips to a socket's UI layout row if it has a description configured, both in the Node Editor as well as in the Material Properties. Note that the patch does not add any actual tooltip content yet, just the infrastructure to show them. By default, sockets without a description still show the old "Default value" tooltip. For an example of how to add socket descriptions, check the Cylinder node in the Geometry Nodes. Differential Revision: https://developer.blender.org/D9967
2022-04-11 02:02:12 +02:00
uiLayout *sub = uiLayoutRow(row, true);
uiTemplateNodeLink(sub, &C, &ntree, &node, &input);
if (input.flag & SOCK_HIDE_VALUE) {
add_dummy_decorator = true;
}
/* input not linked, show value */
else {
switch (input.type) {
case SOCK_VECTOR:
UI: Add support for showing socket descriptions in tooltips Currently, hovering over a socket itself shows no tooltip at all, while hovering over its value field shows "Default value", which is not helpful. This patch therefore implements socket tooltips following the proposal at https://blender.community/c/rightclickselect/2Qgbbc/. A lot of the basic functionality was already implemented for Geometry Nodes, where hovering over the socket itself shows introspection info. This patch extends this by: - Supporting dynamic tooltips on labels, which is important for good tooltip coverage in a socket's region of the node. - Adding a function to setting a dynamic tooltip for an entire uiLayout, which avoids needing to set it manually for a wide variety of socket types. - Hiding the property label field in a tooltip when dynamic tooltip is also provided. If really needed, this label can be restored through the dynamic tooltip, but in all current cases the label is actually pointless anyways since the dynamic tooltip gives more accurate and specific information. - Adding dynamic tooltips to a socket's UI layout row if it has a description configured, both in the Node Editor as well as in the Material Properties. Note that the patch does not add any actual tooltip content yet, just the infrastructure to show them. By default, sockets without a description still show the old "Default value" tooltip. For an example of how to add socket descriptions, check the Cylinder node in the Geometry Nodes. Differential Revision: https://developer.blender.org/D9967
2022-04-11 02:02:12 +02:00
uiItemS(sub);
sub = uiLayoutColumn(sub, true);
ATTR_FALLTHROUGH;
case SOCK_FLOAT:
case SOCK_INT:
case SOCK_ROTATION:
case SOCK_BOOLEAN:
case SOCK_RGBA:
uiItemR(sub, &inputptr, "default_value", UI_ITEM_NONE, "", ICON_NONE);
uiItemDecoratorR(
split_wrapper.decorate_column, &inputptr, "default_value", RNA_NO_INDEX);
break;
case SOCK_STRING: {
const bNodeTree *node_tree = (const bNodeTree *)nodeptr.owner_id;
SpaceNode *snode = CTX_wm_space_node(&C);
if (node_tree->type == NTREE_GEOMETRY && snode != nullptr) {
/* Only add the attribute search in the node editor, in other places there is not
* enough context. */
node_geometry_add_attribute_search_button(C, node, inputptr, *sub);
}
else {
uiItemR(sub, &inputptr, "default_value", UI_ITEM_NONE, "", ICON_NONE);
}
uiItemDecoratorR(
split_wrapper.decorate_column, &inputptr, "default_value", RNA_NO_INDEX);
break;
}
Geometry Nodes: Menu Switch Node This patch adds support for _Menu Switch_ nodes and enum definitions in node trees more generally. The design is based on the outcome of the [2022 Nodes Workshop](https://code.blender.org/2022/11/geometry-nodes-workshop-2022/#menu-switch). The _Menu Switch_ node is an advanced version of the _Switch_ node which has a customizable **menu input socket** instead of a simple boolean. The _items_ of this menu are owned by the node itself. Each item has a name and description and unique identifier that is used internally. A menu _socket_ represents a concrete value out of the list of items. To enable selection of an enum value for unconnected sockets the menu is presented as a dropdown list like built-in enums. When the socket is connected a shared pointer to the enum definition is propagated along links and stored in socket default values. This allows node groups to expose a menu from an internal menu switch as a parameter. The enum definition is a runtime copy of the enum items in DNA that allows sharing. A menu socket can have multiple connections, which can lead to ambiguity. If two or more different menu source nodes are connected to a socket it gets marked as _undefined_. Any connection to an undefined menu socket is invalid as a hint to users that there is a problem. A warning/error is also shown on nodes with undefined menu sockets. At runtime the value of a menu socket is the simple integer identifier. This can also be a field in geometry nodes. The identifier is unique within each enum definition, and it is persistent even when items are added, removed, or changed. Changing the name of an item does not affect the internal identifier, so users can rename enum items without breaking existing input values. This also persists if, for example, a linked node group is temporarily unavailable. Pull Request: https://projects.blender.org/blender/blender/pulls/113445
2024-01-26 12:40:01 +01:00
case SOCK_MENU:
uiItemL(sub, RPT_("Unsupported Menu Socket"), ICON_NONE);
break;
case SOCK_CUSTOM:
input.typeinfo->draw(&C, sub, &inputptr, &nodeptr, input.name);
break;
default:
add_dummy_decorator = true;
}
}
}
if (add_dummy_decorator) {
uiItemDecoratorR(split_wrapper.decorate_column, nullptr, nullptr, 0);
}
node_socket_add_tooltip(ntree, input, *row);
UI: Add support for showing socket descriptions in tooltips Currently, hovering over a socket itself shows no tooltip at all, while hovering over its value field shows "Default value", which is not helpful. This patch therefore implements socket tooltips following the proposal at https://blender.community/c/rightclickselect/2Qgbbc/. A lot of the basic functionality was already implemented for Geometry Nodes, where hovering over the socket itself shows introspection info. This patch extends this by: - Supporting dynamic tooltips on labels, which is important for good tooltip coverage in a socket's region of the node. - Adding a function to setting a dynamic tooltip for an entire uiLayout, which avoids needing to set it manually for a wide variety of socket types. - Hiding the property label field in a tooltip when dynamic tooltip is also provided. If really needed, this label can be restored through the dynamic tooltip, but in all current cases the label is actually pointless anyways since the dynamic tooltip gives more accurate and specific information. - Adding dynamic tooltips to a socket's UI layout row if it has a description configured, both in the Node Editor as well as in the Material Properties. Note that the patch does not add any actual tooltip content yet, just the infrastructure to show them. By default, sockets without a description still show the old "Default value" tooltip. For an example of how to add socket descriptions, check the Cylinder node in the Geometry Nodes. Differential Revision: https://developer.blender.org/D9967
2022-04-11 02:02:12 +02:00
/* clear */
node.flag &= ~NODE_TEST;
}
} // namespace blender::ed::space_node
void uiTemplateNodeView(
uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
{
using namespace blender::ed::space_node;
if (!ntree) {
return;
}
ntree->ensure_topology_cache();
/* clear for cycle check */
LISTBASE_FOREACH (bNode *, tnode, &ntree->nodes) {
tnode->flag &= ~NODE_TEST;
}
if (input) {
ui_node_draw_input(*layout, *C, *ntree, *node, *input, 0, nullptr);
}
else {
ui_node_draw_node(*layout, *C, *ntree, *node, 0);
}
}