BIKINI VEX
Docs文档 Manual手册 Developer开发者 VEX GitHub

VEX Wrangle

Wrangle 节点里的 VEX 风格脚本:按元素跑一遍,读写几何属性。语法接近 Houdini VEX,但类型、前缀和内置函数以本构建为准。

A VEX-style language inside the Wrangle node. The script runs once per element and reads/writes geometry attributes. Syntax is close to Houdini VEX; types, prefixes, and builtins follow this build.

概述Overview

把 Wrangle 加到 Geometry Nodes。面板里选 Run Over 域(点 / 边 / 面 / 角 / 曲线 / 实例),写代码后按 Ctrl+Enter 才编译执行。改代码不会自动重算。

Add a Wrangle node in Geometry Nodes. Pick a Run Over domain (point / edge / face / corner / curve / instance). Press Ctrl+Enter to compile. Editing the text does not auto-evaluate.

v@P += v@N * 0.1;
f@pscale = length(v@P);
报错格式是 L行号: 短中文,下面跟代码片段和 ^。编辑器里错误有波浪线;光标在 {}()[] 上会高亮配对括号。
Errors look like L12: … with a snippet and caret. The editor draws squiggles and highlights matching {}()[].

数据类型Data types

类型Type含义Meaning字面量Literal
bool真 / 假(也可用 boolean)true / false (boolean works too)true false
int整数Integer0 -3
float标量Scalar0.5
vector2二维 (x, y)2D (x, y){1, 0}
vector三维 (x, y, z)3D (x, y, z){0, 1, 0}
vector4四维 (x, y, z, w) — 不是四元数4D (x, y, z, w) — not a quaternion{0, 0, 0, 1}
colorrgba{1, 0, 0, 1}
rotation四元数,存储 xyzw。关键字也可用 quaternion / quatQuaternion stored xyzw. Keywords quaternion / quat also work{0, 0, 0, 1}
matrix22×2 列主序,wrangle only2×2 column-major, wrangle-onlymatrix2()
matrix33×3 列主序,wrangle only3×3 column-major, wrangle-onlymatrix3()
matrix4×4 列主序4×4 column-majorident()
string字符串String"hello"
ray射线命中结果,由 raycast / raycastall 返回。用 rayishit(r) / rayhitpos(r) / rayhitnormal(r) / rayhitdist(r) 读取该手柄。全部命中:ray hits[] = raycastall(...),再 hits[i] 或 foreach (ray r; hits)Ray-hit record from raycast / raycastall. Read that handle with rayishit(r) / rayhitpos(r) / rayhitnormal(r) / rayhitdist(r). All hits: ray hits[] = raycastall(...), then hits[i] or foreach (ray r; hits)raycast(…)
int[] …数组:int[] float[] vector[] string[] matrix[] ray[]Arrays: int[] float[] vector[] string[] matrix[] ray[]array(1, 2, 3)

vector4 和 rotation 不是同一种东西:属性分别存成 Float4 和 Quaternion。

vector4 and rotation are different types: attributes store Float4 vs Quaternion.

int n = 0;
float s = 1.5;
vector p = {0, 1, 0};
rotation q = {0, 0, 0, 1};
int[] xs = array(1, 2, 3);
float ys[] = array(0.1, 0.2);

常量与保留名Constants and reserved names

PI 是 float 常量 3.14159265,可以直接写在表达式里。

PI is the float constant 3.14159265, write it directly in an expression.

下面这些名字不能用作变量、foreach 变量、函数或参数的名字:PI、Position、Rotation、Scale、Normal、Radius、ID、add。大小写必须完全一致才算,所以 vector position、float pi 仍然可以用。属性名不受影响:f@add 照常可用。

These names cannot be used for a variable, foreach variable, function or parameter: PI, Position, Rotation, Scale, Normal, Radius, ID, add. Only the exact spelling is reserved, vector position and float pi still work. Attribute names are not affected: f@add works as before.

