Refactor simulation API to use simulation_zone decorator

This commit is contained in:
Carson Katri 2023-10-09 19:05:08 -04:00 • committed by Carson Katri
commit 63a7516905
4 changed files with 76 additions and 47 deletions

View file

@ -1,26 +1,22 @@
# Simulation
> This API is subject to change as future builds of Blender with simulation nodes are released.
The `geometry-nodes-simulation` branch of Blender 3.5 includes support for "simulation nodes".
Blender 3.6 includes simulation nodes.
Using a *Simulation Input* and *Simulation Output* node, you can create effects that change over time.
As a convenience, the `@simulation` decorator is provided to make simulation node blocks easier to create.
As a convenience, the `@simulation_zone` decorator is provided to make simulation node blocks easier to create.
```python
@simulation
def move_over_time(
geometry: Geometry, # the first input must be `Geometry`
speed: Float,
dt: SimulationInput.DeltaTime, # Automatically passes the delta time on any argument annotated with `SimulationInput.DeltaTime`.
elapsed: SimulationInput.ElapsedTime, # Automatically passes the elapsed time
) -> Geometry:
return geometry.set_position(
offset=combine_xyz(x=speed)
)
from geometry_script import *
@tree
def test_sim(geometry: Geometry):
@simulation_zone
def my_sim(delta_time, geometry: Geometry, value: Float):
return (geometry, value)
return my_sim(geometry, 0.26).value
```
Every frame the argument `geometry` will be set to the geometry from the previous frame. This allows the offset to accumulate over time.
The `SimulationInput.DeltaTime`/`SimulationInput.ElapsedTime` types mark arguments that should be given the outputs from the *Simulation Input* node.
The first argument should always be `delta_time`. Any other arguments must also be returned as a tuple with their modified values.
Each frame, the result from the previous frame is passed into the zone's inputs.
The initial call to `my_sim` in `test_sim` provides the initial values for the simulation.