Commit Graph

59 Commits

Author SHA1 Message Date
Jacques Lucke
363eff4f59 Fix: Nodes: missing null check
This caused a crash when creating a group from a node which has
a virtual input socket (e.g. Menu Switch).
2025-05-23 09:21:18 +02:00
Jacques Lucke
c55ffb5258 Nodes: improve implicit inputs
This improves implicit node inputs in multiple ways:
* Fix crash when switching a group input socket type from e.g. vector to
  integer, while the default input is set to "position". Now, the default input
  type is reset automatically if it's invalid.
* Add Left/Right Handle as possible implicit vector inputs (next to Position and
  Normal). Those were the only ones that we used internally that were not
  exposed yet.
* When creating a new group input from an existing socket, also initialize the
  default input based on the socket. E.g. when grouping a `Set Position` node,
  the `Position` input of the group will now also use the position attribute by
  default.

In addition to these user-level changes, some internal changes were done too:
* Use unified `NodeDefaultInputType` in node declaration instead of function
  pointers which were hard to propagate to node groups.
* Use a new reusable `socket_type_supports_default_input_type` function in rna
  to filter the list of possible input items.

Pull Request: https://projects.blender.org/blender/blender/pulls/139139
2025-05-20 17:54:49 +02:00
Jacques Lucke
81275eff60 Nodes: improve forward compatibility for unknown interface item types
#138747 adds a new interface item type for separators. Unfortunately, the
current code is not forward-compatible with those. This patch improves forward
compatibility by removing all unknown tree interface item types on-load.

Pull Request: https://projects.blender.org/blender/blender/pulls/139015
2025-05-19 17:52:15 +02:00
Jacques Lucke
d04699397b Fix: Nodes: off-by-one error when reordering tree interface items
It was possible to move an input socket above one output socket.

Caused by 45eb746250.
2025-05-17 15:18:26 +02:00
Jacques Lucke
45eb746250 Cleanup: Nodes: generalize finding valid insert position for tree view item
Currently, there are some constraints on the interface of node groups:
* Sockets have to be above panels.
* Outputs have to be above inputs.

The current code that ensures these constraints wasn't able to handle the
case when there are more interface items without the same constraints.

This patch is extracted from #138747.
2025-05-17 11:35:07 +02:00
Jacques Lucke
b2b22cb66d Cleanup: Nodes: switch using enum instead of int
This simplifies adding more interface item types, because there will
be a compiler warning when it's not handled somewhere.
2025-05-17 11:31:08 +02:00
Jacques Lucke
5d98465acd Fix: Geometry Nodes: wrong struct passed to BLO_read_struct
This didn't cause issues so far, because only the size of that passed in
struct is taken into account currently.
2025-05-17 11:27:45 +02:00
Bastien Montagne
dd168a35c5 Refactor: Replace MEM_cnew with a type-aware template version of MEM_callocN.
The general idea is to keep the 'old', C-style MEM_callocN signature, and slowly
replace most of its usages with the new, C++-style type-safer template version.

* `MEM_cnew<T>` allocation version is renamed to `MEM_callocN<T>`.
* `MEM_cnew_array<T>` allocation version is renamed to `MEM_calloc_arrayN<T>`.
* `MEM_cnew<T>` duplicate version is renamed to `MEM_dupallocN<T>`.

Similar templates type-safe version of `MEM_mallocN` will be added soon
as well.

Following discussions in !134452.

NOTE: For now static type checking in `MEM_callocN` and related are slightly
different for Windows MSVC. This compiler seems to consider structs using the
`DNA_DEFINE_CXX_METHODS` macro as non-trivial (likely because their default
copy constructors are deleted). So using checks on trivially
constructible/destructible instead on this compiler/system.

Pull Request: https://projects.blender.org/blender/blender/pulls/134771
2025-03-05 16:35:09 +01:00
Falk David
2822777f13 Nodes: support boolean inputs as toggles in panel headers
Adds the option to create a boolean socket that can be used as a panel toggle.
This allows creating simpler and more compact node group UIs when a panel
can be "disabled".

The toggle input is a normal input socket that is just drawn a bit differently in
the UI. Whether a boolean is a toggle input or not does not affect evaluation.

