Jump to content

Blender function in VEX (snap)

Recommended Posts

I came across this interesting Blender tutorial where the author uses math nodes to recreate a 2D ngon shape similar like using COPs to create patterns and I decided to give a try translating it in Houdini using VEX and I got stuck trying to find a VEX function similar to a Blender one which I couldn't so I am sure that a smarter brain could easily solve this exercise so here is the challenge. 

This is the tutorial link and around 12:30 he explains the theory behind the function 'snap' and at 23:57 it shows it in action:

Which basically it returns the closest value that B[] is from A. I hope it makes sense.

Any tips?

Thanks in advance.

Edited by Mzigaib
Link to comment
Share on other sites


VEX has a lot of sample functions for these, for example sample_circle_uniform:


So you can do something like this in a Detail Wrangle:

int count = chi("count");
for ( int i = 0; i < count; ++i )
    float u = float ( i ) / count;
    vector2 uv = set ( u, 1 );
    vector2 p = sample_circle_uniform ( uv );
    vector pnew = set ( p.x, 0, p.y );
    addpoint ( 0, pnew );


Link to comment
Share on other sites

That's really cool! One more trick learned.

My goal was to try to use the formula on the tutorial to draw in COPs the ngon shape with VEX.

After a lot of trying and error I could translate the formula to COPs using VOP COP and the hardest part was to trying to figure out the different functions that are created specific for Blender.
That being said I was able to find what the 'snap' function and what it does is simple:

float snap = floor(val1/val2)*val2;

So after that was just a matter of assemble the formula in the inline VOP inside the VOP COP and vua-la, It worked!

So I did:

distance = length($uv);
angle = fit(atan2($uv.y,$uv.x),-$PI,$PI,0,2*$PI);
float val = (2*PI)/$n;
float snap = floor($angle/val)*val;
float x = $r*sin((($n-2)*PI)/(2*$n)) / sin(PI-($angle-snap)-((($n-2)*PI)/(2*$n)));

$dist = x > distance;

Being the $uv the x/y pixel positions offseted to the center and $n being the ngon sides and $r being the radius parameters.

Thanks for the trick tip that was really useful.

Edited by Mzigaib
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...