Add enum generation

This commit is contained in:
Carson Katri 2022-11-17 13:07:50 -05:00
commit 828fb8b6da
9 changed files with 226 additions and 40 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View file

@ -121,4 +121,30 @@ The same script without chaining calls is written more verbosely as:
@tree("Cube Tree")
def cube_tree(size: Vector):
return mesh_to_volume(mesh=cube(size=size))
```
### Spanning Multiple Lines
Often times you want each chained calls to be on a separate line. There are a few ways to do this in Python:
1. Newlines around arguments
```python
cube(
size=size
).mesh_to_volume()
```
2. Parentheses
```python
(cube(size=size)
.mesh_to_volume())
```
3. Line continuation
```python
cube(size=size) \
.mesh_to_volume()
```

View file

@ -20,7 +20,47 @@ The general process is:
> Properties and inputs are different types of argument. A property is a value that cannot be connected to a socket. These are typically enums (displayed in the UI as a dropdown), with specific string values expected. Check the documentation for a node to see what the possible values are for a property.
Let's take a look at two nodes as an example.
## Enum Properties
Many nodes have enum properties. For example, the math node lets you choose which operation to perform. You can pass a string to specify the enum case to use. But a safer way to set these values is with the autogenerated enum types. The enums are namespaced to the name of the node in PascalCase:
```python
# Access it by Node.Enum Name.Case
math(operation=Math.Operation.Add)
math(operation=Math.Operation.Subtract)
math(operation='MULTIPLY') # Or manually pass a string
```
Internally, this type is generated as:
```python
import enum
class Math:
class Operation(enum.Enum):
Add = 'ADD'
Subtract = 'SUBTRACT'
Multiply = 'MULTIPLY'
...
...
```
The cases will appear in code completion if you setup an [external editor](../../setup/external-editing.md).
## Duplicate Names
Some nodes use the same input name multiple times. For example, the *Math* node has three inputs named `value`. To specify each value, pass a tuple for the input:
```python
math(operation=Math.Operation.Wrap, value=(0.5, 1, 0)) # Pass all 3
math(operation=Math.Operation.Wrap, value=(0.5, 1)) # Only pass 2/3
math(operation=Math.Operation.Wrap, value=0.5) # Only pass 1/3
```
![](./math_wrap.png)
## Examples
Here are two examples to show how a node maps to a function.
### Cube
@ -72,8 +112,8 @@ size.cube(...)
1. Name `Capture Attribute` -> `capture_attribute`
2. Keyword Arguments
* Properties
* `data_type: Literal['FLOAT', 'INT', 'FLOAT_VECTOR', 'FLOAT_COLOR', 'BYTE_COLOR', 'STRING', 'BOOLEAN', 'FLOAT2', 'INT8']`
* `domain: Literal['POINT', 'EDGE', 'FACE', 'CORNER', 'CURVE', 'INSTANCE']`
* `data_type: CaptureAttribute.DataType`
* `domain: CaptureAttribute.Domain`
* Inputs
* `geometry: Geometry`
* `value: Vector | Float | Color | Bool | Int`
@ -82,7 +122,7 @@ size.cube(...)
The node can now be used as a function:
```python
result = capture_attribute(data_type='BOOLEAN', geometry=cube_geo) # Specify a property and an input
result = capture_attribute(data_type=CaptureAttribute.DataType.Boolean, geometry=cube_geo) # Specify a property and an input
result.geometry # Access the geometry
result.attribute # Access the attribute
```
@ -92,8 +132,8 @@ The generated documentation will show the signature, result type, and [chain syn
#### Signature
```python
capture_attribute(
data_type: Literal['FLOAT', 'INT', 'FLOAT_VECTOR', 'FLOAT_COLOR', 'BYTE_COLOR', 'STRING', 'BOOLEAN', 'FLOAT2', 'INT8'],
domain: Literal['POINT', 'EDGE', 'FACE', 'CORNER', 'CURVE', 'INSTANCE'],
data_type: CaptureAttribute.DataType,
domain: CaptureAttribute.Domain,
geometry: Geometry,
value: Vector | Float | Color | Bool | Int
)

View file

@ -3,9 +3,9 @@
Blender's *Text Editor* leaves a lot to be desired. Writing scripts without code completion can be tough.
Using an external code editor is one way to improve the editing experience.
This guide will show how to setup [Visual Studio Code](https://code.visualstudio.com/) to edit Geometry Scripts. However, the same concepts apply to IDEs.
This guide will show how to setup [Visual Studio Code](https://code.visualstudio.com/) to edit Geometry Scripts. However, the same concepts apply to other IDEs.
> This guide assumes you have already installed Visual Studio Code and setup the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python). If not, please follow the setup guides for those tools before continuing.
> This guide assumes you have already installed Visual Studio Code and setup the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python). If not, please setup those tools before continuing.
## Code Completion
When the Geometry Script add-on starts, it generates a Python typeshed file that can be used to provide code completion.

View file

@ -26,7 +26,7 @@ def city_builder(
return geometry
```
Run the script to create the tree, then add a *Geometry Nodes* modifier to your curve object and select the *City Builger* node group.
Run the script to create the tree, then add a *Geometry Nodes* modifier to your curve object and select the *City Builder* node group.
## Buildings
Let's start with the buildings. We'll distribute points on a grid with `size_x` and `size_y`.