Align to face normal vector

Help me rotation new object by normal a face other object. I have code that works half. The axes at the selected polygon and the new object must be the same     • The MathViz addon is very useful to visualize matrices, quats and vecs. Is the normal axis in image above in local or global coordinates? Oct 25 '17 at 4:14
• I do not understand ... The manipulator in normal mode i.imgur.com/PnOisQi.png I need the local axis of the new object to look the same as this normal Oct 25 '17 at 8:51
• if you encounter some issue with 2.8 replace every "*" with "@" wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API
– DB3D
Apr 24 '19 at 0:17

Align to vector using Vector.rotation_difference(v)

Altered the script from this answer to find the quaternion rotation difference between the local z axis of the edit mesh and the normal of its selected (active) face.

Run script with target object in edit mode, one face selected and active. An object in scene named "Cube.001" will be located at face centre and oriented by face normal.

import bpy
from mathutils import Matrix, Vector
import bmesh
context = bpy.context
obj = context.edit_object
mw = obj.matrix_world.copy()
bm = bmesh.from_edit_mesh(obj.data)
# for this example jmake a face active
face = bm.select_history.active
o = face.calc_center_median()
# calculate the axis dif in local coords

axis_src = face.normal
# local z-axis
axis_dst = Vector((0, 0, 1))

matrix_rotate = mw.to_3x3()
matrix_rotate = matrix_rotate * axis_src.rotation_difference(axis_dst).to_matrix()
matrix_translation = Matrix.Translation(mw * o) #

obj2 = context.scene.objects.get("Cube.001")
obj2.matrix_world = matrix_translation * matrix_rotate.to_4x4() Result of script. Moved cube in edit mode, showing local coords.

• Sumptuously! That's what I needed. Many thanks Oct 25 '17 at 10:23
• Look at the top polygon does not work. i.imgur.com/JqYfL7F.gifv Oct 25 '17 at 19:59
• @ВладКиндюшов whoops, should have checked that. Anyhoo, think I have it now. Oct 26 '17 at 6:32
• I add ".inverted()" in to "axis_src.rotation_difference(axis_dst).to_matrix()" line 18 i.imgur.com/lSksTnW.gifv Oct 27 '17 at 19:40
• Think using BMFace.calc_tangent will give us the other alignment vector (that the normal axes are showing) to align object x axis to. (can make the rotation matrix with Matrix(norm, tangent, norm.cross(tangent)). Apologies. Oct 28 '17 at 0:07

Alexander Nedovizin solved this problem

import bpy
from mathutils import Matrix, Vector
import bmesh
context = bpy.context
obj = context.edit_object
mw = obj.matrix_world.copy()
bm = bmesh.from_edit_mesh(obj.data)
face = bm.select_history.active
o = face.calc_center_median()

axis_src = face.normal
axis_src2 = face.calc_tangent_edge()
axis_dst = Vector((0, 0, 1))
axis_dst2 = Vector((0, 1, 0))

vec2 = axis_src * obj.matrix_world.inverted()
matrix_rotate = axis_dst.rotation_difference(vec2).to_matrix().to_4x4()

vec1 = axis_src2 * obj.matrix_world.inverted()
axis_dst2 = axis_dst2*matrix_rotate.inverted()
mat_tmp = axis_dst2.rotation_difference(vec1).to_matrix().to_4x4()
matrix_rotate = mat_tmp*matrix_rotate
matrix_translation = Matrix.Translation(mw * o) #

obj2 = context.scene.objects.get("Cube.001")
obj2.matrix_world = matrix_translation * matrix_rotate.to_4x4()