Skip to content

Commit 467ae71

Browse files
committed
Add curve support
1 parent 1ffed55 commit 467ae71

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

api/node_mapper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .state import State
77
from .types import *
88
from .static.input_group import InputGroup
9+
from .static.curve import Curve
910
from ..absolute_path import absolute_path
1011

1112
class OutputsList(dict):
@@ -28,6 +29,13 @@ def build(_primary_arg=None, **kwargs):
2829
argname = prop.identifier.lower().replace(' ', '_')
2930
if argname in kwargs:
3031
value = kwargs[argname]
32+
if isinstance(value, list) and len(value) > 0 and isinstance(value[0], Curve):
33+
for i, curve in enumerate(value):
34+
curve.apply(getattr(node, prop.identifier).curves[i])
35+
continue
36+
if isinstance(value, Curve):
37+
value.apply(getattr(node, prop.identifier).curves[0])
38+
continue
3139
if isinstance(value, enum.Enum):
3240
value = value.value
3341
setattr(node, prop.identifier, value)

api/static/curve.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List
2+
import enum
3+
4+
class HandleType(enum.Enum):
5+
AUTO = 'AUTO'
6+
VECTOR = 'VECTOR'
7+
AUTO_CLAMPED = 'AUTO_CLAMPED'
8+
9+
class Point:
10+
"""
11+
A single point on a curve
12+
"""
13+
14+
x: float
15+
y: float
16+
handle_type: HandleType
17+
18+
def __init__(self, x: float, y: float, handle_type: HandleType = HandleType.AUTO):
19+
self.x = x
20+
self.y = y
21+
self.handle_type = handle_type
22+
23+
class Curve:
24+
"""
25+
A class that represents a curve.
26+
27+
Create a curve from a set of `Point`s.
28+
```python
29+
my_curve = Curve(
30+
Point(0, 0, Handle.AUTO_CLAMPED),
31+
Point(0.2, 0.3, Handle.AUTO),
32+
Point(1, 1, Handle.VECTOR)
33+
)
34+
```
35+
"""
36+
37+
points: List[Point]
38+
39+
def __init__(self, *points: Point):
40+
if len(points) == 1 and isinstance(points[0], list):
41+
self.points = points[0]
42+
else:
43+
self.points = list(points)
44+
45+
def apply(self, curve):
46+
"""
47+
Apply the points to a curve object.
48+
"""
49+
for i, point in enumerate(self.points):
50+
if len(curve.points) > i:
51+
curve.points[i].location = (point.x, point.y)
52+
curve.points[i].handle_type = point.handle_type.value
53+
else:
54+
curve.points.new(point.x, point.y).handle_type = point.handle_type.value

api/tree.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .types import *
99
from .node_mapper import *
1010
from .static.attribute import *
11+
from .static.curve import *
1112
from .static.expression import *
1213
from .static.input_group import *
1314
from .static.sample_mode import *

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [Input Groups](./api/advanced-scripting/input-groups.md)
2222
- [Attributes](./api/advanced-scripting/attributes.md)
2323
- [Boolean Math](./api/advanced-scripting/boolean-math.md)
24+
- [Curves](./api/advanced-scripting/curves.md)
2425
- [Drivers](./api/advanced-scripting/drivers.md)
2526
- [Simulation](./api/advanced-scripting/simulation.md)
2627

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Curves
2+
3+
Some nodes, such as *Float Curve* take a curve as a property. You can create a curve with the `Curve` class.
4+
5+
```python
6+
float_curve(
7+
mapping=Curve(
8+
Point(0, 0),
9+
Point(0.5, 0.25),
10+
Point(1, 1, HandleType.VECTOR), # Optionally specify a handle type, such as `AUTO`, `VECTOR`, or `AUTO_CLAMPED`.
11+
)
12+
)
13+
```
14+
15+
![](./float_curve.png)
16+
17+
You can also pass the points as a list to `Curve`.
18+
19+
```python
20+
points = [Point(0, 0), Point(1, 1)]
21+
float_curve(
22+
mapping=Curve(points)
23+
)
24+
```
25+
26+
If a node has multiple curve properties, such as the *Vector Curves* node, pass a list of curves to the node.
27+
28+
```python
29+
vector_curves(
30+
mapping=[x_curve, y_curve, z_curve]
31+
)
32+
```
35.6 KB
Loading

0 commit comments

Comments
 (0)