[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [57433] trunk/blender/release/scripts/ modules/bpy_extras/object_utils.py: handy function for getting the 2d camera coords for a worldspace location.
Brecht Van Lommel
brechtvanlommel at pandora.be
Thu Jun 13 16:06:34 CEST 2013
Nitpick about the terminology used here. "Camera space" usually refers
to the coordinates when transformed by the camera matrix but without
any perspective transformation applied, so I would say "2D camera
space coords" here. OpenGL and Renderman would call the result of this
function Normalized Device Coordinates, though I'm not sure if there
is a common name for the space, maybe "NDC space".
On Thu, Jun 13, 2013 at 3:51 PM, Campbell Barton <ideasman42 at gmail.com> wrote:
> Revision: 57433
> http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=57433
> Author: campbellbarton
> Date: 2013-06-13 13:51:01 +0000 (Thu, 13 Jun 2013)
> Log Message:
> -----------
> handy function for getting the 2d camera coords for a worldspace location.
>
> bpy_extras.object_utils.world_to_camera(scene, obj, coord)
>
> Modified Paths:
> --------------
> trunk/blender/release/scripts/modules/bpy_extras/object_utils.py
>
> Modified: trunk/blender/release/scripts/modules/bpy_extras/object_utils.py
> ===================================================================
> --- trunk/blender/release/scripts/modules/bpy_extras/object_utils.py 2013-06-13 13:09:32 UTC (rev 57432)
> +++ trunk/blender/release/scripts/modules/bpy_extras/object_utils.py 2013-06-13 13:51:01 UTC (rev 57433)
> @@ -25,6 +25,7 @@
> "object_add_grid_scale",
> "object_add_grid_scale_apply_operator",
> "object_image_guess",
> + "world_to_camera",
> )
>
>
> @@ -265,3 +266,40 @@
> if image is not None:
> return image
> return None
> +
> +
> +def world_to_camera(scene, obj, coord):
> + """
> + Returns the 2d camera space coords for a 3d point.
> +
> + Where (0, 0) is the bottom left and (1, 1) is the top right of the camera frame.
> + values outside 0-1 are also supported.
> +
> + Takes shift-x/y, lens angle and sensor size into account
> + as well as perspective/ortho projections.
> +
> + :arg scene: Scene to use for frame size.
> + :type scene: :class:`bpy.types.Scene`
> + :arg obj: Camera object.
> + :type obj: :class:`bpy.types.Object`
> + :arg coord: World space location.
> + :type coord: :class:`mathutils.Vector`
> + :return: normalized 2d vector.
> + :rtype: :class:`mathutils.Vector`
> + """
> +
> + co_local = obj.matrix_world.normalized().inverted() * coord
> +
> + camera = obj.data
> + frame = [-v for v in camera.view_frame(scene=scene)[:3]]
> + if camera.type != 'ORTHO':
> + frame = [(v / -v.z) * co_local.z for v in frame]
> +
> + min_x, max_x = frame[1].x, frame[2].x
> + min_y, max_y = frame[0].y, frame[1].y
> +
> + x = (co_local.x - min_x) / (max_x - min_x)
> + y = (co_local.y - min_y) / (max_y - min_y)
> +
> + from mathutils import Vector
> + return Vector((x, y))
>
> _______________________________________________
> Bf-blender-cvs mailing list
> Bf-blender-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-blender-cvs
More information about the Bf-committers
mailing list