float a = 2 * PI * f@t;
v@P += set(cos(a), sin(a), 0);
// float PI = 3.14;      错误 / error: reserved name
// vector Position = v@P; 错误 / error: reserved name

属性Attributes

用前缀 @ 读写当前元素的属性。没写前缀的裸名字(如 P)也会当属性。

Prefix @ reads/writes the current element’s attribute. A bare name such as P is also an attribute.

前缀Prefix类型Type
f@float
i@int
b@bool
u@vector2
v@vector
q@vector4
c@color
r@rotation
s@string
2@ / 3@matrix2 / matrix3wrangle only
4@ / m@matrix 4×4
i[]@ f[]@ s[]@
u[]@ v[]@ q[]@ c[]@ r[]@
2[]@ 3[]@ 4[]@
arrayswrangle only

数组的元素保持声明的类型:vector2 xs[] / u[]@name 的元素是 vector2,vector4 / color / rotation 数组(q[]@ c[]@ r[]@)的元素有四个分量,matrix2 / matrix3 / matrix 数组(2[]@ 3[]@ 4[]@)的元素是对应阶数的矩阵。电子表格按同样的形状显示:(x, y)、(x, y, z, w)、[2×2]、[3×3]。format("{}", x) 也会打印完整的值:vector4 打印四个分量,矩阵按列打印成 {{a, b}, {c, d}},数组打印成 [a, b, c]。

The items of an array keep the declared type: vector2 xs[] / u[]@name hold vector2 items, vector4 / color / rotation arrays (q[]@ c[]@ r[]@) hold four components, and matrix2 / matrix3 / matrix arrays (2[]@ 3[]@ 4[]@) hold matrices of that size. The spreadsheet shows them in the same shape: (x, y), (x, y, z, w), [2×2], [3×3]. format("{}", x) prints the whole value as well: four components for a vector4, a matrix by columns as {{a, b}, {c, d}}, and an array as [a, b, c].

数组属性的长度。数组没有长度上限,局部数组(vector vs[])和存成属性的数组(v[]@name)都一样。属性本身每个元素有 120 个数的空间(120 个 int / float、60 个 vector2、40 个 vector、30 个 vector4 / color / rotation / matrix2、13 个 matrix3、7 个 matrix);更长的数组会自动接着存进隐藏的伴随属性 .name.wrangle_array.1、.2……,读取、append、point(0, "name", i) 采样时都会拼回完整的数组,数组变短后多余的伴随属性会被删除。电子表格显示数组的前一段,并在末尾标出总长度,例如 … (1000)。伴随属性在所有元素上都占空间,所以只有个别元素特别长时会比较费内存。字符串数组仍然受单个属性空间的限制,超出时节点会给出提示。

Length of array attributes. Arrays have no length limit, neither local arrays (vector vs[]) nor arrays stored as an attribute (v[]@name). The attribute itself has room for 120 numbers per element (120 ints or floats, 60 vector2, 40 vectors, 30 vector4 / color / rotation / matrix2, 13 matrix3, 7 matrices); a longer array continues in hidden companion attributes .name.wrangle_array.1, .2, ... Reading it, append and sampling with point(0, "name", i) all give the complete array again, and the companions are removed when the arrays get shorter. The spreadsheet shows the first part of the array and its total length at the end, for example … (1000). The companions take room on every element, so a few very long arrays among many short ones cost memory. String arrays are still limited to the room of the attribute, the node says so when one does not fit.

数组必须存进带 [] 的数组属性:i[]@nbr = pointneighbours(0, i@index);。写成 i@nbr = ...、@nbr = ... 或没有声明的 nbr = ... 会报错,因为它们都是单值属性。

An array has to be stored in an array attribute, written with []: i[]@nbr = pointneighbours(0, i@index);. Writing i@nbr = ..., @nbr = ... or an undeclared nbr = ... is an error, those are all single value attributes.

内置:v@P 位置,v@N 法向,i@index 当前下标(只读),i@id,i@numpt。

Built-ins: v@P position, v@N normal, i@index current index (read-only), i@id, i@numpt.

