Hello,
I want to draw a bounding box using my blender addon. For that bounding box I use the blf module to draw the dimensions of the box onto the axes.
Somehow, this only works for the default resolution (1920, 1080). As soon as I change this resolution (which I need to do) the text is at the wrong position and distorted. (the left picture is the correct projection for default resolution, the others are the wrong projection and distorted):
I first calculate the world position of the text, then try to project it into camera space using world_to_camera_view(). This works for the viewport and for the default resolution but not as soon as the resolution is changed. Note that I use GPU Module for rendering.
I looked up how to use world_to_camera_view in several examples (i.e. this one) and I try to do (almost) the same:
scene = bpy.context.scene
cam = scene.camera
# point in 3d scene
vec_point_3d = mathutils.Vector((point_3d[0], point_3d[1], point_3d[2], 1))
# mapping the 3d point into the camera space
obj = bpy.data.objects['Origin'] # empty representing the origin of my bounding box
matrix_world = obj.matrix_world
co_2d = object_utils.world_to_camera_view(scene, cam, matrix_world @ vec_point_3d)
render_scale = scene.render.resolution_percentage / 100
render_size = (int(scene.render.resolution_x * render_scale),
int(scene.render.resolution_y * render_scale))
x_pos = round(co_2d.x * render_size[0]) #/ render_size[0]
y_pos = round(co_2d.y * render_size[1]) #/ render_size[1]
# draw text
point_2d = [x_pos, y_pos]
blf.position(font_id, point_2d[0], point_2d[1], 0)
blf.draw(font_id, text)
I don’t understand why this happens because as the source code shows, the camera matrix is multiplied onto the text coordinates, so the projection should work?
Does anyone have an idea why this might happen? I’d be thankful for any help!