Script to rotate objects 90 degrees around their center


#1

To the scripting world:

I have a feeling there is a script for this, but I am looking for a way to select an object, say in an ortho view (or even in perspective mode) and with an alias have it rotate 90 degrees either vertically or horizontally around it’s center. There would be four aliases: Turn right or left around a vertical axis through its center and turn up or down around a horizontal axis through its center.

Is this doable?

Thank you.


#2

Certainly scriptable, but does BoxEdit work for you? Gumball should also for most objects…

–Mitch


#3

Hi Mitch,

I know that BoxEdit would work and, of course, gumball, but what I’m hoping for is the speed of selecting an object,hitting an alias and having it spin (on its axis). I find that this is something I need to do several times a day, and instead of selecting it and, assuming the gumball is at its center,zooming in, carefully clicking on the right rotate curve, and then typing 90 degrees in the little field, enter. A lot of steps.

Just hoping, that’s all.


#4

Try this one.
It will enable you to rotate objects left and right by 90 degrees around z,y,x axis with following aliases:
‘lz’ - left 90 degrees around z axis, ‘rz’ - right 90 degrees around z axis, ‘ly’ - left 90 degrees around y axis, ‘ry’ - right 90 degrees around y axis, ‘lx’ - left 90 degrees around x axis, ‘rx’ - right 90 degrees around x axis

import rhinoscriptsyntax as rs

def rotateObject(_id):
    if _id:
        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)
        else:
            print "Something is wrong with your geometry"
            return

        directionAxis = rs.GetString("Choose rotation direction and axis", "lz", ["lz","rz","ly","ry", "lx", "rx"])
        if directionAxis == "lz":
            rs.RotateObject(_id, centroid, 90, [0,0,1])
        elif directionAxis == "rz":
            rs.RotateObject(_id, centroid, -90, [0,0,1])
        elif directionAxis == "ry":
            rs.RotateObject(_id, centroid, 90, [0,1,0])
        elif directionAxis == "ly":
            rs.RotateObject(_id, centroid, -90, [0,1,0])
        elif directionAxis == "lx":
            rs.RotateObject(_id, centroid, 90, [1,0,0])
        elif directionAxis == "rx":
            rs.RotateObject(_id, centroid, -90, [1,0,0])
        else:
            print "You typed an unsupported rotation"
            return
    else:
        print "You did not pick anything"
        return

id = rs.GetObject("Pick up the object you wish to rotate", preselect=True)
rotateObject(id)

By the way, did not know about the BoxEdit command.
Thanks Mitch.


#5

Hey djordje,

I really appreciate this. I’m used to working with rvb files, though. (drag and drop in to an open instance of Rhino). How would I use yours?

Thank you.

Cosmas

@djordje


#6

You can use the upper script as a button: Download it, and drop onto your Rhino workspace:
rotateObj.rui (7.1 KB)
Left click on the button “rotateObj” will run the upper script. Right one will always perform rotation of object around z axis, 90 degrees to the left.

It is also possible to assing an Alias to the script, and run it as a command.


#7

Awesome. Thank you very much.

@djordje


#8

Oh man,

I am sorry to harrass you, but it’s not working for me. I am trying to create aliases for your pythonscript and have done the following:
Tools/ PythonScript/ Edit
In the PythonScript dialogue/ Tools/ Options choose one of the folders that are listed. I chose:
c:\users\cdemetriou\appdata\roaming\mcneel\rhinoceros\5.0\scripts
I then extracted your rotateobj.rar file in to this folder.
Then I go to Rhino’s Tools/ Options/ Alias and create an alias for each one of the following:
lz
rz
ly
ry
lx
rx
with the following macro:
_-RunPythonScript "lz.py"
Adjust the above for each respective alias.

When I tried running lz… I got “unknown command”.

There must be a simple step I’m missing. Did I not open or close Rhino at some critical moment? I appreciate your patience.

Thank you.

Cosmas
rotateObj.rar (2.4 KB)

@djordje


#9

Not sure really.
Maybe you could get back to Rhino’s Tools-> Options-> Alias and check whether or not the aliases you defined are still there.
Try restarting Rhino.


#10

It worked. I had put a space between the _ and the - (hyphen). When I took the space away. It worked. Thank you very much, Djordje. This is a real time saver.

I really appreciate it.


#11

Hi!
Is there any way to rotate objects 90 degrees around only C-plane ?


#12

Hi leex - BoxEdit has a CPlane option.


#13

Hi Jarek!

I see.
But Is it possible to do it in the script?


#14

Sure - you want to rotate objects around their bounding box center relative to the current active CPlane? Are you looking to rotate individual objects each about their centers or a group together around a common center?

–Mitch


#15

Hi Mith!
I want to rotate objects around their bounding box center relative to the current active CPlane


#16

OK, here are two from my library, the first rotates each object around its own center relative to CPlane; the second, all selected objects together around a common bounding box center. To change the rotation angle, just change the input argument at the bottom - like 180 instead of 90 for a half-turn, or -90 to rotate clockwise instead of counter-clockwise. I have +/- 90° and 180° set up as keyboard shortcuts here.

RotateSelectedAroundEachBBCtr.py (607 Bytes)

RotateSelectedAroundBBCtr.py (576 Bytes)

–Mitch


Script files to load when Rhino starts
#17

Thank you Mitch!


(Heiwayuuri) #18

Hey all,I want to rotate all select objects.
but, Message: Parameter must be a Guid or string representing a Guid

What should I do?

Thank you.


#19

How are you using the script? With what objects? --Mitch


(Heiwayuuri) #20

I use this script.
Ocjects use Rectangle.