v@N 就是名为 N 的属性(不是 normal),而且不需要事先存在:几何体上没有名为 N 的属性时,读取它得到的就是 Blender 为当前运行域计算的法向(和 Normal 节点一致)——点上是顶点法向,面上是面法向,面拐角上是拐角法向,边上取两端点法向的中间值;曲线上是曲线法向。采样也一样:point(0, "N", i)、edge(0, "N", i)、face(0, "N", i)、corner(0, "N", i) 在任何运行域里都返回那个域的法向。只读 v@N 不会新建属性;写入 v@N(例如 v@N = -v@N;)会创建名字就叫 N 的属性,初始值是计算出的法向,之后读到的就是这个属性。v@normal 同理,对应名为 normal 的属性。

v@N is the attribute named N (not normal), and it does not have to exist: when the geometry has no attribute called N, reading it gives the normal Blender computes for the domain the node runs over (the same as the Normal node) — the vertex normal on points, the face normal on faces, the corner normal on face corners, between the normals of its two points on edges, and the curve normal on curves. Sampling works the same way: point(0, "N", i), edge(0, "N", i), face(0, "N", i) and corner(0, "N", i) return the normal of that domain whichever domain the node runs over. Only reading v@N creates no attribute; writing it (for example v@N = -v@N;) creates an attribute named exactly N, starting from the computed normals, and later reads return that attribute. v@normal works the same way for an attribute named normal.

CPU 与 GPU。Geometry Wrangle 的同一段代码有两条执行路径:解释器(CPU)和编译成 GLSL 的计算着色器(GPU)。只有带循环的程序才会尝试 GPU(直线代码在 CPU 上更快),两条路径的结果一致。GPU 上可以用:所有类型的局部数组及数组函数(append、insert、removeindex、removevalue、slice、find、unique、min / max / sum、sort、union / subtract / intersect)、矩阵属性(2@ 3@ 4@)、@ptnum 等元素编号,以及用 point() / edge() / face() / corner() 采样别的域或别的几何输入(point(1, "P", i))。GPU 上一个局部数组能放 120 个 int / float、60 个 vector2、40 个 vector、30 个 vector4 / matrix2、13 个 matrix3 或 7 个 matrix;数组一旦超过这个容量,节点会自动改用 CPU 重新计算,结果完整不丢项。下面这些仍然只在 CPU 上运行:数组属性和字符串属性、addpoint / addprim / delete_geometry、射线与邻近查询、几何输入编号不是常数的采样,以及被采样的属性不存在或类型与用法不符的情况。用环境变量 BLENDER_VEX_PROFILE=1 启动 Blender 时,控制台会打印 VEX_GPU ran / failed / skipped 及原因;BLENDER_VEX_NO_GPU=1 强制使用 CPU。后台模式(blender -b)下只有在主线程上求值时才使用 GPU,其余情况用 CPU。

CPU and GPU. The Geometry Wrangle has two ways to run the same code: the interpreter (CPU) and a compute shader compiled to GLSL (GPU). The GPU is only tried for programs with loops (straight-line code is faster on the CPU), and both give the same result. On the GPU you can use: local arrays of every type and the array functions (append, insert, removeindex, removevalue, slice, find, unique, min / max / sum, sort, union / subtract / intersect), matrix attributes (2@ 3@ 4@), the element numbers such as @ptnum, and sampling another domain or another geometry input with point() / edge() / face() / corner() (point(1, "P", i)). One local array on the GPU holds 120 ints or floats, 60 vector2, 40 vectors, 30 vector4 or matrix2, 13 matrix3, or 7 matrices; as soon as an array needs more, the node runs on the CPU instead and the result is complete. These still run on the CPU only: array and string attributes, addpoint / addprim / delete_geometry, ray and proximity queries, sampling with a geometry input number that is not a constant, and sampling an attribute that does not exist or whose type does not match how it is used. Starting Blender with the environment variable BLENDER_VEX_PROFILE=1 prints VEX_GPU ran / failed / skipped with the reason to the console; BLENDER_VEX_NO_GPU=1 forces the CPU. In background mode (blender -b) the GPU is only used when the node is evaluated on the main thread, otherwise the CPU.

