# Get edge slope/angle of depression in mesh

I’m looking to write a script that is able to produce each edge’s change in height between its two vertices so that I can export the data to an external file. I am using a model of this mesh as a reference while designing brackets which can be assembled to make the final product.

I have the angles between faces, as well as the edge length. The difficulty is with finding the angle, or slope of the edges in the mesh.

If I were to do this manually, I would calculate the height difference between two vertices and take the inverse sine of the y difference over the edge length.

## 1 Answer

In maths, a slope is a proportion between a horizontal and vertical components of a distance:

$$m=\frac{y_2-y_1}{x_2-x_1}$$

In 3D world I imagine you want to replace $$y$$ coordinate ("height" in 2D world) with $$z$$ (Blender coordinate system is "z-up"), but then what is the horizontal distance? It's simply the hypotenuse of a triangle, defined by other sides ("legs") $$x$$ and $$y$$, so a Pythagorean theorem can be used to calculate the horizontal distance $$d$$:

$$d = \sqrt{ x^2 + y^2}$$

but the python's Vector class allows you to simply use "swizzling" to easily get just the $$xy$$ components as a new vetor and then read the length of that vector:

from bpy import context as C
from math import atan, degrees
ob = C.object
me = ob.data
v1, v2 = me.vertices  # I have just 2 vertices in the test mesh
offset = v2.co - v1.co  # order matters - it affects the sign of the slope!
slope = offset.z / offset.xy.length
angle_radians = atan(slope)
angle_degrees = degrees(angle_radians)

print(f"{v1.co=}\n{v2.co=}\n{offset=}\n{slope=}\n{angle_radians=}\n{angle_degrees=}")


Example result:

v1.co=Vector((0.0, 0.0, 0.0))
v2.co=Vector((1.0, 0.0, 1.0))
offset=Vector((1.0, 0.0, 1.0))
slope=1.0
angle_radians=0.7853981633974483
angle_degrees=45.0

• You need to be in Object mode to run this script, or more specifically, if you're in Edit Mode, you will read the last state of the mesh in Object Mode - to read the most up-to-date state in Edit Mode, you need to use bmesh
• in Object Mode you can access any edge's vertices like so: v1i = me.edges[i].vertices[0] and v2i = me.edges[i].vertices[1], where i is the index of the edge, and the results are not vertex objects but just their indices, so that later you need to read the actual vertex data as v1 = me.vertices[v1i].
• using bmesh reading vertices is easier because you can access them from the edge directly.