# Average Normal

## Recommended Posts

Posted (edited)

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
Posted (edited)

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

## Create an account

Register a new account

×
• Donations