元素编号(只读):@ptnum、@edgenum、@facenum、@cornernum、@curvenum 给出当前元素所属的点 / 边 / 面 / 拐角 / 曲线。在自己的运行域上就是当前下标(跑在面上时 @facenum == i@index);在别的域上取相连的那个元素,有多个时取第一个,没有时为 -1。@primnum 和 @vtxnum 是 @facenum 和 @cornernum 的别名。

Element numbers (read-only): @ptnum, @edgenum, @facenum, @cornernum and @curvenum give the point / edge / face / corner / curve the current element belongs to. On the domain the node runs over it is the current index (@facenum == i@index when running over faces); on another domain it is the connected element, the first one when there are several and -1 when there is none. @primnum and @vtxnum are aliases of @facenum and @cornernum.

运行域Run over@ptnum@edgenum@facenum@cornernum@curvenum
Point自身itself第一条相连的边first connected edge第一个相连的面first connected face第一个拐角first corner所在曲线(曲线的点)its curve (curve points)
Edge第一个端点first end point自身itself第一个相连的面first connected face第一个拐角first corner-1
Face第一个拐角的点point of its first corner第一个拐角的边edge of its first corner自身itself第一个拐角first corner-1
Corner拐角的点its point拐角的边its edge所在的面its face自身itself-1
Curve曲线的第一个点first point of the curve-1-1-1自身itself

面板参数用 chf("amp")、chi、chv、chr 等;Compile 会按名字生成通道插座。色带用 chramp("ramp", t),曲线用 chcurve("curve", value)(按 value 的类型生成 float / vector / color 曲线)。节点上的 Docs 按钮会打开函数清单。

Panel parameters use chf("amp"), chi, chv, chr, … Compile creates sockets from those names. Color ramps use chramp("ramp", t) and curves chcurve("curve", value) (a float / vector / color curve, picked from the type of value). The Docs button on the node opens the function list.

运算符Operators

+ - * / %   ** 幂power   == != < > <= >=   && || !

= += -= *= /=   ++ -- (int)(int)   ?:

成员:.x .y .z .w 或 .r .g .b .a。下标列主序:v[0] 等于 v.x;m[i] 是第 i 列;m[i][j] 可读可写。

Members: .x .y .z .w or .r .g .b .a. Indexing is column-major: v[0] is v.x; m[i] is column i; m[i][j] is readable and writable.

*

  • rotation * rotation → 四元数合成quaternion product
  • rotation * vector → 用 q 旋转 v(请左乘)rotate v by q (left-multiply)
  • matrix * matrix → 同阶矩阵积same-order product
  • matrix * vector → 完整线性乘法,不是 affine transform_pointfull linear mul, not affine transform_point
  • float * matrix → 对角缩放(4×4 的 m44 保持 1)diagonal scale (4×4 m44 stays 1)
  • vector * matrix → 只警告,按行向量计算。请写成 M * vwarning only; computed as a row vector. Write M * v
v@P = ident() * vector4(v@P.x, v@P.y, v@P.z, 1); // 维度必须对齐
v@P = transform_point(v@P, M);                   // 含平移的点变换
v@P = q * v@P;                                   // 四元数旋转

控制流Control flow

if / else / else if

C 风格。没有单独的 elif 关键字:else 后面再跟一条 if。

C-style. There is no elif keyword: write else if.

if (f@pscale > 1) {
    v@Cd = {1, 0, 0, 1};
} else if (f@pscale > 0.5) {
    v@Cd = {1, 1, 0, 1};
} else {
    v@Cd = {0, 0, 1, 1};
}

for

int s = 0;
for (int i = 0; i < npoints(); i++) {
    s += 1;
    if (i == 10) { break; }
    if (i == 5) { continue; }
}

foreach

