diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-06-13 17:51:01 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-06-13 17:51:01 +0400 |
commit | f173ff531c327f48e8613a28be9850a68ee07627 (patch) | |
tree | 17162d9ff8ca4ad378267ff7398b71351a07c2d3 /release/scripts/modules/bpy_extras | |
parent | a17d8a29bbb41e836aa4576136e094bc852390c6 (diff) |
handy function for getting the 2d camera coords for a worldspace location.
bpy_extras.object_utils.world_to_camera(scene, obj, coord)
Diffstat (limited to 'release/scripts/modules/bpy_extras')
-rw-r--r-- | release/scripts/modules/bpy_extras/object_utils.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/release/scripts/modules/bpy_extras/object_utils.py b/release/scripts/modules/bpy_extras/object_utils.py index 9381f49d408..7d7654d01af 100644 --- a/release/scripts/modules/bpy_extras/object_utils.py +++ b/release/scripts/modules/bpy_extras/object_utils.py @@ -25,6 +25,7 @@ __all__ = ( "object_add_grid_scale", "object_add_grid_scale_apply_operator", "object_image_guess", + "world_to_camera", ) @@ -265,3 +266,40 @@ def object_image_guess(obj, bm=None): 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)) |