Balancing a Human Foot Mesh

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 :wink:

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)

1 Like

And here is the simpler “Align by 3 points” script.

Edit: Updated to v2

### --- Aligh a mesh by 3 points 
### --- script by Holo 2020 --- 
### --- v2.0 

import rhinoscriptsyntax as rs

def ReorientObjects(object, points):
    rs.EnableRedraw(False)
    if len(points)>2:
        
        plane=rs.PlaneFromPoints(points[0],points[1],points[2])
        Origin = plane[0]
        circle =rs.AddCircle(plane,150)
        
        reference = (plane.Origin, plane.Origin+plane.XAxis, plane.Origin+plane.YAxis)
        
        xyOrigin = rs.coerce3dpoint((points[0].X,points[0].Y,0))
        xy1 = rs.coerce3dpoint((points[1].X,points[1].Y,0))
        xy2 = rs.coerce3dpoint((points[2].X,points[2].Y,0))
        
        xyPlane = rs.PlaneFromPoints(xyOrigin,xy1,xy2)
        xyCircle = rs.AddCircle(xyPlane,150)
        
        target = (xyPlane.Origin, xyPlane.Origin+xyPlane.XAxis, xyPlane.Origin+xyPlane.YAxis)
        
        
        rs.OrientObject (object, reference, target, flags=0)
        
    rs.EnableRedraw(True)

object = rs.GetObject("Object to reorient", preselect=True)
if object:
    points=rs.GetPoints(draw_lines=True, message1="get points", max_points=3)
    if points:
        if len(points)==3:
            ReorientObjects(object, points)
        else:
            print "Error: 3 points are needed"

These are quick python scripts, so I skipped all annotations and also add and delete temporary objects in the document instead of coding in more complex Rhino Common style. So that should make it easier to understand for those who in the future stumple upon this.

1 Like

Hi Holo!

That looks like quite an impressive script that required a lot of your personal, time, and resources! Thank you for providing this to me. At the moment I believed I have my solution but I am always open to making things more efficient or experimenting with different commands and workflows. I will let you know how it goes once I try it out! I might need to contact you again to ask you how the heck I would apply this! Thanks again Holo!

Hey man,
since you are doing such important work I decided to make a custom toolbar with icons for you :slight_smile:

image

Version 2:
Happy_Feet.rui (11.4 KB)

Just store the rui file on your system (on a place where you won’t accidentally loose it, or forget where it was when you need to reinstall Rhino so a dropbox folder is smart) and then drag it into your Rhino window and it will automagically appear :slight_smile:

Note:
Rhino stores it’s workspace when you close it, so make sure ONLY one instance of Rhino is running when you do it and then close it and reopen Rhino. (If you have two instances open and then close the one with the toolbar in it first then the other Rhino will override the workspace when you close that one, so then you have to redo the “installation” later on)

Good luck and hope you find the auto tool useful and fun to watch.

1 Like

hello, thanks the command, however I see that if I select a box in space it does not align correctly with the planeC

Hi Eliel,
thanks for the feedback. I obviously had a bug there, so I swapped to a totally different approach that should work much better.

Edit: I udated the toolbar file in the post above, so please redownload it and save it to the same location as last time.

I also updated the Auto script so it workes a bit faster on heavy meshes. (But it is slow when the meshes reaches 200.000 faces and above.)

And note that the Auto mode does NOT take center of gravity into account, it just lands the mesh on three points even if center of mass is outside of their boundry.

1 Like

Thanks a bunch Holo! I tried dragging it into Rhino. A pop asked me if I wanted to open it or insert it. And then nothing happened. FYI!

Shoot, I just realized you are on MAC, so I need some help here.
@wim do you know how to convert a toolbar from PC to MAC?

That isn’t possible yet.
The U/I between the two does not currently allow it.
It’s going to take some dark magic to make it possible.
I know it’s on the developer’s list.