Script wanted: measure distance to a selected point from current camera

Hi,

for rendering z-depth channels I’m looking for a little button script that allow me to select a point (per osnap) on objects and get the distance from the current viewport camera location point to this point. So the z-depth distances for a specific postwork DOF effect could be determinated.

I hope it’s not to difficult and someone could post a script please.

Ciao,
Micha

You can try the following Python script:

import rhinoscriptsyntax as rs

def DistToCamera():
    pt=rs.GetPoint("Pick point on object")
    if not pt: return
    dist=rs.Distance(pt,rs.ViewCamera())
    print "Distance from pick point to camera is {} units".format(dist)
DistToCamera()

I can also do a vb rhinoscript one if you want…

–Mitch

1 Like

Here is a drag-n-drop VB rhinoscript version:

Option Explicit
Rhino.AddStartUpScript Rhino.LastLoadedScriptFile
Rhino.AddAlias "DistToCamera", "! _NoEcho _-Runscript (DistToCamera)"

'Call DistToCamera()
Sub DistToCamera()
	Dim pt,dist,units,prec,msg
	pt = Rhino.GetPoint("Pick point on object")
	If Not IsArray(pt) Then Exit Sub
	dist = Rhino.Distance(pt, Rhino.ViewCamera())
	units = Rhino.UnitSystemName(,, True)
	prec = Rhino.UnitDistanceDisplayPrecision()
	msg = "Distance from pick point to camera is "
	Call Rhino.Print(msg & Cstr(Round(dist, prec)) & " " & units)
End Sub

Hi Mitch,

thank you very much for your super fast reply. :smiley:

I don’t know so much about scripting. I tried to use the script as button script, because I like to collect all my tools in a toolbar easy to transfer to an other Rhino install in the future. So I tried this one, but it run without any interaction. Do you have an idea what I did wrong?. :

-_RunScript (
Option Explicit
Rhino.AddStartUpScript Rhino.LastLoadedScriptFile
Rhino.AddAlias “DistToCamera”, “! _NoEcho _-Runscript (DistToCamera)”

'Call DistToCamera()
Sub DistToCamera()
Dim pt,dist,units,prec,msg
pt = Rhino.GetPoint(“Pick point on object”)
If Not IsArray(pt) Then Exit Sub
dist = Rhino.Distance(pt, Rhino.ViewCamera())
units = Rhino.UnitSystemName(, True)
prec = Rhino.UnitDistanceDisplayPrecision()
msg = "Distance from pick point to camera is "
Call Rhino.Print(msg & Cstr(Round(dist, prec)) & " " & units)
End Sub
)

You need to eliminate all the alias stuff… This is why in general I don’t put up scripts that are drag-and drop. The following is ready to paste into a button:

! _NoEcho _-Runscript (
Option Explicit

Call DistToCamera()
Sub DistToCamera()
	Dim pt,dist,units,prec,msg
	pt = Rhino.GetPoint("Pick point on object")
	If Not IsArray(pt) Then Exit Sub
	dist = Rhino.Distance(pt, Rhino.ViewCamera())
	units = Rhino.UnitSystemName(,, True)
	prec = Rhino.UnitDistanceDisplayPrecision()
	msg = "Distance from pick point to camera is "
	Call Rhino.Print(msg & Cstr(Round(dist, prec)) & " " & units)
End Sub
)

Hope this works better…

4 Likes

Great, that is it.

Thank you very much,
Micha