Script to rotate objects 90 degrees around their center

Dunno, seems to work OK here. Are you trying to use it in normal Rhino or in a Grasshopper script component? The script is not designed for Grasshopper.

Sorry, I gave you a misunderstanding.

I can select many objects.but This code selects only one.

OK, I hacked together a fix to use multiple objects…

#original script by djordje 2014, modified by Mitch 2018
python
import rhinoscriptsyntax as rs

def rot_ctr(id):
    centroid=None
    if rs.IsCurve(id) and rs.IsCurveClosed(id):
        centroid = rs.CurveAreaCentroid(id)[0]
    elif rs.IsCurve(id) and not rs.IsCurveClosed(id):
        d = rs.CurveDomain(id)
        centroid = rs.EvaluateCurve(id, (d[0]+d[1])/2)
    elif rs.IsSurface(id) or rs.IsPolysurface(id):
        centroid = rs.SurfaceAreaCentroid(id)[0]
    elif rs.IsMesh(id):
        centroid = rs.MeshAreaCentroid(id)
    return centroid


def RotateObjects90():
    ids = rs.GetObjects("Pick objects to rotate", preselect=True)
    if not ids: return
    
    choices=["lz","rz","ly","ry", "lx", "rx"]
    dirAxis = rs.GetString("Choose rotation direction and axis", "lz", choices)
    if (dirAxis is None) or (dirAxis not in choices): return
    
    rs.EnableRedraw(False)
    for id in ids:
        if rot_ctr(id):
            if dirAxis == "lz":
                rs.RotateObject(id, rot_ctr(id), 90, [0,0,1])
            elif dirAxis == "rz":
                rs.RotateObject(id, rot_ctr(id), -90, [0,0,1])
            elif dirAxis == "ry":
                rs.RotateObject(id, rot_ctr(id), 90, [0,1,0])
            elif dirAxis == "ly":
                rs.RotateObject(id, rot_ctr(id), -90, [0,1,0])
            elif dirAxis == "lx":
                rs.RotateObject(id, rot_ctr(id), 90, [1,0,0])
            elif dirAxis == "rx":
                rs.RotateObject(id, rot_ctr(id), -90, [1,0,0])
RotateObjects90()

HTH, --Mitch

1 Like

I’m so happy!!
Thank you Helvetosaur.

Finally, hear only one wish.
I want to rotate with the height of the object being the lowest.
I do not know how to evaluate objects and rotate.

I really appreciate your kindness.

I don’t quite understand this - you want to rotate around each object’s center, but projected to the object’s lowest point?

Exactly what i’m looking for.
But Could i ask you for some help to tweek it a little bit ?
I’ve made a custom Keyboard for a 3D navigation mouse, and i’ve added a rotary encoder. I can assign a Keyboard input when turning the rotary encoder one way or another. My Idea would be to be able to assign a keyboard shortcut to the encoder in order to turn my objet, by increments of 18 Degrees, since i’ve got 20 Steps on my encoder. Is there a way to assign the LX (For example) as a keyboard Alias ?
Many Thanks