Also see #133936 for guides on how to add and remove panel toggles.

Pull Request: https://projects.blender.org/blender/blender/pulls/133936
2025-02-28 19:07:02 +01:00
Pratik Borhade
3a1e7ffebe Fix #134737: Node: Panel deleted when Parented with children
Skip `move_to_parent()` operation when new_parent is already
children of "item" to avoid running into this situation.

Pull Request: https://projects.blender.org/blender/blender/pulls/135148
2025-02-26 14:13:33 +01:00
илья _
119fc054f8 Cleanup: BKE: Nodes: Pass-by-reference
Restriction of the nodes api to clearly define never-null function arguments.
Side effects: some assertions and null-check (with early return) were removed.
On the caller side is ensured to never derefer null to pass argument (mainly in RNA).
In addition, one pointer argument now actually a return type.

By-reference return types instead of pointers going to be separate kind of
change since also imply of cleaning up variables created from reference.

Also good future improvement would be to mark a copy-constructor as
explicit for DNA node types.

Pull Request: https://projects.blender.org/blender/blender/pulls/134627
2025-02-19 13:44:11 +01:00
Brecht Van Lommel
478426c937 Cleanup: Various clang-tidy warnings in blenkernel
Pull Request: https://projects.blender.org/blender/blender/pulls/133734
2025-01-31 17:03:17 +01:00
Hans Goudey
7d6e098ca1 Cleanup: Remove unused includes in blenkernel
Pull Request: https://projects.blender.org/blender/blender/pulls/133688
2025-01-28 15:27:34 +01:00
Jacques Lucke
01be014b3d Fix: add missing node tree topology cache 2025-01-13 15:06:50 +01:00
Hans Goudey
13b79072e2 Refactor: Nodes: Use C++ new, std::string in node/socket/tree types
Make the type structs non-trivial, use new and delete for allocation and
freeing, and use std::string for most strings they contain. Also use
StringRef instead of char pointers in a few places. Mainly this improves
ergonomics when working with the strings.

Pull Request: https://projects.blender.org/blender/blender/pulls/132750
2025-01-08 16:34:41 +01:00
Brecht Van Lommel
920e709069 Refactor: Make header files more clangd and clang-tidy friendly
When using clangd or running clang-tidy on headers there are
currently many errors. These are noisy in IDEs, make auto fixes
impossible, and break features like code completion, refactoring
and navigation.

This makes source/blender headers work by themselves, which is
generally the goal anyway. But #includes and forward declarations
were often incomplete.

* Add #includes and forward declarations
* Add IWYU pragma: export in a few places
* Remove some unused #includes (but there are many more)
* Tweak ShaderCreateInfo macros to work better with clangd

Some types of headers still have errors, these could be fixed or
worked around with more investigation. Mostly preprocessor
template headers like NOD_static_types.h.

Note that that disabling WITH_UNITY_BUILD is required for clangd to
work properly, otherwise compile_commands.json does not contain
the information for the relevant source files.

For more details see the developer docs:
https://developer.blender.org/docs/handbook/tooling/clangd/

Pull Request: https://projects.blender.org/blender/blender/pulls/132608
2025-01-07 12:39:13 +01:00
Jacques Lucke
8bec7dce35 Nodes: avoid O(n^2) when looking up interface input indices
Previously, the code was O(n^2) because it iterated over all interface sockets,
and for each it tried to find the corresponding index by iterating over all
inputs again. Now, a `VectorSet` is used to make finding the index `O(1)`. The
new utility function may also be useful elsewhere.

Pull Request: https://projects.blender.org/blender/blender/pulls/132272
2024-12-23 15:01:07 +01:00
Hans Goudey
de8f882817 Cleanup: Use "this" keyword to access class members 2024-12-11 11:47:20 -05:00
Jacques Lucke
4fd49ae9f3 Fix #119341: improve propagating node tree interface settings
Copy the interface socket directly which makes sure that all relevant data is passed on.
The default is still adapted from the current socket value.

There are still places that could be improved that are not covered by this patch yet.
For example creating a group from a Set Position node changes the default Position input.
That could be fixed separately.

