# How can I compare normals in AN with python

I am trying to create a script that assigns a differente rotation to polys according to their normals but I can´t figure out how to do it. This is my script so far.

import math
import mathutils

mirando_arriba = mathutils.Vector((0.0000,0.0000,1.0000))
mirando_derecha = mathutils.Vector((0.0000,1.0000,0.0000))
mirando_izquierda = mathutils.Vector((-0.0000,-1.0000,-0.0000))
mirando_abajo = mathutils.Vector((0.0000,0.0000,-1.0000))
mirando_fondo = mathutils.Vector((1.0000,0.0000,0.0000))
mirando_trasera = mathutils.Vector((-1.0000,0.0000,-0.0000))
if (Normal == mirando_arriba):
result = rotateY
TextBlock = "Mira hacia arriba"
elif (Normal ==  mirando_derecha):
result = rotateX
TextBlock = "Mira hacia derecha"
elif (Normal ==  mirando_abajo):
result = rotateX
TextBlock = "Mira hacia abajo"
elif (Normal ==  mirando_fondo):
result = rotateX
TextBlock = "Mira hacia fondo"
elif (Normal ==  mirando_izquierda):
result = rotateX
TextBlock = "Mira hacia izquierda"
elif (Normal ==  mirando_trasera):
result = rotateY
TextBlock = "Mira hacia trasera"
else:
result = rotateY
TextBlock = "No mira"

• By polys do you mean faces? Or mesh objects? Commented Dec 31, 2015 at 12:07
• Yes I mean faces Commented Jan 2, 2016 at 11:30

The only issue I see here is comparing vectors that are float vectors and that can give false negatives.

For vector comparison it's better to use "distance", rather than "==". (Also, in blender precision appears to go up to 6, so use at most < 0.00001)

However, that means in your code:

if (Normal - mirando_arriba).length_squared < 0.0001:
result = rotateY
TextBlock = "Mira hacia arriba"


and same for the rest.

• For better peformance you can also use (...).length_squared. (This is faster because it doesn't have to calculate the square root of the distance internally.) blender.org/api/blender_python_api_2_76_2/… Commented Jan 6, 2016 at 11:42