# Rotate a plane to align with the Ground, while using Vertices to determine its Rotation

This is sort of an open ended question. I am trying to write a python script that rotates a plane to align with the ground. I have to use the plane itself instead of its origin point. I have accomplished one rotation in one direction so far using some trig stuff. But I think there has to be a better way to do this. Please let me know if you know of a solution other wise I'll just keep coding this one lol.

https://youtu.be/HHq-yMF397Q

basically what I've done is flatten it out like you would if you where looking at the scene from one direction and then just worked with the triangles. I figure if I just work my way around it (Z and Y directions next) I can eventually get it flat.

I will able to piece together a much more elegant solution, once I understood how matrixes work a little better. I'm still a little confused by this code but It does what it needs to. https://www.youtube.com/watch?v=cWcAwfF6zgM

import bpy
from mathutils import Matrix, Vector

def scale_from_vector(v):
mat = Matrix.Identity(4)
for i in range(3):
mat[i][i] = v[i]
return mat

o2 = bpy.data.objects['Cubeface002']
face  = o2.data.polygons[0]

loc_dst, rot_dst, scale_dst = o2.matrix_world.decompose()

q = face.normal.rotation_difference((0, 0, 1))

o2.matrix_world = (
Matrix.Translation(loc_dst) @
q.to_matrix().to_4x4() @
scale_from_vector(scale_dst)
)