Hi Danny, @dannytso123
Here is my first attempt on an “auto align mesh to XY plane” script.
This is a bit more fancier than a simple pick three points tool, but I had to start where the most fun was
Edit: Updated to version 2
### --- Land a mesh on the XY plane
### --- script by Holo 2020 ---
### --- v1.1
import rhinoscriptsyntax as rs
def pointWhatSideOfVector(a,b,c):
ba=b-a
ca=c-a
result = ((ba.X*ca.Y)-(ba.Y*ca.X))
if result >0:
return "Left"
elif result <0:
return "Right"
else:
return "ON"
def landMesh(obj, steps, degrees):
rs.Prompt( "landing mesh in "+str(degrees)+" steps" )
bbox=rs.BoundingBox(obj)
center = (bbox[0]+bbox[2])/2
size = rs.Distance(bbox[0],bbox[2])
farPoint = rs.coerce3dpoint((center[0],center[1],center[2]))
farPoint.Z -= 1000*size
rs.EnableRedraw(False)
for i in range (steps):
rs.Prompt("Aligning with "+str(degrees)+" degrees. Step "+str(i)+" of "+str(steps) )
rs.Sleep(0)
#rs.Prompt(str(i))
closestPT = rs.MeshClosestPoint(obj, farPoint)[0]
Z_direction = rs.coerce3dpoint((closestPT[0],closestPT[1],closestPT[2]))
Z_direction.Z += 10
PTplane = rs.PlaneFromPoints(closestPT, center, Z_direction)
rs.RotateObject(obj, center, degrees*-1, PTplane.ZAxis, copy=False)
newClosestPT = rs.MeshClosestPoint(obj, farPoint)[0]
result = pointWhatSideOfVector(closestPT,newClosestPT,center)
if result == "Left":
rs.RotateObject(obj, center, degrees*-1, (newClosestPT-closestPT), copy=False)
elif result == "Right":
rs.RotateObject(obj, center, degrees, (newClosestPT-closestPT), copy=False)
bbox=rs.BoundingBox(obj)
rs.MoveObject(obj, (0,0,-1*bbox[0][2]))
rs.EnableRedraw(True)
obj=rs.GetObject("Select mesh to autoalign to XY plane",rs.filter.mesh, preselect=True)
if obj:
landMesh(obj, 15, 2)
landMesh(obj, 25, 1)
landMesh(obj, 5, 0.1)
landMesh(obj, 5, 0.01)