Add simulation input and output node as a pair #106557

Merged
Member

Simulation input and output nodes are currently added individually, but they always need to exist as pair. This PR modifies the Add Node menu so that a single menu entry adds and input and output node together.

The NODE_OT_add_node operator currently adds just a single node type. A new variant of this operator is added which adds a simulation zone with origin + target node instead. This requires some modification of the NodeAddOperator base class, moving the node_type property into the final implementation. Unlike the NODE_OT_add_node operator, the NODE_OT_add_simulation_zone adds 2 different node types.

After adding the two nodes, a reference needs to be added to "pair" them: Input node ("origin") stores the UID of the output node ("target") in its output_node_id property. So far this was detected automatically when adding an input, but this method is not very robust (e.g. it depends on order of adding nodes and adding multiple pairs can be tricky).

Now the pairing is done explicitly through an API function node_geo_simulation_input_pair_with_output. The NODE_OT_add_simulation_zone operator performs pairing of the input/output nodes after adding them. The function is accessible through RNA, so an operator may be added if necessary to allow users to fix unpaired nodes.

In addition to pairing the two nodes, the operator also positions them at a comfortable distance, as well as adding a default link between the two Geometry sockets for convenience.

Resolves #105727

Simulation input and output nodes are currently added individually, but they always need to exist as pair. This PR modifies the _Add Node_ menu so that a single menu entry adds and input and output node together. The `NODE_OT_add_node` operator currently adds just a single node type. A new variant of this operator is added which adds a _simulation zone_ with origin + target node instead. This requires some modification of the `NodeAddOperator` base class, moving the `node_type` property into the final implementation. Unlike the `NODE_OT_add_node` operator, the `NODE_OT_add_simulation_zone` adds 2 different node types. After adding the two nodes, a reference needs to be added to "pair" them: Input node ("origin") stores the UID of the output node ("target") in its `output_node_id` property. So far this was detected automatically when adding an input, but this method is not very robust (e.g. it depends on order of adding nodes and adding multiple pairs can be tricky). Now the pairing is done explicitly through an API function `node_geo_simulation_input_pair_with_output`. The `NODE_OT_add_simulation_zone` operator performs pairing of the input/output nodes after adding them. The function is accessible through RNA, so an operator may be added if necessary to allow users to fix unpaired nodes. In addition to pairing the two nodes, the operator also positions them at a comfortable distance, as well as adding a default link between the two Geometry sockets for convenience. Resolves #105727 <video width="320" controls src="/attachments/1134f217-c1c2-47e9-96a2-6f76d7c1dc15"></video>
Lukas Tönne added 5 commits 2023-04-04 17:29:03 +02:00
8dfeeb1c29 Removed outdated comment on the create_node function.
The search operator has been moved and does not call this function
directly any more.
ea6aa88a23 Implemented an explicit pairing function for simulation input/output.
This replaces the search logic in the simulation input node, which
previously set the `output_node_id` to pair an input to an output.
Now the pairing takes place right after nodes are created by the operator.

An operator will also be added that allows pairing simulation nodes in case
the pairing is lost somehow or one part of the input/output pair is deleted.
That shouldn't normally be happening, but it might be the result of python
scripting or some corner case. It's good to have a regular method of
correcting such errors.
Lukas Tönne added this to the Nodes & Physics project 2023-04-04 17:30:03 +02:00
Lukas Tönne added the
Module
Nodes & Physics
label 2023-04-04 17:30:10 +02:00
Lukas Tönne requested review from Jacques Lucke 2023-04-04 18:17:34 +02:00
Lukas Tönne requested review from Hans Goudey 2023-04-04 18:17:34 +02:00

Does that also work if I add the pair via the Python API without using an operator? e.g., by using: my_node_group.nodes.add.new("GeometryNodeSimulation")

Does that also work if I add the pair via the Python API **without** using an operator? e.g., by using: my_node_group.nodes.add.new("GeometryNodeSimulation")
Author
Member

Does that also work if I add the pair via the Python API without using an operator? e.g., by using: my_node_group.nodes.add.new("GeometryNodeSimulation")

No, the nodes.new function just takes a single actual node type. You can, however, do what the menu entry does and add an input, an output, and then pair the two nodes:

origin_node = nodes.new("GeometryNodesSimulationInput")
target_node = nodes.new("GeometryNodesSimulationOutput")

