# VEX cookbook

Examples and suggestions for programming in VEX.

## VEX examples ¶

To see the VEX implementation of a VEX-based operator:

2. Open the tile menu and choose Type Properties.

Other example VEX source code is available on the web.

## Constant Color COP ¶

You could write a “Constant Color COP” function this way:

```cop
constant(vector clr=1; float alpha=1)
{
R = clr.r;
G = clr.g;
B = clr.b;
A = alpha;
}
```

However, you should use the high-efficiency assign function whenever possible.

```cop
constant(vector clr=1; float alpha=1)
{
assign(R, G, B, clr);
A = alpha;
}
```

## User noise functions ¶

This example defines two user functions which generate normalized multiple octaves of Perlin noise.

```float
myfperlin3d(vector pos; int octaves; float rough)
{
int    i;
float    nval;
vector    pp;
float    result, sum, scale;
// Because parameters are passed by reference, we don't really
// want to modify the parameters value.  Therefore, we copy it
// to a temporary variable.
pp = pos;
scale = 1;
sum = 0;
result = 0;
for (i = 0; i <= octaves; i++)
{
result += noise(pp);
sum += scale;
pp *= 2;
scale *= rough;
}
return result / sum;
}
float
myfperlin1d(float pos; int octaves; float rough)
{
int    i;
float    nval;
float    pp;
float    result, sum, scale;
// Because parameters are passed by reference, we don't really
// want to modify the parameters value.  Therefore, we copy it
// to a temporary variable.
pp = pos;
scale = 1;
sum = 0;
result = 0;
for (i = 0; i <= octaves; i++)
{
result += noise(pp);
sum += scale;
pp *= 2;
scale *= rough;
}
return result / sum;
}
```

It is possible to use #define macros to encode the contents of the function and simply call the macro to generate vector or other dimensions of noise.

# VEX

## Language ¶

• Details of VEX syntax, data types, and so on.

• Overview of how to use the VEX language compiler vcc and its pre-processor and pragma statements.

## Next steps ¶

• You can read the contents of primitive/point/vertex groups in VEX as if they were attributes.

• Examples and suggestions for programming in VEX.

• VEX has functions that let you treat edges as unshared per-face half-edges.

• You can use the assert() macro to print information while you are debugging VEX code.

## Reference ¶

• Guide to the different contexts in which you can write VEX programs.