I removed one `assert` that did not seem important. It was making sure that an interface
item is only copied within a group, but I don't see a reason for why it should not be possible
to copy an item to a different group.

Pull Request: https://projects.blender.org/blender/blender/pulls/131394
2024-12-05 13:15:51 +01:00
Iliya Katueshenock
7348e670b3 Cleanup: BKE: Use StringRefNull instead of char *
Use StringRefNull for all function arguments and return types.
Not a StringRef but StringRefNull since there is still large
interaction with C api so null-termination usually necessary.

If string is expected to be not only empty but also a null then
optional is used. This change depends on #130935.

Pull Request: https://projects.blender.org/blender/blender/pulls/131204
2024-12-02 19:24:07 +01:00
Jacques Lucke
7dc630069b Nodes: support subpanels in node group interface
Previously, only one level of panels was supported. Now, they can be nested arbitrarily.
Panels still have to come at the bottom though.

The panel color used to be just the node color darkened a bit. Now it uses the
`TH_PANEL_SUB_BACK` theme setting which is also used by panels in other places
in Blender. However, the contrast of that is a bit weaker than what we had in nodes before.
Therefore I increased the contrast a bit.

Pull Request: https://projects.blender.org/blender/blender/pulls/128886
2024-11-26 17:01:14 +01:00
Jacques Lucke
74f266c119 Fix #127855: crash when changing interface socket type with custom sockets
There were multiple issues that popped up when following the reproduction steps
in the report:
* Somehow, `socket.socket_data` was set for a custom socket type. I don't know
  how that happened yet. I added some code to clean it up on file-load.
* `declaration_for_interface_socket` did not work yet when the socket type is
  not registered. Now it just creates a custom socket with the correct idname,
  even if that's not registered yet.
* There was a missing node tree update when changing `bl_socket_idname` of
  interface sockets.

Pull Request: https://projects.blender.org/blender/blender/pulls/128191
2024-09-26 20:41:06 +02:00
Iliya Katueshenock
1b67be14c6 Cleanup: BKE: Nodes: Functions renaming
Use snake style naming for all the kernel nodes functions.
Omit kernel prefix in the names since of the using namespace.
Use full forms of the terms
('iter' -> 'iterator', 'ntree' -> 'node_tree', 'rem' -> 'remove', ...).

Pull Request: https://projects.blender.org/blender/blender/pulls/126416
2024-08-19 20:27:37 +02:00
Campbell Barton
c071030ac3 Cleanup: spelling in comments 2024-08-04 13:45:06 +10:00
Bastien Montagne
8f70a803b5 Nodes Read/Write: Add FIXME comments about usages of 'raw data' BLO API.
`BLO_read_data_address` should basically almost never be used. However,
this code is not trivial to update, and it has an active team working on
it, so for now just tagging the issue there.
2024-07-31 19:52:12 +02:00
Bastien Montagne
80948f43ca Refactor: BLO: Add array size parameter to BLO_read_pointer_array.
For some reason this was the only one of these 'read array' functions
with no such parameter.
2024-07-31 16:55:29 +02:00
Iliya Katueshenock
75d17b1db5 Cleanup: Move BKE_node to namespace
Move all header file into namespace.
Unnecessary namespaces was removed from implementations file.
Part of forward declarations in header was moved in the top part
of file just to do not have a lot of separate namespaces.

Pull Request: https://projects.blender.org/blender/blender/pulls/121637
2024-05-13 16:07:12 +02:00
Campbell Barton
9918488bb1 Cleanup: use uppercase tags, following own style guide 2024-05-03 11:33:21 +10:00
Brecht Van Lommel
15b9ae5436 Refactor: Use typed functions for blend file data reading
This makes the read and write API functions match more closely, and adds
asserts to check that the data size is as expected.

There are still a few places remaining that use BLO_read_data_address
and similar generic functions, these should eventually be replaced as well.

Pull Request: https://projects.blender.org/blender/blender/pulls/120994
2024-04-24 17:01:22 +02:00
Iliya Katueshenock
f8efd4fad0 Fix #120367: Attach to root panel if new parent is null
If pointer to new parent is null, use pointer to root panel of interface.

