Trying to create a rectangle at a given point.. failing hard at it

Hey guys.
New to python and rhino, but not new to programming.
I have been programming in C# in unity3D for the past 4 years now as a hobby. Got a job where they use rhino and started right away making scripts to save time lol.

So… I am making a form that pops up. The user enters all the data, and then it changes all the text fields in the template. Works pretty darn good and the time that saves is awesome!

But I am having a problem with addRectangle. Again, I am new to python and rhinoscript so it’s throwing me off.

Here is how I am currently trying to do it.

texturePOB = rs.ObjectsByName("BOMTexture P1")
getTexturePoint = rs.ObjectType(texturePOB)
textureP1 = rs.PointCoordinates(getTexturePoint)
if textureP1: rs.AddRectangle( textureP1 , 5.0, 15.0 )

Now I understand the error that it’s throwing me, but I just don’t know enough of rhinoscirpt to find what I am looking for. In unity I would save the Vector3 of the transform of the object… found transform… I even found Vector3… but nothing that helps me in this program lol.

Any help would be very appreciated :slightly_smiling:


can you try to explain what are you´re planning to do ? Some notes on the snippet of code posted above:

texturePOB = rs.ObjectsByName("BOMTexture P1")

the rs.ObjectsByName method returns a list of object ids or None if no object with this name has been found. So you cannot pass the result in the next line:

getTexturePoint = rs.ObjectType(texturePOB)

since rs.ObjectType is expecting a single object id not a list of ids which is returned even when a single object has been found with that name. The rs.ObjectType method returns an integer which stands for one of the object types listed. You cannot use this integer in the next line:

textureP1 = rs.PointCoordinates(getTexturePoint)

since rs.PointCoordinates method expects the id of a point object. This method might be used to return or change the location of a point object. In the first case, where it returns a single point tuple like (0,0,0), you could not pass this to the next method:

if textureP1: rs.AddRectangle( textureP1 , 5.0, 15.0 )

since rs.AddRectangle expects a plane (no point) as the first argument. In order to find out how all these rs methods and functions work, you might use the rhinoscript python editor to write your code, open it using _EditPythonScript then use F1 to open the documentation from where you can lookup how a method works. Most of the methods are listed with short examples. There is an online source as well with the same content.


1 Like

Thanks for the reply :slightly_smiling:

From debugging all my code. It returns the object I named BOMTexture P1 just fine. If it finds two of them, then I can just call the number of the array ie: (BomTexture P1[1])
From what I have read, if the list is only 1, the default return is the one object it found.
That’s why I am using it like this.
I did this same way to find all my objects with the name DATE, then I did a loop to change them all to the current date and just stepped i inside of DateTextObject[i]

So since it does grab only the one object, ObjectType works just fine too. It makes getTexturePoint into a Point. Since texturePOB is a point to begin with.

If I run my entire code and do
if textureP1: print textureP1
I get the xyz coord of that point it grabbed before (texturePOB).

So… since it can do all this… and you’re saying it can’t… now I’m really confused lol.

All I am trying to do is, create a rectangle at a certain place in my template. I used a point, since that would be much for easy to grab

Imagine… I am building a parts inventory spreadsheet.

Oh, and thanks for that tip on F1… I was using the api in one window and notepad++ lmao.

I’ve had a long day today at work… I hope all that made sense.

if i run this, with a single point named “BOMTexture P1” or even in an empty scene i get this error:

Message: Parameter must be a Guid or string representing a Guid

your code did not show that you´re indexing the result using a variable like texturePOB[0].

try this in a scene with 1 point object named “BOMTexture P1”:

import rhinoscriptsyntax as rs
def DoSomething():
    ids = rs.ObjectsByName("BOMTexture P1")
    if not ids: return
    if len(ids) != 1: return
    point = rs.PointCoordinates(ids[0])
    plane = rs.WorldXYPlane()
    rs.MovePlane(plane, point)
    rs.AddRectangle(plane, 5.0, 15.0)
if __name__=="__main__":

Any better ?


Again, thanks for the help man…

To be honest… I wrote this post like RIGHT when I woke up. I wanted to post it in the morning and let it boil all day lol.

What I pasted here, was old code during my trail and errors. Sorry. I am indexing the array.

But, what you did post did not work.
rs.MovePlane(plane, point) Did nothing. It just created the rectangle like normal at 0,0,0

What I have done is this

def Texture():
    getOrigin = rs.ObjectsByName("orginPoint") #this is just a point at 0,0,0
    ids = rs.ObjectsByName("TexturePoint")
    point1 = rs.PointCoordinates(getOrigin[0])
    point2 = rs.PointCoordinates(ids[0])
    plane = rs.WorldXYPlane()
    plane = rs.RotatePlane(plane, -90.0, [0,0,1])
    rect = rs.AddRectangle(plane, 5.0, 15.0)
    translation = point2 - point1
    rs.MoveObject(rect, translation)

I know it’s not pretty, but it gets the job done :slightly_smiling:

Do you have any input on what I came up with?

Looks good now. Btw. you can also reference the origin using something like

import Rhino
origin = Rhino.Geometry.Plane.WorldXY.Origin

or just by setting the point in a variable without creating a point object at all if it is not required:

origin = rs.coerce3dpoint( (0,0,0) )

the reason why my plane above was not moved is that i forgot to apply the output to a variable. Normally i create my plane and set origin like this:

import Rhino
plane = Rhino.Geometry.Plane.WorldXY
plane.Origin = Rhino.Geometry.Point3D(1,2,3)