Skip to content

Commit 48929ce

Browse files
committed
Add documentation to InputGroups and fix input recreation
1 parent aa11b39 commit 48929ce

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

api/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def build_tree(builder):
2929
# Clear the node group before building
3030
for node in node_group.nodes:
3131
node_group.nodes.remove(node)
32-
while len(node_group.inputs) > len(signature.parameters):
32+
while len(node_group.inputs) > sum(map(lambda p: len(p.annotation.__annotations__) if issubclass(p.annotation, InputGroup) else 1, list(signature.parameters.values()))):
3333
node_group.inputs.remove(node_group.inputs[-1])
3434
for group_output in node_group.outputs:
3535
node_group.outputs.remove(group_output)

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Advanced Scripting](./api/advanced-scripting.md)
1919
- [Node Groups](./api/advanced-scripting/node-groups.md)
2020
- [Generators](./api/advanced-scripting/generators.md)
21+
- [Input Groups](./api/advanced-scripting/input-groups.md)
2122

2223
# Tutorials
2324

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Input Groups
2+
3+
Some geometry node trees need a lot of arguments.
4+
5+
```python
6+
@tree("Terrain Generator")
7+
def terrain_generator(
8+
width: Float
9+
height: Float
10+
resolution: Int
11+
scale: Float
12+
w: Float
13+
):
14+
...
15+
```
16+
17+
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.
18+
19+
This is where input groups come in. An input group is class that contains properties annotated with valid socket types.
20+
21+
To create an input group, declare a new class that derives from `InputGroup`.
22+
23+
```python
24+
class TerrainInputs(InputGroup):
25+
width: Float
26+
height: Float
27+
resolution: Int
28+
scale: Float
29+
w: Float
30+
```
31+
32+
Then annotate an argument in your tree function with this class.
33+
34+
```python
35+
@tree("Terrain Generator")
36+
def terrain_generator(
37+
inputs: TerrainInputs
38+
):
39+
...
40+
```
41+
42+
This will create a node tree with the exact same structure as the original implementation. The inputs can be accessed with dot notation.
43+
44+
```python
45+
size = combine_xyz(x=input.width, y=input.height)
46+
```
47+
48+
And now passing the inputs through from another function is much simpler.
49+
50+
```python
51+
def point_terrain(
52+
terrain_inputs: TerrainInputs,
53+
radius: Float
54+
):
55+
return terrain_generator(
56+
inputs=terrain_inputs
57+
).mesh_to_points(radius=radius)
58+
```
59+
60+
## Instantiating Input Groups
61+
62+
If you nest calls to tree functions, you can instantiate the `InputGroup` subclass to pass the correct inputs.
63+
64+
```python
65+
def point_terrain():
66+
return terrain_generator(
67+
inputs=TerrainInputs(
68+
width=5,
69+
height=5,
70+
resolution=10,
71+
scale=1,
72+
w=0
73+
)
74+
).mesh_to_points()
75+
```

0 commit comments

Comments
 (0)