distance between an object and the viewport

is there a built-in function for calculating the distance between an object and the view-port?
i want to get the closest object:


niko
.

don’t think this exist at all

but you can between the camera and an object !

in viewport you see everything in the world !
and don’t think there is any reference for the zoon in/out function for viewport

ni-ko-o-kin, you mean the distance from a point in the 3D space to the viewport surface? try this:


rv3d = bpy.data.screens["Default"].areas[4].spaces.active.region_3d #typically: bpy.context.space_data.region_3d
distance_to_origin = rv3d.view_matrix.to_translation().length #combine this view_matrix with the location of your point!

not following here !

where is located the viewport compared to the world scene ?

how can you calculate the distance to viewport ?

>>> bpy.data.screens[‘Default’].areas[2].spaces.active.region_3d.view_matrix.to_translation().length
14.988801002502441

what exactly does this function return? there is no change to the return value when i change my location in the 3d-space in the viewport, i get the same value: 14.988…

niko
.

Try zoom, you will find the difference.
Look also at the few functions from bpy_extras.view3d_utils module (you will find the folder of bpy_extras Python package in 2.59\scripts\modules of the standard Blender installation)

Great! Now it works. I had the wrong context.

bpy_extras.view3d_utils: know them already. very useful functions!

thx a lot for the hints!

niko
.