VEX snippets

Assorted VEX snippets to use with Wrangle SOP

To test a point group membership:

int ismember = inpointgroup(0, "group1", @ptnum);

Returns a boolean value whether a point is a member of specified group.

To assign a point to a group:

if(@P.x>5) @group_mygroup = 1;

If points’ position on X axis is more than 5, assign the point to the “mygroup”.

More about groups within Wrangle SOP see here.

To create a point at the center of each primitive:

addpoint(0, @P);
removeprim(0, @primnum, 1);

Delete point/primitive by attribute:

if(@P.x > 0) removeprim(0, @primnum, 1);

Remove a primitive if it’s position on X axis is larger than 0. Use removepoint to work with points. 

If – else conditional:

int ingroup = inpointgroup(0, "group1", @ptnum);
if (ingroup == 1) @Cd = set(1,0,0); 
else @Cd = set(0,0,0);

Performs a test whether a current point is a member of group1. if true, set point color to red. Else, set point color to black.

Point clouds – remove points by number of neighbors in radius

int pcloud = pcopen(0, "P", @P, 2, 10); 
if (pcnumfound(pcloud) > 3) removepoint(0,@ptnum);

pcopen function opens a pointcloud that is wired to the first input. then it iterates over the point positions and evaluates how many points (up to 10) are there in the radius of 2 units, for any given point. If the currently tested point has more than 3 neighbors found in the specified radius, the point will be removed.

Point clouds – group points by number of connections

if (neighbourcount (0, @ptnum) > 3)
{setpointgroup (0, "grouped", @ptnum, 1);}

A point becomes a member of “grouped” if number of connections for the point is > 3

Point clouds and arrays – isolate overlapping points

int near[] = pcfind(0, "P", @P, 0.0001, 2);
if(len(near) > 1) setpointgroup(0, "double", @ptnum, 1);

pcfind is putting all points within a radius of i.e. 0.0001 in an array called “near”. len is counting how many points were found in the “near” array. and if it found more than one/itself then it will be put into a pointgroup called “double”.

Basic use of noise function

@P += noise(@Time);

Get bounding box values

vector bbox = getbbox_size(0);
vector bbox_max = getbbox_max(0);
vector bbox_min = getbbox_min(0);
vector bbox_center = getbbox_center(0);   

Each of these functions return geometry bounding box values – it’s size, maximum and minimum values in each axis and the geometry center point. Result is always vector as it is a point in 3d space but you can isolate individual axis easily too:

float xsize = getbbox_size(0).x;
float y_max = getbbox_max(0).y;
float z_min = getbbox_min(0).z; 

Draw normalized linear ramp based on bounding box values:

float y_max = getbbox_max(0).y;
float y_min = getbbox_min(0).y;
@ramp = fit(@P.y, min, max, 0, 1); 

This is an extremely handy snippet – in this case it will return a lowest and highest point of your object represented as a linear gradient from 0 to 1. Very useful for all sorts of effects, procedural modelling and texturing.

Compute distance from the object center:

vector center = getbbox_center(0);
@dist = distance(@P, center);

Given the current point position @P and bbox center position (center), the distance function computes distance between those two points. The result is a gradient spreading from the center of geometry to it’s edges.

Use ramp parameter to remap an attribute

float remap = fit(@P.y, 127, 166, 0, 1);
@scale = chramp('myRampName', remap);

This snippet will create a ramp parameter called “myRampName” with the @remap attribute as an input. Ramp operates in 0-1 range so the input value should be already in that range. Anything outside will be clamped (@P.y, 127, 166 was a desired range in my example).
With ramp parameter you can further remap an input visually and in non-linear fashion.

For loop in wrangle (running over detail):

float count = 100;
for(int n=0; n<count; n++) addpoint(0, {0,0,0});

This loop repeats itself as long as the condition is true. In this case generates a point in world zero position, one hundred times. This wrangle SOP doesn’t have any input and must be set to Detail mode as there are initially no points or primitives to run over.

Orient template points for copy SOP:

@up = {0,1,0};
@orient = quaternion(maketransform(@N,@up));
vector4 rot_Y = quaternion(radians(ch('Y')),{0,1,0});
@orient = qmultiply(@orient, rot_Y);

This snippet allows random orientation in Y axis for each input point by dialing control sliders. For more sophisticated ways to orient points, read here.

To expand a group over geometry:

Following code lets you select an input group and expand it uniformly by a specified distance.

My example is using point clouds to achieve the result and has a point group called group1 for a group filter in the wrangle SOP.

int pc = pcopen(0, 'P', @P, ch('radius'), chi('maxpts'));
while (pciterate(pc) > 0)
{
int currentpt;
pcimport(pc, 'point.number', currentpt);
setpointgroup(0, 'group1', currentpt, 1);
}

One thought on “VEX snippets

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s