# Can I get the orientation matrix in real time?

I need to calculate the normal orientation matrix for the tool. There is a way to get a custom orientation matrix

``````bpy.context.scene.transform_orientation_slots.custom_orientation.matrix.to_4x4()
``````

Is there a similar method for getting any orientation matrix ? (GLOBAL, LOCAL, NORMAL, GIMBAL, VIEW, CURSOR)

I will say in advance that I am not satisfied with creating a custom orientation, since I need to get a realtime matrix

Global is probably just an identity matrix. The local matrix can be read by Python rather easily – for example `object.matrix` will usually give you what you want. The “normal” matrix can be generated by mathutils- a bmesh face has the property `BMface.normal` and from the `BM.verts[...].co` you can figure out the median point, or any other kind of geometric center with a bit of math (there’s tutorials on the internet for finding different kind of centers, so I’ll leave it to you to find the one you want). Then once you have the location of the center of the face and the normal, you can construct a matrix with `mathutils.Matrix.Translation(val_translation) @ mathutils.Matrix.Rotation(val_rotation)` (the order matters). Be aware that this is relative to the object’s origin so you’ll want to apply the object’s local matrix first if it isn’t at 0 translation/rotation/scale.

For the cursor and view you can use a similar method, although I don’t actually know how to query the view vector, it’s probably easy to do. The 3D cursor IIRC is a property of the scene. And I really don’t know how to get a matrix for gimbal space. Heck, I could be wrong about some of the above, but it should be enough to get you started at least.

as far as I understand from your answer, you can’t get the orientation matrix directly from the blender. I think this is a big omission, because for any tool you need the matrix of the selected(orientation and position). For my addon, I described all the position and orientation matrices for the mesh, except for the normal matrix. I still don’t understand the logic of getting the median vector chosen vertex normal.
I can’t even figure out exactly what the normal orientation blender is counting on. From the vertex normal, or from link faces normal? Moreover, we still have different types of objects, and for each of them we need to calculate all the matrices differently. That’s the problem, all these matrices are calculated on the blender side, it would be cool to get them.

If it is not possible to get the matrices from the blender, could you help me with the normal orientation matrix? To begin with, where should we take normals? With the selected vertices or faces normal link?

``````#object matrix
ob = position_matrix()
#print(bpy.context.scene.transform_orientation_slots.custom_orientation.matrix.to_4x4())
orig_loc, orig_rot, orig_scale = ob.decompose()
orig_scale_mat = Matrix.Scale(orig_scale, 4, (1, 0, 0)) @ Matrix.Scale(orig_scale, 4, (0, 1, 0)) @ Matrix.Scale(orig_scale, 4, (0, 0, 1))

uniques = bpy.context.objects_in_mode_unique_data

# Selected Object(EDIT_MODE)
bms = {}
for obj in uniques:
bms[obj] = bmesh.from_edit_mesh(obj.data)

# Selected Vertex
verts = []
for obj in bms:
verts.extend([v for v in bms[obj].verts if v.select])

# Get Faces Normal
faces_normal = []
for vert in verts:

verts_normal = []
for obj in bms:
verts_normal.extend([v.normal for v in bms[obj].verts if v.select])

to_Sort = del_duplicate(verts_normal)
# I don't think I'm calculating the median vector correctly
mw = Vector(sum(to_Sort, Vector()) / len(to_Sort)).normalized().to_track_quat('Z', 'X').to_matrix().to_4x4()

matrix_new = orig_rot.to_matrix().to_4x4() @ orig_scale_mat.normalized() @ mw``````