Simple Vector Maths
Blender uses vectors , so this becomes a simple matter of subtracing the global camera location vector from each global vertex location vector. (links)
The global location of the camera is camera_object.matrix_world.translation
. The global location of a vertex is its local coordinate (For vertex j in mesh me
: me.vertices[j].co
) , multiplied by the matrix world of the mesh object. (ob.matrix_world
)
Test script, prints the camdist list to system console. camdist[i]
is the global distance of me.vertices[i]
from the camera.
import bpy
context = bpy.context
scene = context.scene
cam = scene.camera
ob = context.active_object
if cam and ob and ob.type == 'MESH':
camloc = cam.matrix_world.translation
mw = ob.matrix_world
me = ob.data # mesh
camdists = [(mw * v.co - camloc).length for v in me.vertices]
print(camdists)