Skip to content

Commit 5fbc7d1

Browse files
authored
Merge pull request #4 from carson-katri/named-outputs
Support Named Outputs
2 parents 7d539ba + bfe5aae commit 5fbc7d1

File tree

6 files changed

+46
-10
lines changed

6 files changed

+46
-10
lines changed

.github/workflows/github_pages.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
- main
77
pull_request:
88

9+
concurrency: preview-${{ github.ref }}
10+
911
jobs:
1012
deploy:
1113
runs-on: ubuntu-20.04
@@ -28,3 +30,9 @@ jobs:
2830
with:
2931
github_token: ${{ secrets.GITHUB_TOKEN }}
3032
publish_dir: ./book/book
33+
34+
- name: Deploy preview
35+
uses: rossjrw/pr-preview-action@v1
36+
if: ${{ github.ref != 'refs/heads/main' }}
37+
with:
38+
source-dir: ./book/book

api/static/attribute.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ def __call__(self, *args, **kwargs):
4444
"""
4545
Creates a "Named Attribute" node with the correct arguments passed, and returns the "Attribute" socket.
4646
"""
47-
from geometry_script import named_attribute
48-
return named_attribute(data_type=self.data_type, name=self.name, *args, **kwargs).attribute
47+
from geometry_script import named_attribute, Type
48+
result = named_attribute(data_type=self.data_type, name=self.name, *args, **kwargs)
49+
# Handle Blender 3.5+, which includes an `exists` result.
50+
if isinstance(result, Type):
51+
return result
52+
else:
53+
return result.attribute
4954

5055
def exists(self, *args, **kwargs):
5156
"""
5257
Creates a "Named Attribute" node with the correct arguments passed, and returns the "Exists" socket.
58+
59+
> Only available in Blender 3.5+
5360
"""
5461
from geometry_script import named_attribute
5562
return named_attribute(data_type=self.data_type, name=self.name, *args, **kwargs).exists

api/tree.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,20 @@ def validate_param(param):
9898
outputs = builder(**builder_inputs)
9999

100100
# Create the output sockets
101-
for i, result in enumerate(_as_iterable(outputs)):
102-
if not issubclass(type(result), Type):
103-
result = Type(value=result)
104-
# raise Exception(f"Return value '{result}' is not a valid 'Type' subclass.")
105-
node_group.outputs.new(result.socket_type, 'Result')
106-
link = node_group.links.new(result._socket, group_output_node.inputs[i])
101+
if isinstance(outputs, dict):
102+
# Use a dict to name each return value
103+
for i, (k, v) in enumerate(outputs.items()):
104+
if not issubclass(type(v), Type):
105+
v = Type(value=v)
106+
node_group.outputs.new(v.socket_type, k)
107+
node_group.links.new(v._socket, group_output_node.inputs[i])
108+
else:
109+
for i, result in enumerate(_as_iterable(outputs)):
110+
if not issubclass(type(result), Type):
111+
result = Type(value=result)
112+
# raise Exception(f"Return value '{result}' is not a valid 'Type' subclass.")
113+
node_group.outputs.new(result.socket_type, 'Result')
114+
node_group.links.new(result._socket, group_output_node.inputs[i])
107115

108116
_arrange(node_group)
109117

api/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def z(self):
175175

176176
def capture(self, value, **kwargs):
177177
data_type = socket_type_to_data_type(value._socket.type)
178-
geometry, attribute = self.capture_attribute(data_type=data_type, value=value, **kwargs)
179-
return geometry, attribute
178+
res = self.capture_attribute(data_type=data_type, value=value, **kwargs)
179+
return res.geometry, res.attribute
180180
def transfer(self, attribute, **kwargs):
181181
data_type = socket_type_to_data_type(attribute._socket.type)
182182
return self.transfer_attribute(data_type=data_type, attribute=attribute, **kwargs)
Loading

book/src/api/basics/tree-functions.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def cube_tree():
4141

4242
![](./cube_tree_int.png)
4343

44+
By default, each output is named 'Result'. To customize the name, return a dictionary.
45+
46+
```python
47+
@tree("Cube Tree")
48+
def cube_tree():
49+
return {
50+
"My Cube": cube(),
51+
"Scale Constant": 5
52+
}
53+
```
54+
55+
![](./cube_tree_named_outputs.png)
56+
4457
## Group Input
4558
All arguments in a tree function must be annotated with a valid socket type. These types are provided by Geometry Script, and are not equivalent to Python's built-in types. Let's add a size argument to our Cube Tree.
4659

0 commit comments

Comments
 (0)