遍历数组。两种写法:只要元素,或同时要下标。

Iterate an array. Element only, or index plus element.

int[] pts = nearestpoints(0, 8, "k");
vector sum = {0, 0, 0};
foreach (int pt; pts) {
    sum += point(0, "P", pt);
}
foreach (int i; int pt; pts) {
    // i = 0 .. len(pts)-1
}

while

int i = 0;
while (i < 7) {
    i++;
}

三元:float a = (x > 0) ? x : 0; 注释:// 与 /* */。

Ternary: float a = (x > 0) ? x : 0; Comments: // and /* */.

自定义函数User functions

可以在 wrangle 顶层定义函数,支持递归。返回类型写在名字前面;无返回值用 void。可选关键字 function。

Define functions at the top of the wrangle. Recursion is allowed. Return type comes before the name; use void for no return. The function keyword is optional.

int fib(int n) {
    if (n <= 1) {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

void bump(float s) {
    v@P += v@N * s;
}

i@id = fib(8);
bump(0.05);

编译与求值Compile & eval

  • Ctrl+Enter 才编译。编辑过程中不会自动跑。
  • Ctrl+Enter compiles. The script does not run while you type.
  • 编辑器按数据类型上色,基本和节点插座的颜色一致:float 黄绿(不用插座的灰色,免得和普通文字混淆)、int 绿、bool 粉、vector 蓝、color 金、string 橙、rotation 紫、matrix 玫红。v@P、f@mask 这样的属性和 vector、float 这样的类型名用同一种颜色,一眼就能看出类型。
  • The editor colors code by data type, mostly matching the socket colors: float lime (not the gray of its socket, which looks like plain text), int green, bool pink, vector blue, color gold, string orange, rotation violet, matrix rose. Attributes such as v@P or f@mask share the color of the type names vector or float, so the type is visible at a glance.
  • 出错的位置用红色波浪线标出,该行的行号也变红;黄色是警告(例如变量名疑似拼错)。少了 ; 或 ) 时,标记在缺少它的那一行末尾,而不是下一行。输入时光标下方的列表是可用的函数,按 Tab 补全第一项;输入 函数名( 后显示这个函数的参数和类型。
  • The place of an error gets a red wavy underline and a red line number; yellow is a warning (for example a name that looks misspelled). A missing ; or ) is marked at the end of the line that misses it, not on the next line. While typing, the list below the caret shows the available functions and Tab completes the first one; after name( it shows the parameters of that function and their types.
  • 在节点上输入代码时视图不会被锁住:鼠标在代码框上滚轮滚动代码(Shift+滚轮横向滚动,Ctrl+滚轮调整字号);鼠标在代码框外滚轮缩放节点视图,按住中键拖动平移视图,输入状态保持不变。Tab 补全,Ctrl+/ 注释,Alt+E 打开独立编辑窗口。
  • Typing in the node does not lock the view: with the pointer over the code the wheel scrolls the code (Shift+wheel scrolls sideways, Ctrl+wheel changes the text size); anywhere else the wheel zooms the node view and a middle mouse drag pans it, and you keep typing. Tab completes, Ctrl+/ comments, Alt+E opens the separate editor window.
  • addprim / setattribute / delete_geometry 以及两参数的 addpoint(geo, pos) 的 geo 永远当 0;写非 0 会提示并改成 0。addpoint(geo, index, type) 的 geo 是源几何输入。addprim 的 type 为 edge / face / curve(可用 polyline、poly、spline 等别名,或 0/1/2)。
  • geo on addprim / setattribute / delete_geometry and 2-arg addpoint(geo, pos) is always 0; a non-zero value is forced to 0 with a notice. addpoint(geo, index, type) uses geo as the source input. addprim type is edge / face / curve (aliases like polyline, poly, spline, or 0/1/2).
  • 2@ / 3@ 和数组属性是 wrangle only,不写回 Blender 原生层。
  • 2@ / 3@ and array attributes are wrangle-only; they are not native Blender attributes.