Skip to content

Commit 86023a2

Browse files
committed
Add simulation decorator
1 parent bdf703c commit 86023a2

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

api/static/simulation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import inspect
2+
import typing
3+
4+
class SimulationInput:
5+
class DeltaTime: pass
6+
class ElapsedTime: pass
7+
8+
def simulation(block: typing.Callable[typing.Any, 'Geometry']):
9+
"""
10+
Create a simulation input/output block.
11+
12+
> Only available in the `geometry-node-simulation` branch of Blender 3.5.
13+
"""
14+
def wrapped(geometry: 'Geometry', *args, **kwargs):
15+
from geometry_script import simulation_input, simulation_output
16+
simulation_in = simulation_input(geometry=geometry)
17+
signature = inspect.signature(block)
18+
for key, value in signature.parameters.items():
19+
match value.annotation:
20+
case SimulationInput.DeltaTime:
21+
kwargs[key] = simulation_in.delta_time
22+
case SimulationInput.ElapsedTime:
23+
kwargs[key] = simulation_in.elapsed_time
24+
return simulation_output(geometry=block(simulation_in.geometry, *args, **kwargs)).geometry
25+
return wrapped

api/tree.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .static.expression import *
1212
from .static.input_group import *
1313
from .static.sample_mode import *
14+
from .static.simulation import *
1415
from .arrange import _arrange
1516

1617
def _as_iterable(x):

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Attributes](./api/advanced-scripting/attributes.md)
2323
- [Boolean Math](./api/advanced-scripting/boolean-math.md)
2424
- [Drivers](./api/advanced-scripting/drivers.md)
25+
- [Simulation](./api/advanced-scripting/simulation.md)
2526

2627
# Tutorials
2728

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Simulation
2+
3+
> This API is subject to change as future builds of Blender with simulation nodes are released.
4+
5+
The `geometry-nodes-simulation` branch of Blender 3.5 includes support for "simulation nodes".
6+
7+
Using a *Simulation Input* and *Simulation Output* node, you can create effects that change over time.
8+
9+
As a convenience, the `@simulation` decorator is provided to make simulation node blocks easier to create.
10+
11+
```python
12+
@simulation
13+
def move_over_time(
14+
geometry: Geometry, # the first input must be `Geometry`
15+
speed: Float,
16+
dt: SimulationInput.DeltaTime, # Automatically passes the delta time on any argument annotated with `SimulationInput.DeltaTime`.
17+
elapsed: SimulationInput.ElapsedTime, # Automatically passes the elapsed time
18+
) -> Geometry:
19+
return geometry.set_position(
20+
offset=combine_xyz(x=speed)
21+
)
22+
```
23+
24+
Every frame the argument `geometry` will be set to the geometry from the previous frame. This allows the offset to accumulate over time.
25+
26+
The `SimulationInput.DeltaTime`/`SimulationInput.ElapsedTime` types mark arguments that should be given the outputs from the *Simulation Input* node.

0 commit comments

Comments
 (0)