origin_node.pair_with_output(target_node)
> Does that also work if I add the pair via the Python API **without** using an operator? e.g., by using: my_node_group.nodes.add.new("GeometryNodeSimulation") No, the `nodes.new` function just takes a single actual node type. You can, however, do what the menu entry does and add an input, an output, and then pair the two nodes: ```python origin_node = nodes.new("GeometryNodesSimulationInput") target_node = nodes.new("GeometryNodesSimulationOutput") origin_node.pair_with_output(target_node) ```
Member

Thanks, the behavior looks good. I'd change Simulation Nodes to just Simulation or Simulation Zone (haven't made up my mind yet).

Implementation wise it feels like adding NODE_OT_add_node_pair instead of e.g. NODE_OT_add_simulation_zone is premature abstraction and feels like this abstraction won't hold up for long (especially since it has simulation nodes specific behavior already). I think this patch could be a fair amount simpler by not trying to abstract away adding a node pair. Once we add e.g. NODE_OT_add_parallel_loop_zone we can potentially extract common functionality into utility functions (instead of having a single operator for both).

The new pair_with_output method in rna and its implementation seem reasonable.

Regarding the constructor of LazyFunctionForSimulationInputNode, I think it would be better to only get to that place when it is known that the simulation zone is complete, hence the validity of the zone should probably be checked in handle_simulation_input_node already (also see handle_group_node for how it handles error conditions). This simplifies the code there because it doesn't have to handle the case anymore.
Also note that, while this has to be done at some point, it's not a change that has to be included in this patch, because it's kind of unrelated to adding the simulation zone.

Thanks, the behavior looks good. I'd change `Simulation Nodes` to just `Simulation` or `Simulation Zone` (haven't made up my mind yet). Implementation wise it feels like adding `NODE_OT_add_node_pair` instead of e.g. `NODE_OT_add_simulation_zone` is premature abstraction and feels like this abstraction won't hold up for long (especially since it has simulation nodes specific behavior already). I think this patch could be a fair amount simpler by not trying to abstract away adding a node pair. Once we add e.g. `NODE_OT_add_parallel_loop_zone` we can potentially extract common functionality into utility functions (instead of having a single operator for both). The new `pair_with_output` method in rna and its implementation seem reasonable. Regarding the constructor of `LazyFunctionForSimulationInputNode`, I think it would be better to only get to that place when it is known that the simulation zone is complete, hence the validity of the zone should probably be checked in `handle_simulation_input_node` already (also see `handle_group_node` for how it handles error conditions). This simplifies the code there because it doesn't have to handle the case anymore. Also note that, while this has to be done at some point, it's not a change that has to be included in this patch, because it's kind of unrelated to adding the simulation zone.
Author
Member

Simulation or Simulation Zone

I'd prefer just "Simulation", IMO "zone" is too much of an implementation detail.

Implementation wise it feels like adding NODE_OT_add_node_pair

Yeah, i wasn't sure about that, didn't like to hardcode node types into the operator. I'm ok with making the operator just for the input sim too. Do we have an idea if this zone concept will be carried over to general loops eventually?

it's not a change that has to be included in this patch

Ok, but i did noticed it crashes very easily when e.g. removing one node. I think it needs to be made robust soon, so pairing isn't always just expected to be valid. Can add a separate PR for that.

> Simulation or Simulation Zone I'd prefer just "Simulation", IMO "zone" is too much of an implementation detail. > Implementation wise it feels like adding `NODE_OT_add_node_pair` Yeah, i wasn't sure about that, didn't like to hardcode node types into the operator. I'm ok with making the operator just for the input sim too. Do we have an idea if this zone concept will be carried over to general loops eventually? > it's not a change that has to be included in this patch Ok, but i did noticed it crashes very easily when e.g. removing one node. I think it needs to be made robust soon, so pairing isn't always just expected to be valid. Can add a separate PR for that.
Member

I'd prefer just "Simulation", IMO "zone" is too much of an implementation detail.

I'm fine with that for now, but note that the purpose of finding a name for zones was explicitly so that we and users can talk about them instead of having to make up different names all the time. Using the term "Zone" in the menu can be a good teaching moment.

Yeah, i wasn't sure about that, didn't like to hardcode node types into the operator.

Hardcoding node types into operators is totally fine if the purpose of the operator is to add that node.

Do we have an idea if this zone concept will be carried over to general loops eventually?

There will likely be different types of loops, so I don't know what a "general loop" is for you. Either way, yes I think it's very likely that the we will use the zone concept for those as well.

