From 48929cea93dfa004eca8437ad3a1db73f1b884de Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Fri, 18 Nov 2022 18:23:51 -0500 Subject: [PATCH] Add documentation to InputGroups and fix input recreation --- api/tree.py | 2 +- book/src/SUMMARY.md | 1 + .../api/advanced-scripting/input-groups.md | 75 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 book/src/api/advanced-scripting/input-groups.md diff --git a/api/tree.py b/api/tree.py index a95551c..a316cbb 100644 --- a/api/tree.py +++ b/api/tree.py @@ -29,7 +29,7 @@ def tree(name): # Clear the node group before building for node in node_group.nodes: node_group.nodes.remove(node) - while len(node_group.inputs) > len(signature.parameters): + while len(node_group.inputs) > sum(map(lambda p: len(p.annotation.__annotations__) if issubclass(p.annotation, InputGroup) else 1, list(signature.parameters.values()))): node_group.inputs.remove(node_group.inputs[-1]) for group_output in node_group.outputs: node_group.outputs.remove(group_output) diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 412635a..115b612 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -18,6 +18,7 @@ - [Advanced Scripting](./api/advanced-scripting.md) - [Node Groups](./api/advanced-scripting/node-groups.md) - [Generators](./api/advanced-scripting/generators.md) + - [Input Groups](./api/advanced-scripting/input-groups.md) # Tutorials diff --git a/book/src/api/advanced-scripting/input-groups.md b/book/src/api/advanced-scripting/input-groups.md new file mode 100644 index 0000000..f5667f5 --- /dev/null +++ b/book/src/api/advanced-scripting/input-groups.md @@ -0,0 +1,75 @@ +# Input Groups + +Some geometry node trees need a lot of arguments. + +```python +@tree("Terrain Generator") +def terrain_generator( + width: Float + height: Float + resolution: Int + scale: Float + w: Float +): + ... +``` + +There are a couple of problems with this. Firstly, the function signature is getting long. This can make it harder to visually parse the script. And, if we want to use the same arguments in another tree and pass them through to `terrain`, we need to make sure to keep everything up to date. + +This is where input groups come in. An input group is class that contains properties annotated with valid socket types. + +To create an input group, declare a new class that derives from `InputGroup`. + +```python +class TerrainInputs(InputGroup): + width: Float + height: Float + resolution: Int + scale: Float + w: Float +``` + +Then annotate an argument in your tree function with this class. + +```python +@tree("Terrain Generator") +def terrain_generator( + inputs: TerrainInputs +): + ... +``` + +This will create a node tree with the exact same structure as the original implementation. The inputs can be accessed with dot notation. + +```python +size = combine_xyz(x=input.width, y=input.height) +``` + +And now passing the inputs through from another function is much simpler. + +```python +def point_terrain( + terrain_inputs: TerrainInputs, + radius: Float +): + return terrain_generator( + inputs=terrain_inputs + ).mesh_to_points(radius=radius) +``` + +## Instantiating Input Groups + +If you nest calls to tree functions, you can instantiate the `InputGroup` subclass to pass the correct inputs. + +```python +def point_terrain(): + return terrain_generator( + inputs=TerrainInputs( + width=5, + height=5, + resolution=10, + scale=1, + w=0 + ) + ).mesh_to_points() +``` \ No newline at end of file