I would like to export the verts of a 3D object as 2D coordinates relative to the camera region (in pixels or floats from 0,1 so it’s resolution independent) - to use in 2D compositing.
Did some searching and, found the utility function location_3d_to_region_2d that almost does this, but returns pixels relative to the whole viewport region not to the camera “letterbox”. Is there a way to compute the 2D coordinates relative to the camera/resolution? Maybe by substracting the Passepartout region? I am not sure where to get that from. Also it seems that the sensor/gate mode (vertical,horizontal) influences the behavior of the Camera viewport.
from bpy_extras.view3d_utils import location_3d_to_region_2d
my_region = C.screen.areas[2].spaces[0].region_3d #hardcoded region/pane of my screen
location_3d_to_region_2d(bpy.context.region, my_region ,bpy.data.objects["Cube"].data.vertices[1].co)
so you wanna basically know at which pixel coordinate a 3d location will be in the rendered image…
i found the two functions Region.view2d.region_to_view() and Region.view2d.view_to_region() but they return bad numbers (either always 12000 or nan), not sure if they are what you need for this calculation though…
looks like bge.types.KX_Camera.getScreenPosition does what I want, returns a float from 0. to 1. in pixel coordinates.
the only drawbacks: it takes an Object not a Vector as an argument, and it has to run from the engine … but not a big deal.