The matrix of the position and orientation of the gizmo

Creating my custom gizmo for 3D modeling, I was faced with the task to describe the position of the gizmo in different uses. For example, ACTIVE_ELEMENT, MEDIAN_POINT and so on. Plus it should work in Mesh, Curve, Surface … this is a very voluminous and difficult task for me.

And I was thinking, can I use the default gizmo position and orientation, but turn off the default gizmo display?

As far as I know there is no way to access the current gizmo matrix via the python API unfortunately.
You can use this function to get the gizmo position in most cases (not all) by using the cursor:

def get_cursor_to_sel_pos(self, context):
    ''' Get the position of the gizmo by snapping the cursor to selection '''
    cursor_pos_tmp = context.scene.cursor.location.copy()
    cursor_position = Matrix.Translation(context.scene.cursor.location)
    context.scene.cursor.location = cursor_pos_tmp
    return cursor_position

Then you need to get the gizmo orientation so you can create the matrix.
You can do something like this to get the gizmo orientation for the active object:

cursor_to_sel_pos = self.get_cursor_to_sel_pos(context)
gizmo_matrix = cursor_to_sel_pos @ current_object.matrix_world.to_3x3().to_4x4()

But this won’t help in certain cases (if the transform orientation is set to Normal for example)
There is a nice trick , you can create a custom transform orientation and get its matrix:

# Create a custom transform orientation
custom_orientation_name = "transform_orient_temp"
bpy.ops.transform.create_orientation(name=custom_orientation_name, use=True, overwrite=True)
# This is the rotation matrix of the gizmo
custom_orientation_matrix = bpy.context.scene.transform_orientation_slots[0].custom_orientation.matrix.to_4x4()

Now using the position you got with the cursor and the orientation you have above you can finally create the current gizmo matrix like this:

gizmo_matrix = Matrix.Translation(cursor_position.to_translation()) @ custom_orientation_matrix

And you have to create checks for all cases of Transform Orientation settings, Pivot Point settings, Object/Edit mode etc. But yeah, it’s a lot of work for somthing that is already there, but it’s just not exposed in the python API.

Here are two other topics about it:
How do I get the transform manipulator’s vector values?
Getting the current Gizmo matrix with Python in 2.8


thank you very much for your answer, now I will understand. Because for me it was a stumbling block)

1 Like