Script to Snap a mesh vertex to nearest mesh vertex of other mesh

Translated to Python 3.0 using ChatGPT and tested

import Rhino
from Rhino.Geometry import PointCloud

def main():
    # Prompt user to select the first mesh
    result0, objref0 = Rhino.Input.RhinoGet.GetOneObject("Select mesh to edit", False, Rhino.DocObjects.ObjectType.Mesh)
    if result0 != Rhino.Commands.Result.Success:
        print("Failed to select the first mesh.")
        return

    # Prompt user to select the second mesh
    result1, objref1 = Rhino.Input.RhinoGet.GetOneObject("Select static mesh", False, Rhino.DocObjects.ObjectType.Mesh)
    if result1 != Rhino.Commands.Result.Success:
        print("Failed to select the second mesh.")
        return

    # Get tolerance input
    tolerance = 0.1
    result2, tolerance = Rhino.Input.RhinoGet.GetNumber("Tolerance", False, tolerance, 0, 500)
    if result2 != Rhino.Commands.Result.Success:
        print("Failed to get tolerance input.")
        return

    # Get the meshes
    mesh0 = objref0.Mesh()
    mesh1 = objref1.Mesh()

    # Set single-precision vertices
    mesh0.Vertices.UseDoublePrecisionVertices = False
    mesh1.Vertices.UseDoublePrecisionVertices = False

    # Convert vertices to point arrays
    pts = mesh0.Vertices.ToPoint3dArray()
    mesh1_pts = mesh1.Vertices.ToPoint3dArray()

    # Create a PointCloud from the second mesh's vertices
    pc = PointCloud()
    for pt in mesh1_pts:
        pc.Add(pt)

    # Use squared tolerance for efficiency
    tolerance_squared = tolerance * tolerance

    # Iterate through the vertices of the first mesh
    for i in range(len(pts)):
        # Find the closest point in the point cloud
        j = pc.ClosestPoint(pts[i])
        if pc[j].Location.DistanceToSquared(pts[i]) < tolerance_squared:
            # Replace the vertex in mesh0 with the corresponding vertex from mesh1
            mesh0.Vertices.SetVertex(i, mesh1.Vertices[j])

    # Replace the original objects in the document
    doc = Rhino.RhinoDoc.ActiveDoc
    doc.Objects.Replace(objref0.ObjectId, mesh0)
    doc.Objects.Replace(objref1.ObjectId, mesh1)

    # Notify user of success
    print("Meshes updated successfully.")

# Call the main function
if __name__ == "__main__":
    main()