Support for simulation zones in copy operators #106812

Merged
Member

Copying a simulation zone should keep the 1:1 pairing intact (see remap_pairing functions)

  • When copying a simulation input node on its own, unpair it to avoid ambiguity. Only the old simulation input node is paired to the output.
  • When copying a simulation output node on its own, no special action is needed - the node gets a new ID and nothing is paired with it.
  • When copying both input and output, remap the output_node_id property of the simulation input node, so that it is paired with the output copy.

There are a couple of places where copies happen:

  • Node tree copy
  • Duplicate nodes
  • Group Separate (copies nodes from the group tree into another tree)
  • Clipboard (both copy to clipboard and paste into node tree)
  • Shader node tree branch copy for execution

These copy operators do mostly the same thing, but in slightly different ways, which makes the code incompatible (e.g. using a Map<const bNode *, bNode *> node_map vs. Map<bNode *, bNode *> node_map). That's why there are 3 remap_pairing implementations.

Dynamic node declarations are problematic:
Copying nodes invokes nodeDeclarationEnsure to generate declarations for new nodes. It does not, however, change the socket lists. If a dynamic declaration for a node copy alters the sockets (in this case: remove all because the node is unpaired), the subsequent update_socket_declarations will crash because it expects sockets to match the declaration.
At the end of operators there is usually a BKE_ntree_update_main or similar, which invokes update_node_declaration_and_sockets. This method does update the socket lists as well (see #106732), but only if a node is tagged for a respective update.

The solution here is to use update_node_declaration_and_sockets for dynamic declarations instead of just build_node_declaration_dynamic.

Fixes #106732

Copying a simulation zone should keep the 1:1 pairing intact (see `remap_pairing` functions) - When copying a simulation input node on its own, unpair it to avoid ambiguity. Only the old simulation input node is paired to the output. - When copying a simulation output node on its own, no special action is needed - the node gets a new ID and nothing is paired with it. - When copying both input and output, remap the `output_node_id` property of the simulation input node, so that it is paired with the output copy. There are a couple of places where copies happen: * Node tree copy * Duplicate nodes * Group Separate (copies nodes from the group tree into another tree) * Clipboard (both copy to clipboard and paste into node tree) * Shader node tree branch copy for execution These copy operators do mostly the same thing, but in slightly different ways, which makes the code incompatible (e.g. using a `Map<const bNode *, bNode *> node_map` vs. `Map<bNode *, bNode *> node_map`). That's why there are 3 `remap_pairing` implementations. Dynamic node declarations are problematic: Copying nodes invokes `nodeDeclarationEnsure` to generate declarations for new nodes. It does not, however, change the socket lists. If a dynamic declaration for a node copy alters the sockets (in this case: remove all because the node is unpaired), the subsequent `update_socket_declarations` will crash because it expects sockets to match the declaration. At the end of operators there is usually a `BKE_ntree_update_main` or similar, which invokes `update_node_declaration_and_sockets`. This method _does_ update the socket lists as well (see #106732), but only if a node is tagged for a respective update. The solution here is to use `update_node_declaration_and_sockets` for dynamic declarations instead of just `build_node_declaration_dynamic`. Fixes #106732
Lukas Tönne added the
Module
Nodes & Physics
label 2023-04-11 15:48:47 +02:00
Lukas Tönne added 4 commits 2023-04-11 15:49:02 +02:00
b43a85ce5f Move calls to `nodeDeclarationEnsure` past link remapping.
This is necessary because link remapping uses the `socket_map`. If the
declaration removes sockets this `socket_map` becomes invalid. So
updating the declaration may only happen _after_ any references to
previous sockets are no longer needed.
e6c71cb989 Perform a full socket update when updating dynamic declarations.
This is to ensure that dynamic declarations can actually change the
socket layout outside of operators that explicitly call
`update_node_declaration_and_sockets`.
Lukas Tönne added this to the Nodes & Physics project 2023-04-11 15:49:13 +02:00
Lukas Tönne reviewed 2023-04-11 15:57:59 +02:00
@ -3819,3 +3819,2 @@
BLI_assert(node != nullptr);
node->runtime->declaration = new blender::nodes::NodeDeclaration();
blender::nodes::build_node_declaration_dynamic(*ntree, *node, *node->runtime->declaration);
blender::nodes::update_node_declaration_and_sockets(*ntree, *node);
Author
Member

This is to ensure sockets are updated to match dynamic declarations.

This is to ensure sockets are updated to match dynamic declarations.
@ -237,10 +266,6 @@ static int node_clipboard_paste_exec(bContext *C, wmOperator *op)
}
}
for (bNode *new_node : node_map.values()) {
Author
Member

Changing socket layout too early causes the socket_map to become invalid. So this section has been moved down beyond the link remapping section.

This section was originally in node_copy_with_mapping (see f36dd0660983). It was moved out of the primary node duplication loop so that the simulation input could build its declaration based on the copy of the output node.

Same applies in "Duplicate" and "Group Separate" operators below.

Changing socket layout too early causes the `socket_map` to become invalid. So this section has been moved down beyond the link remapping section. This section was originally in `node_copy_with_mapping` (see [f36dd0660983](https://projects.blender.org/blender/blender/commit/f36dd066098311c6ec77ca144e5bb1b2bca252c1)). It was moved out of the primary node duplication loop so that the simulation input could build its declaration based on the copy of the output node. Same applies in "Duplicate" and "Group Separate" operators below.
Lukas Tönne requested review from Jacques Lucke 2023-04-11 15:58:48 +02:00
Lukas Tönne requested review from Hans Goudey 2023-04-11 15:58:48 +02:00
Author
Member

"Group Separate" operator is technically not correct because it copies nodes from the group tree, then potentially updates their ID values, then uses the old ID values to reconstruct links in the new tree. In practice this is unlikely to cause problems because IDs are random uint32_t numbers and almost never collide, so the copied nodes will almost always have the same IDs as the original nodes.

"Make Group" and "Ungroup" operators will currently unpair simulation nodes because they simply move the nodes to a different tree. Then the nodes get new random IDs, which means the output_node_id will almost certainly be incorrect, effectively un-pairing the simulation nodes.

"Group Separate" operator is technically not correct because it copies nodes from the group tree, then potentially updates their ID values, then uses the _old_ ID values to reconstruct links in the _new_ tree. In practice this is unlikely to cause problems because IDs are random `uint32_t` numbers and almost never collide, so the copied nodes will almost always have the same IDs as the original nodes. "Make Group" and "Ungroup" operators will currently unpair simulation nodes because they simply move the nodes to a different tree. Then the nodes get new random IDs, which means the `output_node_id` will almost certainly be incorrect, effectively un-pairing the simulation nodes.
Jacques Lucke requested changes 2023-04-11 17:23:00 +02:00
Jacques Lucke left a comment
Member

Worked perfectly in all my tests. Feels much better now than before :)

Worked perfectly in all my tests. Feels much better now than before :)
@ -185,0 +188,4 @@
{
/* We don't have the old tree for looking up output nodes by ID,
* so have to build a map first to find copied output nodes in the new tree. */
Map<uint32_t, bNode *> dst_output_node_map;
Member

Use int32_t instead of uint32_t for node identifiers.

Use `int32_t` instead of `uint32_t` for node identifiers.
Author
Member

Oh, somehow thought the identifiers were uint32_t, my bad.

Oh, somehow thought the identifiers were `uint32_t`, my bad.
LukasTonne marked this conversation as resolved
@ -439,0 +444,4 @@
dst_node->storage);
/* XXX Technically this is not correct because the output_node_id is only valid
* in the original node group tree and we'd have map old IDs to new nodes first.
* The ungroup operator does not build a node map, it just expects node IDs to
Member

Based on the comment it sounds like the proper solution would be to build the node map. Is there a reason for not just doing that instead of using a solution you already know might not work?

Based on the comment it sounds like the proper solution would be to build the node map. Is there a reason for not just doing that instead of using a solution you already know might not work?
Author
Member

I'd consider that a separate task, to fix the group separate operator. Ideally would also unify all the other copy operators. It is extremely unlikely this would cause problems in practice because of the randomness of identifiers. Relying on randomness and expecting that the identifier method remains unchanged is bad of course.

I've added a new task for this #106852

I'd consider that a separate task, to fix the group separate operator. Ideally would also unify all the other copy operators. It is extremely unlikely this would cause problems in practice because of the randomness of identifiers. Relying on randomness and expecting that the identifier method remains unchanged is bad of course. I've added a new task for this #106852
Lukas Tönne added 1 commit 2023-04-12 12:23:51 +02:00
Jacques Lucke approved these changes 2023-04-13 20:04:57 +02:00
Jacques Lucke left a comment
Member

The patch still uses uint32_t in two places (one of those is in a comment), that should still be fixed.

The patch still uses `uint32_t` in two places (one of those is in a comment), that should still be fixed.
Hans Goudey requested changes 2023-04-14 04:00:41 +02:00
Hans Goudey left a comment
Member

I'm not sure how I feel about adding three implementations of remap_pairing. It seems like they could be unified by creating a Map<int32_t, int32_t> when copying node groups instead of the various set/map types we currently use. If you plan to work on #106852 in the near future, we could still commit this now, but otherwise I think it would make more sense for that unification to happen first, better not to leave it like this longer term.

I'm not sure how I feel about adding three implementations of `remap_pairing`. It seems like they could be unified by creating a `Map<int32_t, int32_t>` when copying node groups instead of the various set/map types we currently use. If you plan to work on #106852 in the near future, we could still commit this now, but otherwise I think it would make more sense for that unification to happen first, better not to leave it like this longer term.
Author
Member

otherwise I think it would make more sense for that unification to happen first, better not to leave it like this longer term.

I'll give the copy unification #106852 a go and see if i run into any major obstacles.

> otherwise I think it would make more sense for that unification to happen first, better not to leave it like this longer term. I'll give the copy unification #106852 a go and see if i run into any major obstacles.
Lukas Tönne added 1 commit 2023-04-17 14:46:12 +02:00
Hans Goudey approved these changes 2023-04-17 15:35:08 +02:00
Lukas Tönne merged commit 315cc66bd8 into geometry-nodes-simulation 2023-04-17 16:11:09 +02:00
Lukas Tönne deleted branch geometry-nodes-simulation-copy-support 2023-04-17 16:11:10 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#106812
No description provided.