Copy shape from closest primitive (VEX)

Example: Can be used to interpolate hairs after hair generation. Run over primitives in an attribute wrangle node. Hairs to be interpolated go to the first stream, guides to second. Amount of points on the primitives need to match.

Can be also used as a base script for clump setup by removing the matrix translation part at the end of the script and mixing the output with the original groom by curveu attribute.

Logic around script:

  • Run over primitives

  • Find nearest primitive on second stream with xyzdist() VEX function

  • Loop over the nearest primitive points and store them in a vector array

  • Set iterated primitive point positions to the nearest primitive point positions

  • Move primitive back to its original position with a transform matrix

//Find nearest primitive on the second stream

int nearprim;
vector nearuv;

float dist = xyzdist(1,@P,nearprim,nearuv);

//Get the nearest primitives points positions to an array

int nearpts[] = primpoints(1,nearprim);
vector positions[];

for ( int i=0; i<len(nearpts); i++ ){
        vector pos = point(1,"P",nearpts[i]);
        append(positions,pos);
}

//Set iterated primitives point positions to the nearest primitives point positions. 

int pts[] = primpoints(0,@primnum);
vector rootP = point(0,"P",pts[0]);

for ( int i=0; i<len(pts); i++ ){
        setpointattrib(0,"P",pts[i],positions[i]);
}


//Move the primitive back with a translation matrix.
// get a vector from the first point of the iterated primitive and the first point of the near primitive

vector move = rootP - positions[0];

matrix xform = ident();
translate(xform,move);

for ( int i=0; i<len(pts); i++ ){
        vector pos = positions[i];
        vector newpos = pos * xform;
        setpointattrib(0,"P",pts[i],newpos);
}
Previous
Previous

Tangle hairs by scattered points (VEX)

Next
Next

Find the average position of a point cloud (VEX)