Tracking point with mouse hover and showing Z-value

Hi Python Guru,

I’m pulling my hair trying to fix this issue ‘Message: expected float, got Point3d’.

I’m writing a python script that would allow me to hover my mouse over a group of points and it would dynamically display the Z-value of each point within the search radius. I seem to hit a road block. Has anyone else done similar script? Thanks!

import Rhino
import scriptcontext as sc
import time
import rhinoscriptsyntax as rs
import clr
from Rhino.Display import DisplayConduit
from Rhino.DocObjects import ObjectType
from System.Drawing import Color
from Rhino.Geometry import RTree, Point3d, Line

class HighestZConduit(DisplayConduit):
    def __init__(self):
        self.snap_point = None

    def DrawForeground(self, e):
        if self.snap_point:
            e.Display.DrawPoint(self.snap_point, Rhino.Display.PointStyle.ControlPoint, 5, Color.Red)

def run_highest_z_tracker_loop(search_radius=5.0, duration_sec=30):
    conduit = HighestZConduit()
    conduit.Enabled = True
    print("Tracking for {} seconds...".format(duration_sec))

    view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView
    viewport = view.ActiveViewport

    # Build RTree with all point objects
    rtree = RTree()
    point_lookup = []
    index = 0
    for obj in sc.doc.Objects:
        if obj.ObjectType == ObjectType.Point:
            pt = obj.Geometry.Location
            point_lookup.append(pt)
            rtree.Insert(pt, index)
            index += 1

    start = time.time()
    while time.time() - start < duration_sec:
        cursor_coords = rs.GetCursorPos()
        if not cursor_coords:
            continue
        cursor_point = Point3d(cursor_coords[0], cursor_coords[1], cursor_coords[2])
        cam_location = viewport.CameraLocation

        ray = Line(cam_location, cursor_point)

        hits = []
        def search_callback(sender, args):
            pt = Point3d(point_lookup[args.Id])
            t_ref = clr.StrongBox[float](0.0)
            if ray.ClosestParameter(pt, t_ref):
                t = t_ref.Value
                if isinstance(t, float):
                    closest = ray.PointAt(t)
                    dist = pt.DistanceTo(closest)
                    if dist <= search_radius:
                        hits.append(pt)

        rtree.SearchSphere(cursor_point, search_radius * 2, search_callback)

        if hits:
            best = max(hits, key=lambda p: p.Z)
            conduit.snap_point = best
            print("Highest Z:", round(best.Z, 3))
        else:
            conduit.snap_point = None

        sc.doc.Views.Redraw()
        time.sleep(0.1)

    conduit.Enabled = False
    print("Finished tracking.")

run_highest_z_tracker_loop()