minor edits to world_to_camera() utility function, include Z so you can tell if the points in font of the camera and avoid divide by zero.

This commit is contained in:
Campbell Barton 2013-06-13 14:07:36 +00:00
parent f9b06060c2
commit 93ba74c20a

@ -287,13 +287,18 @@ def world_to_camera(scene, obj, coord):
:return: normalized 2d vector.
:rtype: :class:`mathutils.Vector`
"""
from mathutils import Vector
co_local = obj.matrix_world.normalized().inverted() * coord
z = -co_local.z
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]
if z == 0.0:
return Vector((0.5, 0.5, 0.0))
else:
frame = [(v / (v.z / 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
@ -301,5 +306,4 @@ def world_to_camera(scene, obj, coord):
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))
return Vector((x, y, z))