Constant viewport scale of lines using bgl/gpu module


I’ve created a modal operator to draw custom axis, but not sure how to properly implement constant viewport scaling.

At the moment I’m using a view_distance to calculate scale of lines:

    scale = 0.085 * rv3d.view_distance
    positions = [(scale, 0.0, 0.0), (0.0, scale, 0.0), (0.0, 0.0, scale)]

It works fine when axis in the world center (location (0, 0, 0)), but as soon as I move it around scaling became wrong. Code should take into account location of camera/axis but not sure how to implement it properly.

Appreciate any help!

Ok, I found out how to get the real location of viewpoint and calculate real distance between points:

    rv3d =
    view_loc = rv3d.view_matrix.inverted().translation
    pivot_loc = context.collection.ET_pivot_loc
    dist = (view_loc - pivot_loc).magnitude
1 Like