3D coordinates to 2D coordinates in camera coordinates/pixels not screen region

Hi all,

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)

Many thanks

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…

maybe you can calculate it yourself, similar to how it is shown here:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ (see The Projection Matrix)

BGE has the required function apprently:
http://blenderartists.org/forum/archive/index.php/t-246525.html

yes, exactly.

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.

thanks for pointing me into that direction.

I found this on web:

http://www.scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/perspective-projection-matrix/

Maybe helpful to construct a view projection matrix to calculate what you need.

Perhaps you have a look at blender’s own code, it definitely got this stuff somewhere, and it should be exposed to python.