3
$\begingroup$

I have an imported mesh and camera is positioned to look at this mesh object. Is there a way to get distance from camera to every vertex(point) in mesh object.

P. S. I want to perform this action in a script, but a link to a User Interface action that performs what I desire wold be helpful too.

$\endgroup$

1 Answer 1

4
$\begingroup$

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)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .