# Average Normal

## Recommended Posts

Hello,

I'm having troubles getting around how vex works sometimes.

I would like to do something like
(Detail)

```vector avgN = 0;

for (int i=0; i < nprimitives(0) ; i++) {
avgN = avgN + prim(0, "N", i);
}

avgN = avgN / nprimitives(0);```

(plz ignore my name)

Edited by Drunkenpanda
##### Share on other sites

Heyo, there are a few ways to solve this that I can think of at the top of my head.

1. Use the "avg" vex function. Just make an array of all the normal attribute values and then plug that array of vectors into the "avg" function and it will give you the average value of all the vectors in the array.
2. Use the "attribute promote" node. Just add the "attribute promote" node and set it to promote the "N" attribute from primitives to detail and set the promotion mode to "average" (this is the default) and boom, you should now have it all averaged out and as a detail attribute.

##### Share on other sites

but it does rely on the N attribute to exist on primitives already (usually they are on points or vertices)

so you could also do either

```vector avgN = 0;

for (int i=0; i < npoints(0) ; i++) {
avgN = avgN + vector(point(0, "N", i));
}

avgN = avgN / npoints(0);```

or directly fetch the primitive normal with the native function for that:

```vector avgN = 0;

for (int i=0; i < nprimitives(0) ; i++) {
avgN = avgN + prim_normal(0, i, {0.5, 0.5, 0.5});
}

avgN = avgN / nprimitives(0);```

but like underscoreus said, the easiest way is probably to use an attributepromote SOP (combined with a facet SOP to generate the N attribute for your points)

Edited by acey195

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×