Pull Request: https://projects.blender.org/blender/blender/pulls/120369
2024-04-08 12:42:34 +02:00
Hans Goudey
e0567eadbd Cleanup: Use FunctionRef for IDProperty iteration callback
Avoid the need for a seaprate user_data argument.
2024-03-26 15:39:39 -04:00
Hans Goudey
efee753e8f Cleanup: Move BKE_idprop.h to C++ 2024-03-26 13:07:04 -04:00
Jacques Lucke
067daec270 Nodes: don't use combined input/output sockets in node tree interface
Those shouldn't really exist at all currently, since we forbid them in 354915cf3c.
Unfortunately, not all cases were removed, and many node trees with such sockets
have been created during the development of 4.1 due to the auto-smooth changes.

I'm not entirely sure if it's still possible to properly remove them now. The commit
above mentions that we might lose links due to changed identifiers. Maybe we're
better off just supporting these inlined sockets while also allowing the "align with
previous socket" functionality.

Either way, not creating more of such node trees for now is reasonable, since we
explicitly disabled that functionality for now.

Pull Request: https://projects.blender.org/blender/blender/pulls/119654
2024-03-20 11:26:07 +01:00
Lukas Tönne
5ad49f4142 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
Bastien Montagne
03c1cc4310 Cleanup: Move BKE_lib_query header to Cpp era. 2024-01-18 12:21:19 +01:00
Jacques Lucke
c954940e76 Fix: remove assert when creating interface socket
We don't actually protect against interface sockets with empty
names elsewhere. This assert was triggered when trying to group
a `Bake` node, because it tried to create an interface socket of
the extend socket. This case is handled by the check below.
2024-01-17 14:27:51 +01:00
Hans Goudey
3e76a1a6c2 Cleanup: Move BKE_lib_id.h to C++ 2024-01-15 12:44:14 -05:00
Jacques Lucke
c29857fffa Fix #113532: geometry nodes is always reevaluated after undo
The issue was that the undo system detected that node trees have been changed
even though they have not. This is because the memory dump of the node tree
changed after unrelated changes. The root issue was that
`bNodeTreeInterface::write` also wrote the struct, and the pointer of the
`bNodeTreeInterface` itself. While this pointer is generally stable, it is not
during file-write, because when IDs are written, a shallow copy is created
first (see `BLO_write_init_id_buffer_from_id`). The temporary buffer for this
shallow copy is allocated on the heap, so its pointer may change over time.

The fix is to simply not write the `bNodeTreeInterface` separately, since it's
already embedded in `bNodeTree`.

Pull Request: https://projects.blender.org/blender/blender/pulls/114322
2023-10-31 13:46:31 +01:00
Lukas Tönne
dfc3f75e77 Fix #113860: Nullptr checks for node socket and panel name pointers
In 3.6 the names of node group sockets were using char arrays, but now
use allocated strings. The RNA system assigns nullptr to such strings
when assigning an empty string through python (UI assignment appears to
always generate a valid string). This creates issues with many STL
functions, in particular assigning nullptr to `std::string` will crash.

We have to check for valid pointers before using them in places that
don't handle nullptrs.

Pull Request: https://projects.blender.org/blender/blender/pulls/113924
2023-10-19 11:48:08 +02:00
Hans Goudey
ecb52140cd Fix #113128: Socket hide value not copied to group input
This needs to be copied explicitly to the interface socket.

Pull Request: https://projects.blender.org/blender/blender/pulls/113286
2023-10-06 00:43:45 +02:00
Lukas Tönne
71732a9600 Fix #113014: Improved sorting to keep node group outputs above inputs
The position validation when manipulating node group items now includes
outputs..inputs order in addition to sockets..panels order. The method
for finding a valid position has been simplified, it's just a single
iteration of insertion sort.

Versioning has been added to ensure files from 4.0 alpha with
potentially unsorted sockets get re-sorted. This uses `std::stable_sort`
so that sockets keep their relative order apart from the input/output
grouping.

Pull Request: https://projects.blender.org/blender/blender/pulls/113060
2023-10-03 12:18:36 +02:00
Hans Goudey
c6380d772c Fix: Debug build error after recent cleanup
Caused by 8954b70d49
2023-09-22 11:22:30 -04:00
Hans Goudey
8954b70d49 Cleanup: Small changes in node interface item addition
- Use `StringRef` over `StringRefNull`, since it handles `nullptr` in
  its constructor and gives more flexibility about the string's source.
