code nodes

2 node types in the code category.

script#

aliases: python js javascript python_sop

Arbitrary JS over the whole geometry with the engine library (builder, math, noise, BVH, manifold, lib.run any node). Return a Geometry.

The body of async function(geo, inputs, lib). geo is a writable clone of input 0; inputs[i] are the raw inputs. lib: Geometry, GeoBuilder, math, topology, random, noise, KDTree, TriBVH, manifold, merge(geos), run(type, params, ...inputs), warn(msg), frame, time, fps, timeInc (reading them makes the script re-cook per frame). Graph vars by their bare name, read-only, as in "=expr" params (e.g. lib.run('box', { size: height })). Must return a Geometry (e.g. return geo;).

Inputs

#namedescription
0geomain input (optional) (optional)
1input1extra input (optional)
2input2extra input (optional)
3input3extra input (optional)

Parameters

nametypedefaultrangedescription
codecode"return geo;"async function body; may use await lib.run(...)

Example

{
  "type": "script",
  "params": {
    "code": "const parts = [];\nfor (let i = 0; i < 5; i++) parts.push(await lib.run('box', { size: [1, i + 1, 1], center: [i * 1.5, (i + 1) / 2, 0] }));\nreturn lib.merge(parts);"
  }
}

wrangle#

aliases: attribwrangle pointwrangle primwrangle vex snippet attribute_wrangle

Run a JS snippet per point / prim / vertex (or once) with VEX-style @attributes. The universal escape hatch.

Code runs once per element with @attr bound to the current element. Read/write any attribute: @P, @N, @Cd, @pscale, @orient... New attributes are created on write (type from prefix f@ i@ v@ p@ u@ s@, a known name, or the first assigned value). Vectors are arrays with .x/.y/.z accessors; use add/sub/mul/div/dot/cross/normalize/length/lerp/fit (no operator overloading). Element numbers: @ptnum @primnum @vtxnum @numpt @numprim @elemnum. Groups: @group_name = 1. Time: @Frame @Time @TimeInc (the wrangle re-cooks per frame only when the code uses them). Read graph vars by bare name or with ch("lift"). Graph vars are read-only. Inputs 1-3 are readable with point(1, "P", i), nearpoint(1, pos), xyzdist(1, pos)… Create/delete with addpoint, addprim, removepoint, removeprim (applied after the run). See help topic "wrangle" for the full function list.

Inputs

#namedescription
0geogeometry to process
1input1extra input readable as 1 (optional)
2input2extra input readable as 2 (optional)
3input3extra input readable as 3 (optional)

Parameters

nametypedefaultrangedescription
codecode""JS statements. Start directly with the code, for example "@P.y += noise(@P) * 0.2; @Cd = [@P.y, 0.5, 1];"
aliases: snippet vexpression vex
run_overenum"points"element class to iterate
options: points prims vertices detail
aliases: class runover
groupgroup""only run on elements in this group (point, prim or vertex pattern, matching run_over)

Example

{
  "type": "wrangle",
  "params": {
    "run_over": "points",
    "code": "@P.y += sin(@P.x * 4) * 0.2;\n@Cd = hsv2rgb(fit(@P.y, -0.2, 0.2, 0.6, 0), 0.7, 1);"
  }
}

Used in the examples asset_cleanup, blob_creature, cloth_drape, eroded_valley, forest_on_terrain, fracture_settle, growth, guided_town, image_cutouts, lsystem_tree, market_street, ocean_surface, rail_along_curve, scifi_panel, sparks, terrain_tiles, town_from_lots, unreal_landscape, worn_machine.