> I'd prefer just "Simulation", IMO "zone" is too much of an implementation detail. I'm fine with that for now, but note that the purpose of finding a name for zones was explicitly so that we and users can talk about them instead of having to make up different names all the time. Using the term "Zone" in the menu can be a good teaching moment. > Yeah, i wasn't sure about that, didn't like to hardcode node types into the operator. Hardcoding node types into operators is totally fine if the purpose of the operator is to add that node. > Do we have an idea if this zone concept will be carried over to general loops eventually? There will likely be different types of loops, so I don't know what a "general loop" is for you. Either way, yes I think it's very likely that the we will use the zone concept for those as well.
Author
Member

Made a separate PR for checking the output is valid before creating the LF #106585

Made a separate PR for checking the output is valid before creating the LF #106585
Lukas Tönne added 1 commit 2023-04-05 11:39:51 +02:00
70e2c269a5 Revert "Avoid crashing when the sim input node has invalid output id."
There will be a check to avoid creating the lazy function in the first
place instead of checking inside the function.

This reverts commit c3b43ca52d.
Lukas Tönne added 1 commit 2023-04-05 11:47:38 +02:00
Lukas Tönne added 1 commit 2023-04-05 12:09:47 +02:00
Jacques Lucke approved these changes 2023-04-05 14:32:22 +02:00
Jacques Lucke left a comment
Member

Got some minor comments inline, but looks good now, thx.

Got some minor comments inline, but looks good now, thx.
@ -149,2 +159,4 @@
bl_label = "Add Simulation Zone"
bl_options = {'REGISTER', 'UNDO'}
origin_type = "GeometryNodeSimulationInput"
Member

Would call it input_node_type.

Would call it `input_node_type`.
@ -151,0 +180,4 @@
if origin_node is None or target_node is None:
return {'CANCELLED'}
# Simulation input must be paired with the output
Member

End comments with a dot.

End comments with a dot.
@ -207,2 +185,4 @@
nodeRegisterType(&ntype);
}
bool node_geo_simulation_input_pair_with_output(bNode *simulation_input_node,
Member

Should probably also fail in the case when trying to connect an output to an input that is already linked to another input.

Should probably also fail in the case when trying to connect an output to an input that is already linked to another input.
Lukas Tönne reviewed 2023-04-05 14:45:50 +02:00
@ -207,2 +185,4 @@
nodeRegisterType(&ntype);
}
bool node_geo_simulation_input_pair_with_output(bNode *simulation_input_node,
Author
Member

Right now you can easily cause an ambiguous situation by e.g. duplicating a sim input node. The duplicate then has the same output_node_id so it's paired with the same output node But that doesn't make much sense because which input node then defines the initial state?

We should also make sure the input node's output_node_id is cleared when duplicating the node, copy&pasting, etc. I think the storage copyfunc should be able to take care of that. Then any new input node is unpaired by default.

Right now you can easily cause an ambiguous situation by e.g. duplicating a sim input node. The duplicate then has the same `output_node_id` so it's paired with the same output node But that doesn't make much sense because which input node then defines the initial state? We should also make sure the input node's `output_node_id` is cleared when duplicating the node, copy&pasting, etc. I think the storage `copyfunc` should be able to take care of that. Then any new input node is unpaired by default.
Jacques Lucke approved these changes 2023-04-05 14:55:15 +02:00
@ -207,2 +185,4 @@
nodeRegisterType(&ntype);
}
bool node_geo_simulation_input_pair_with_output(bNode *simulation_input_node,
Member

I think short- to mid-term we should disallow copying only a simulation input or output node. For the most part, the ui should treat those as one.

I think the copy operator should take care of updating the output_node_id. It should be set to zero in other cases. We just have to make sure that we don't clear it when making the copy for the depsgraph (or when copying the entire node tree).

I'm find with solving all of that after this patch is committed.

I think short- to mid-term we should disallow copying only a simulation input or output node. For the most part, the ui should treat those as one. I think the copy operator should take care of updating the `output_node_id`. It should be set to zero in other cases. We just have to make sure that we don't clear it when making the copy for the depsgraph (or when copying the entire node tree). I'm find with solving all of that after this patch is committed.
Lukas Tönne added 4 commits 2023-04-05 16:01:33 +02:00
Lukas Tönne merged commit 98bc439e47 into geometry-nodes-simulation 2023-04-05 16:20:55 +02:00
Lukas Tönne deleted branch geometry-nodes-simulation-add-pair 2023-04-05 16:20:56 +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#106557
No description provided.