- Change `const char *` to `StringRef` as well, for improved ergonomics
  and efficiency.
- Move `add_interface_socket_from_node` to a .cc file. It's not small
  or performance sensitive enough to be inlined.
- Avoid allocating empty strings for item descriptions.
- Allocate with `BLI_strdupn` to avoid unnecessary `strlen` calls.
- Move a bit more code into a proper namespace.

Pull Request: https://projects.blender.org/blender/blender/pulls/112721
2023-09-22 15:43:59 +02:00
Lukas Tönne
b00c2f9ac4 Fix #112622: draw_color_simple is optional for custom nodes
Use magenta fallback color for sockets without a `draw_color_simple`
callback. This is not ideal, but without a context or node instance
the older `draw_color` callback can't be used.

Pull Request: https://projects.blender.org/blender/blender/pulls/112658
2023-09-21 11:25:55 +02:00
Lukas Tönne
d2f4ebcd6a Fix #112331: Add update tags directly in bNodeTreeInterface API methods
Calling an API function after the node panels patch does not internally
tag the node tree with `NTREE_CHANGED_INTERFACE` any more, because the
node tree is not directly accessible from `bNodeTreeInterface`. Before
node panels the API functions for interfaces could tag the tree directly
for later update consideration, which now requires explicit tagging
calls.

The fix is to add a flag and mutex directly to `bNodeTreeInterface`, so
API methods can tag after updates. This mostly copies runtime data
concepts from `bNodeTree`. The `ensure_interface_cache` method is
equivalent to `ensure_topology_cache` and should be called before
accessing `interface_inputs` and similar cache data.

Pull Request: https://projects.blender.org/blender/blender/pulls/111741
2023-09-14 14:13:07 +02:00
Lukas Tönne
fc321739f0 Fix #111963: Followup fix to ensure sockets inside panels have unique identifiers
#111972 made sure copied sockets have unique identifiers, but it didn't
cover the case of copying a panel and its children.

- Added an optional `uid_gen` argument to item copy functions which
  generates new unique identifiers for copied items. If not specified
  the items will retain the original identifiers (e.g. when copying an
  entire node tree).
- Removed the `copy_items` panel function from the API. Only used
  internally and requires a uid generator now.

Pull Request: https://projects.blender.org/blender/blender/pulls/112074
2023-09-07 15:06:38 +02:00
Lukas Tönne
b2b1657721 Fix #111963: Copying sockets was not creating unique identifiers
Items in the node group interface need to have unique identifiers.
Copying an item (socket or panel) was not doing this, so the node groups
end up having sockets with the same identifier. Linking sockets was
broken because copies could not be distinguished.

Pull Request: https://projects.blender.org/blender/blender/pulls/111972
2023-09-05 13:31:58 +02:00
Hans Goudey
9fd4091f5b Fix: Incorrect assert in node tree interface copy
Panels could have parents if the parent was the root panel.
Anyway, the assert isn't really helpful here, better to keep that
just for higher level editing functions.
2023-09-01 15:30:19 -04:00
Hans Goudey
32ca4caaac Fix: Various crashes and issues dragging node group interface
Mostly the issues were because drag and drop moving to a different
index in the parent didn't take into account the root panel. Also add
a null check for safety similar to b36367e663.
2023-09-01 15:30:19 -04:00
Lukas Tönne
cf12d11de6 Fix #111713: Nodes crash when library overriding
Library overrides crashed with on trying to access the interface root
panel. The root panel should not be exposed through RNA, but also
accessing its position property should be allowed.

Several fixes here:
- Fix position property getter when the item has no parent panel
  (i.e. the root panel).
- Avoid infinite loops in the greedy override comparison: exclude the
  loopback parent property.
- Don't return the root panel via the parent property in the first
  place, just return nullptr.

Second attempt at fix, first one in #111755 broke the
bl_node_group_interface test due to incorrect handling of the nullptr
parent case (reverted by 68440742)

Pull Request: https://projects.blender.org/blender/blender/pulls/111782
2023-09-01 12:39:51 +02:00